Functions
addCommas(input)
- Accepts
number | string. Anything else returns"". - Strings have existing commas stripped first.
- If
isPersian(...)recognises the input as Persian,digitsFaToEnis applied first. - The cleaned string must match
/^-?\d+(\.\d+)?$/. Otherwise returns"".
removeCommas(value)
- Input must be a string; otherwise throws
TypeError("PersianTools: removeCommas - The input must be string"). - Strips commas (and optional whitespace after each comma) then
Number(...). - Empty string returns
0(becauseNumber("") === 0). - Non-numeric content returns
NaN— check withNumber.isNaN(...).
Pitfalls
addCommasreturns""for unrecognised input — silently, no throw.- Arabic-Indic digits (
٠-٩) are NOT handled.isPersian("٧٨")is false, so they fall through and fail the final regex →"". Normalize withdigitsArToEnorautoConvertDigitsToENfirst. removeCommasreturns anumber, not a string. For round-tripping, recompose withaddCommas.removeCommasdoes not convert Persian/Arabic digits. Pre-normalize.
Composition
Source
src/modules/commas/add.ts, src/modules/commas/remove.ts · Tests: test/addCommas.spec.ts, test/removeCommas.spec.ts