Files
boursecu_user/src/utils/persianDigits.ts
T
safavimohadeseh78andCursor a5e458ed25 Split desktop and mobile layouts with shared page data so APIs fetch once per context.
Add layout shells, desktop views, module-level page caches, and UI fixes for auth, home, wallet, transactions, address, and related pages.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-14 10:08:46 +03:30

30 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const PERSIAN_DIGITS = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'] as const
export function toPersianDigits(value: string | number | null | undefined): string {
if (value === null || value === undefined) return ''
return String(value).replace(/\d/g, (digit) => PERSIAN_DIGITS[Number(digit)] ?? digit)
}
export function toEnglishDigits(value: string): string {
return value.replace(/[۰-۹]/g, (digit) => {
const index = PERSIAN_DIGITS.indexOf(digit as (typeof PERSIAN_DIGITS)[number])
return index >= 0 ? String(index) : digit
})
}
export function formatFixed(value: string | number | null | undefined, decimals = 2): string {
const num = Number(value)
if (!Number.isFinite(num)) return toPersianDigits(String(value ?? ''))
return toPersianDigits(num.toFixed(decimals))
}
export function formatPercent(
value: number,
decimals = 2,
options: { showSign?: boolean; suffix?: string } = {},
): string {
const { showSign = false, suffix = '%' } = options
const prefix = showSign && value > 0 ? '+' : value < 0 ? '-' : showSign ? '+' : ''
return `${prefix}${formatFixed(Math.abs(value), decimals)}${suffix}`
}