Skip to content

Install Numora

Add the Numora TypeScript numeric input library to any project. No framework required.

Install

Numora has zero runtime dependencies. Pick your package manager:

pnpm add numora

Basic usage

Import the NumoraInput class and pass it a container element:

import { NumoraInput } from 'numora'

const container = document.querySelector<HTMLElement>('#my-input-container')!

const numoraInput = new NumoraInput(container, {
  decimalMaxLength: 2,
  // typing 1234.56 displays "1,234.56"
})

Options

The second argument is an optional options object (NumoraInputOptions). It extends NumoraOptions with the callbacks, defaultValue and a few native attributes. You can leave any of them out.

Formatting

OptionDefaultWhat it does
localeunsetA BCP 47 tag ('de-DE') or true for the runtime's locale. Sets thousandSeparator, decimalSeparator and groupingStyle. If you also pass one of those, yours wins. See Locale.
formatOn'change'Adds separators on every keystroke ('change') or when the field loses focus ('blur'). See Formatting.
groupingStyle'thousand''thousand' (1,234,567), 'lakh' (12,34,567), 'wan' (123,4567) or 'none'. 'none' is the only way to turn grouping off.
thousandSeparator','The character between digit groups. Exactly one character. It cannot be a digit or one of - + e E k K m M b B t T. Pass it alone and, if it lands on the decimal separator, the decimal separator switches to the other of . and ,. Pass both separators as the same character and Numora throws. Every error is in the invalid options table.
decimalSeparator'.'The character between the integer and decimal parts. Exactly one character, with the same rules as thousandSeparator.
decimalMaxLength18Maximum number of decimal digits. Numora cuts extra digits off and never rounds. See Decimals.
decimalMinLength0Pads decimals with zeros up to this many digits. Numora pads on blur and when your app sets the value, never while the user types.

What the user can enter

OptionDefaultWhat it does
allowNegativefalseAllows a leading -.
allowLeadingZerosfalseKeeps leading zeros (007) instead of removing them. See Leading zeros.
padLeadingZerofalsePuts a 0 in front of a bare decimal separator (.5 becomes 0.5).
allowCompactNotationfalseExpands 1k to 1000. See Compact notation.
allowScientificNotationtrueExpands a pasted or app-set 1.5e-7 to 0.00000015. A typed e is never accepted. false rejects such a paste. See Scientific notation.
maxLengthunsetThe most characters the raw value can have. Digits, the decimal separator and a leading - count. Grouping separators and decimalMinLength padding don't. See Max length.
isAllowedunset(rawValue) => boolean. Return false to reject a keystroke or paste. See Validation.

Value and callbacks

OptionWhat it does
defaultValueThe initial value, as a raw value ('1234.5' in every locale). Later writes go through setValue(). Numora ignores a value option. See Value types.
onValueChangeCalled with { value, formattedValue } when the raw value changes. value is the raw value: ASCII digits, a . decimal, no grouping. An update() that changes the raw value calls it too.
onFormattedValueChangeCalled with the field's text every time Numora rewrites it and the text is new. That includes the rewrites onValueChange says nothing about: grouping on blur, removing separators on focus, setValue(), and an update() that changes only the format. Not called on creation.

Native attributes

Numora copies these onto the <input>: id, name, className, placeholder, disabled, readOnly, required, tabIndex, title, autofocus and ariaLabel, which sets aria-label. Set anything else on numoraInput.element.

Don't set the native maxLength on the element. It counts grouping separators, and the Numora option doesn't.

The instance

Pass a container and Numora creates an <input> inside it. Pass an existing <input> and Numora adopts it. The instance has three read-only properties and three methods:

numoraInput.rawValue        // '1234.5', the value to store or send
numoraInput.formattedValue  // '1,234.5', the text in the field
numoraInput.element         // the <input> itself

// Write a raw value. It is sanitized and formatted; onValueChange is not called.
numoraInput.setValue('2500')

// Change options. They merge over the current ones, so decimalMaxLength stays 2.
// The field now shows '2.500'. rawValue is still '2500'.
numoraInput.update({ locale: 'de-DE' })

// Remove Numora's listeners. The <input> stays in the page.
numoraInput.destroy()

update() follows four rules:

  • A separator you passed yourself wins over a locale set later. A field created with thousandSeparator: ',' and decimalSeparator: '.' still shows 2,500 after update({ locale: 'de-DE' }).
  • A key set to undefined goes back to its default: update({ locale: undefined }) removes the locale.
  • If the merged options are invalid, update() throws. The field keeps its old options and its value.
  • Numora calls onValueChange only if the raw value changed, and onFormattedValueChange only if the text changed. A new locale changes the text and not the raw value. A lower decimalMaxLength can change both.

Everything the package exports

numora has three entry points.

  • numora exports the NumoraInput class and three functions that need no input element: formatValue, parseValue and getSeparatorsFromLocale. It exports two constants, GroupingStyle and FormatOn, which hold the option values: GroupingStyle.Lakh is 'lakh'. It exports the types NumoraOptions, NumoraInputOptions, NumoraValues and LocaleTag.
  • numora/pipeline exports each sanitization step as its own function. It has no semver guarantee: a step can change or go in a minor release. See Sanitization.
  • numora/internal holds code shared with numora-react. It has no semver guarantee either. Don't import from it.

TypeScript

Numora ships its own declarations. Import types directly:

import type { NumoraOptions } from 'numora'

const options: NumoraOptions = { decimalMaxLength: 6 }