Skip to main content
Two complementary helpers for thousand-separator formatting.

Functions

addCommas(input)

Behaviour
  • Accepts number | string. Anything else returns "".
  • Strings have existing commas stripped first.
  • If isPersian(...) recognises the input as Persian, digitsFaToEn is applied first.
  • The cleaned string must match /^-?\d+(\.\d+)?$/. Otherwise returns "".

removeCommas(value)

Behaviour
  • 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 (because Number("") === 0).
  • Non-numeric content returns NaN — check with Number.isNaN(...).

Pitfalls

  • addCommas returns "" 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 with digitsArToEn or autoConvertDigitsToEN first.
  • removeCommas returns a number, not a string. For round-tripping, recompose with addCommas.
  • removeCommas does 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