fn

maskFromTemplate

v0.0.14

Compile a human-readable template into a mask array. Token characters become matcher slots; everything else becomes a fixed literal. With the default tokens the compiled array is cached and shared (frozen) per template string; pass a custom tokens map to opt out.

Example

ts
maskFromTemplate('##/##/####'); // [/\d/, /\d/, '/', /\d/, /\d/, '/', /\d/, /\d/, /\d/, /\d/]

Signature

ts
export function maskFromTemplate(
  template: string,
  tokens: Readonly<Record<string, RegExp>> = DEFAULT_MASK_TOKENS,
): ReadonlyArray<RegExp | string>{ ... }

Parameters

ParameterTypeDescription
templatestringe.g. '+1 (###) ###-####'
tokens?Readonly<Record<string, RegExp>>Token → matcher map

Returns

ReadonlyArray<RegExp | string>The compiled mask expression