Skip to main content
wordsToNumber parses Persian number words (e.g. "سه هزار دویست و دوازده") into a number, with optional fuzzy matching and a choice of output digit system.

Function

Return type narrowed by overloads:
  • addCommas: truestring
  • digits: "fa" | "ar"string
  • otherwise → number
  • falsy input → "" (string)

Basic usage

Output formatting

digits accepts "en" | "fa" | "ar" — not "persian" / "english" / "both".

Fuzzy mode

Enables typo correction via a curated TYPO_LIST plus Levenshtein matching against the vocabulary. Slower; use for voice/OCR input, skip for structured input.

Auto-normalization

  • autoConvertDigitsToEn: true (default) — "۱۲ هزار" and "١٢ هزار" both work.
  • autoConvertArabicCharsToPersian: true (default) — "یكصد" (Arabic kaf) is corrected before lookup.
Disable these only if your upstream pipeline already normalizes.

Silent skipping

Unknown tokens are silently skipped rather than treated as errors:
For strict validation, round-trip through numberToWords and compare.

Pitfalls

  • digits enum: "en" | "fa" | "ar". Older docs use "persian" | "english" | "both" — wrong.
  • Falsy input returns "" (string), not null or 0. Guard with a truthy check.
  • No null returns for unrecognized input. Tokens are silently skipped — see “Silent skipping” above.
  • Return type depends on options. Use the overloads; cast at call sites that mix configurations.

Source

src/modules/wordsToNumber/index.ts, constants.ts, fuzzy.ts · Tests: test/wordsToNumber-fuzzy.spec.ts