wordsToNumber parses Persian number words (e.g. "سه هزار دویست و دوازده") into a number, with optional fuzzy matching and a choice of output digit system.
Function
addCommas: true→stringdigits: "fa" | "ar"→string- otherwise →
number - falsy input →
""(string)
Basic usage
Output formatting
digitsaccepts"en" | "fa" | "ar"— not"persian" / "english" / "both".
Fuzzy mode
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.
Silent skipping
Unknown tokens are silently skipped rather than treated as errors:numberToWords and compare.
Pitfalls
digitsenum:"en" | "fa" | "ar". Older docs use"persian" | "english" | "both"— wrong.- Falsy input returns
""(string), notnullor0. Guard with a truthy check. - No
nullreturns 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