Skip to content

Sanitization

Numora runs every value through a sanitization pipeline before it formats. Watch three messy values go through it. Each step marks what it is about to change. Steps with nothing to do are skipped:

"$.75" goes through ten steps in order. 2 of them change it. The result is "0.75".

Anatomy: sanitization covers the pipeline in full. This page covers character filtering and the standalone removeThousandSeparators utility.

Non-numeric character filtering

Numora removes invalid characters and keeps:

  • Digits (0-9)
  • Decimal separator (configured via decimalSeparator)
  • Negative sign (-) if allowNegative is true

Numora keeps a sign character only in the leading position. A - anywhere else in the string is removed and the rest of the input stays, so "5-3" becomes "53". Numora also removes a leading +, so "+5" becomes "5", because it has no explicit positive sign.

While typing, . and , are both decimal keys, whatever the configured separator.

What the figure leaves out

The figure has every optional step switched on. A default field skips three of the ten: compact expansion runs only with allowCompactNotation, the leading 0 is added only with padLeadingZero, and the cut at maxLength runs only when maxLength is set. So a default field turns $.75 into .75, and 0.75 only with padLeadingZero. Leading zeros are removed unless you set allowLeadingZeros.

The pipeline also does four things the ten steps don't name:

  • Length cap. Numora reads the first 10,000 characters of any text and drops the rest.
  • Whitespace and invisible characters. Step 1 removes every whitespace character, zero-width characters (U+200B to U+200D, U+2060) and bidi marks (U+200E, U+200F, U+061C, U+202A to U+202E, U+2066 to U+2069). 1 234 becomes 1234.
  • Paste format detection. Pasted, dropped and autofilled text is first checked for another locale's format. The figure skips this and reads its text with the field's own separators. Two different marks: the last one is the decimal mark, so 1.234,56 pasted into an en-US field is read as 1234.56. One mark, repeated: it is grouping, so 1.234.567 is read as 1234567. One mark, once: it is grouping when the digits around it form valid groups (1,234) and the decimal mark when they do not (1.5). The field's own decimal separator is always the decimal mark. Dropped or autofilled text in a focused field that formats on change is the exception: Numora reads it as its own display text.
  • Minus signs and decimal glyphs. Step 3 turns fullwidth and other native digits into 0-9. It turns three minus variants into -: U+2212, U+FF0D and U+FE63. An en dash or em dash is not a minus sign and is removed, so –5 becomes 5 even with allowNegative. It turns a native decimal mark (٫, ،, ,, ., 。) into the field's decimal separator.

removeThousandSeparators

Numora exports a standalone removeThousandSeparators utility that strips thousand separators from a formatted string. Use it to get a raw numeric value out of a display string.

To read the raw value from the input, see Value types.

import { removeThousandSeparators } from 'numora/pipeline'

removeThousandSeparators('1,234,567.89', ',') // → '1234567.89'
removeThousandSeparators('1.234.567,89', '.') // → '1234567,89'
removeThousandSeparators('1 234 567', ' ')    // → '1234567'