diff --git a/apps/api/src/contacts/normalize.ts b/apps/api/src/contacts/normalize.ts index f84c65f..bbd4ded 100644 --- a/apps/api/src/contacts/normalize.ts +++ b/apps/api/src/contacts/normalize.ts @@ -1,19 +1,29 @@ -export function emptyToNull(value?: string | null): string | null { - if (value == null) return null; - const trimmed = value.trim(); - return trimmed.length ? trimmed : null; -} - -export function normalizeEmail(value?: string | null): string | null { - const v = emptyToNull(value); - return v ? v.toLowerCase() : null; -} - -/** Keep digits and leading + for Iranian/international numbers. */ -export function normalizePhone(value?: string | null): string | null { - const v = emptyToNull(value); - if (!v) return null; - const cleaned = v.replace(/[^\d+]/g, ''); - return cleaned.length ? cleaned : null; -} - \ No newline at end of file +export function emptyToNull(value?: string | null): string | null { + if (value == null) return null; + const trimmed = value.trim(); + return trimmed.length ? trimmed : null; +} + +export function normalizeEmail(value?: string | null): string | null { + const v = emptyToNull(value); + return v ? v.toLowerCase() : null; +} + +const FA_DIGITS = '۰۱۲۳۴۵۶۷۸۹'; +const AR_DIGITS = '٠١٢٣٤٥٦٧٨٩'; + +/** ارقام فارسی و عربی را به لاتین برمی‌گرداند تا از شماره حذف نشوند. */ +function latinDigits(value: string): string { + return value.replace(/[۰-۹٠-٩]/g, (ch) => { + const fa = FA_DIGITS.indexOf(ch); + return String(fa >= 0 ? fa : AR_DIGITS.indexOf(ch)); + }); +} + +/** Keep digits and leading + for Iranian/international numbers. */ +export function normalizePhone(value?: string | null): string | null { + const v = emptyToNull(value); + if (!v) return null; + const cleaned = latinDigits(v).replace(/[^\d+]/g, ''); + return cleaned.length ? cleaned : null; +}