mask, unmask, isComplete
Pure functions: they work without the DOM — in the browser, on the server, in tests. The mask can be an object or a pattern string.
Signature
ts
function mask(value: string, mask: MaskInput): string
function unmask(value: string, mask: MaskInput): string
function isComplete(value: string, mask: MaskInput): boolean
function isValid(value: string, mask: MaskInput): boolean| Function | Returns |
|---|---|
mask | The formatted value |
unmask | The unmasked value — for data and the database |
isComplete | Whether all required positions are filled |
isValid | Filled and correct: the date exists, the checksum matches — see Ready-made masks |
null and undefined count as an empty string.
Examples
ts
import { mask, unmask, isComplete } from 'tinyval-mask';
import { phone } from 'tinyval-mask/presets';
mask('89991234567', phone()); // '+7 (999) 123-45-67'
unmask('+7 (999) 123-45-67', phone()); // '79991234567'
isComplete('+7 (999) 123', phone()); // false
isValid('4111 1111 1111 1112', card()); // false — wrong check digit
mask('20261002', '9999-99-99'); // '2026-10-02'ts
// server: normalize phones from any source
const normalized = users.map((u) => ({ ...u, phone: unmask(u.phone, phone()) }));
// '8 (999) 123-45-67', '+7 999 1234567', '9991234567' → '79991234567'See also
- Masks & validation
- attach — a mask on an input field