Skip to main content
Iranian and Arabic input often arrives with three different numeral systems mixed together. These functions move between them and provide an autoConvertDigitsToEN helper that normalizes any input to English digits.

The three systems

These are disjoint ranges — Persian digits ≠ Arabic-Indic digits despite casual users using the names interchangeably.

Converters

autoConvertDigitsToEN is the right default when ingesting unknown user input (form field, copy-paste, OCR).

Input rules and errors

  • digitsEnToFa / digitsEnToAr accept string | number. Anything else throws TypeError("PersianTools: digitsEnToFa - The input must be string or number") (and similarly for digitsEnToAr).
  • digitsFaToEn, digitsFaToAr, digitsArToEn, digitsArToFa accept string. Anything else throws TypeError("PersianTools: <fn> - The input must be string").
  • autoConvertDigitsToEN passes falsy input through as-is (no throw).

Exported constants

Use these when building larger custom regexes — don’t re-declare the character classes inline.

Types

Pitfalls

  • The strict converters throw on null / undefined / wrong types — they do NOT gracefully return "". Wrap in try/catch or guard upstream.
  • digitsFaToEn leaves Arabic digits untouched. Use autoConvertDigitsToEN for mixed input.
  • Hand-rolling with String.fromCharCode math is fragile — the Persian and Arabic ranges sit at different offsets. Use these functions.

Source

src/modules/digits/ (converters/, digits.constants.ts) · Tests: test/digits.spec.ts