Skip to content

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
FunctionReturns
maskThe formatted value
unmaskThe unmasked value — for data and the database
isCompleteWhether all required positions are filled
isValidFilled 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 ​