52 lines
1.8 KiB
TypeScript
52 lines
1.8 KiB
TypeScript
const PERSIAN_DIGITS = '۰۱۲۳۴۵۶۷۸۹'
|
||
const ARABIC_DIGITS = '٠١٢٣٤٥٦٧٨٩'
|
||
|
||
/** تبدیل ارقام فارسی/عربی و جداکننده اعشار به فرمت انگلیسی */
|
||
export function persianToEnglishDigits(value: string | number | null | undefined): string {
|
||
if (value == null) {
|
||
return ''
|
||
}
|
||
|
||
return String(value)
|
||
.replace(/[۰-۹]/g, (d) => String(PERSIAN_DIGITS.indexOf(d)))
|
||
.replace(/[٠-٩]/g, (d) => String(ARABIC_DIGITS.indexOf(d)))
|
||
.replace(/٫/g, '.')
|
||
}
|
||
|
||
export function shouldNormalizeInputDigits(options: {
|
||
isPassword?: boolean
|
||
type?: string
|
||
inputmode?: string
|
||
}): boolean {
|
||
if (options.isPassword) {
|
||
return true
|
||
}
|
||
const type = String(options.type ?? '').trim().toLowerCase()
|
||
if (type === 'price' || type === 'number' || type === 'tel') {
|
||
return true
|
||
}
|
||
const mode = String(options.inputmode ?? '').trim().toLowerCase()
|
||
return mode === 'numeric' || mode === 'decimal' || mode === 'number'
|
||
}
|
||
|
||
/** تبدیل ارقام فارسی/عربی در فیلدهای عددی فرمهای احراز هویت و پروفایل */
|
||
export function normalizeAuthFormFields(fields: {
|
||
mobile?: string | null
|
||
password?: string | null
|
||
token?: string | null
|
||
n_code?: string | number | null
|
||
}) {
|
||
if (fields.mobile != null && fields.mobile !== '') {
|
||
fields.mobile = persianToEnglishDigits(String(fields.mobile).trim())
|
||
}
|
||
if (fields.password != null && fields.password !== '') {
|
||
fields.password = persianToEnglishDigits(String(fields.password))
|
||
}
|
||
if (fields.token != null && fields.token !== '') {
|
||
fields.token = persianToEnglishDigits(String(fields.token).trim())
|
||
}
|
||
if (fields.n_code != null && fields.n_code !== '') {
|
||
fields.n_code = persianToEnglishDigits(String(fields.n_code).trim())
|
||
}
|
||
}
|