or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-text-mask-addons

Ready-to-use addons and extensions for the Text Mask input masking library, including specialized masks and pipes for common formatting scenarios.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/text-mask-addons@3.7.x

To install, run

npx @tessl/cli install tessl/npm-text-mask-addons@3.7.0

index.mddocs/

Text Mask Addons

Text Mask Addons provides ready-to-use masks and pipes for the Text Mask input masking library. It offers specialized input formatters for common scenarios including currency/number formatting, email validation, and intelligent date input correction.

Package Information

  • Package Name: text-mask-addons
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install text-mask-addons

Core Imports

import { createNumberMask, emailMask, createAutoCorrectedDatePipe } from "text-mask-addons";

For individual imports (recommended for smaller bundle size):

import createNumberMask from 'text-mask-addons/dist/createNumberMask';
import emailMask from 'text-mask-addons/dist/emailMask';
import createAutoCorrectedDatePipe from 'text-mask-addons/dist/createAutoCorrectedDatePipe';

CommonJS:

const { createNumberMask, emailMask, createAutoCorrectedDatePipe } = require("text-mask-addons");

Basic Usage

import { createNumberMask, emailMask, createAutoCorrectedDatePipe } from "text-mask-addons";

// Currency mask
const currencyMask = createNumberMask({
  prefix: '$',
  includeThousandsSeparator: true,
  allowDecimal: true,
  decimalLimit: 2
});

// Email mask (use directly)
const emailInputMask = emailMask;

// Date correction pipe
const datePipe = createAutoCorrectedDatePipe('mm/dd/yyyy', {
  minYear: 1900,
  maxYear: 2100
});

Capabilities

Number/Currency Formatting

Creates configurable number and currency input masks with support for prefixes, suffixes, thousands separators, decimals, and negative numbers.

/**
 * Creates a number/currency formatting mask function
 * @param {NumberMaskConfig} config - Configuration options
 * @returns {MaskFunction} Mask function that takes rawValue and returns mask array
 */
function createNumberMask(config = {}): MaskFunction;

Configuration Options:

interface NumberMaskConfig {
  /** Text to display before the amount. Defaults to '$' */
  prefix?: string;
  /** Text to display after the amount. Defaults to '' */
  suffix?: string;
  /** Whether to separate thousands. Defaults to true */
  includeThousandsSeparator?: boolean;
  /** Character for thousands separation. Defaults to ',' */
  thousandsSeparatorSymbol?: string;
  /** Allow decimal fraction input. Defaults to false */
  allowDecimal?: boolean;
  /** Character for decimal point. Defaults to '.' */
  decimalSymbol?: string;
  /** Maximum digits after decimal. Defaults to 2 */
  decimalLimit?: number;
  /** Always include decimal point. Defaults to false */
  requireDecimal?: boolean;
  /** Allow negative numbers. Defaults to false */
  allowNegative?: boolean;
  /** Allow leading zeros. Defaults to false */
  allowLeadingZeroes?: boolean;
  /** Maximum length of integer part. Defaults to null (unlimited) */
  integerLimit?: number | null;
}

Usage Examples:

// Basic currency mask
const dollarMask = createNumberMask();
// Uses default: prefix '$', thousands separator, no decimals

// European currency format
const euroMask = createNumberMask({
  prefix: '€ ',
  thousandsSeparatorSymbol: '.',
  decimalSymbol: ',',
  allowDecimal: true
});

// Percentage input
const percentMask = createNumberMask({
  prefix: '',
  suffix: '%',
  allowDecimal: true,
  decimalLimit: 1
});

// Integer-only with custom limits
const integerMask = createNumberMask({
  prefix: '',
  includeThousandsSeparator: false,
  integerLimit: 6,
  allowNegative: true
});

Email Address Formatting

Provides email input masking with validation and formatting assistance.

/**
 * Email mask object containing both mask and pipe functions
 * Used directly with Text Mask - no configuration needed
 */
const emailMask: {
  /** 
   * Mask function for email input formatting
   * @param {string} rawValue - Current input value
   * @param {Object} config - Text Mask configuration object
   * @returns {Array} Mask array for input formatting
   */
  mask: (rawValue: string, config: MaskConfig) => Array<string | RegExp>;
  /** 
   * Pipe function for email input validation and correction
   * @param {string} conformedValue - Value after mask is applied
   * @param {Object} config - Text Mask configuration object
   * @returns {string | false} Corrected value or false to reject
   */  
  pipe: (conformedValue: string, config: PipeConfig) => string | false;
};

Text Mask Configuration Types:

interface MaskConfig {
  placeholderChar: string;
  currentCaretPosition: number;
}

interface PipeConfig {
  currentCaretPosition: number;
  rawValue: string;
  previousConformedValue: string;
  placeholderChar: string;
}

Usage Example:

import emailMask from 'text-mask-addons/dist/emailMask';

// Use directly with Text Mask component
// emailMask is a special object containing both mask and pipe functions
// Text Mask automatically separates them internally
// The mask handles @ and . positioning automatically
// The pipe validates email format and prevents invalid input

Date Input Auto-Correction

Creates intelligent date input pipes that auto-correct user input, prevent invalid dates, and assist with date formatting.

/**
 * Creates a date auto-correction pipe function
 * @param {string} dateFormat - Date format pattern. Defaults to 'mm dd yyyy'
 * @param {DatePipeConfig} config - Configuration options
 * @returns {DatePipeFunction} Pipe function for date correction
 */
function createAutoCorrectedDatePipe(dateFormat = 'mm dd yyyy', config = {}): DatePipeFunction;

Configuration Options:

interface DatePipeConfig {
  /** Minimum allowed year. Defaults to 1 */
  minYear?: number;
  /** Maximum allowed year. Defaults to 9999 */
  maxYear?: number;
}

Date Format Patterns:

The dateFormat string supports these components:

  • mm - Month (01-12)
  • dd - Day (01-31, validated against month)
  • yy - Two-digit year (00-99)
  • yyyy - Four-digit year
  • HH - Hours (00-23)
  • MM - Minutes (00-59)
  • SS - Seconds (00-59)

Pipe Return Type:

interface DatePipeResult {
  /** Corrected date value */
  value: string;
  /** Array of character positions that were auto-corrected */
  indexesOfPipedChars: number[];
}

/** 
 * Date correction pipe function
 * @param {string} conformedValue - Date value after mask is applied
 * @returns {DatePipeResult | false} Corrected result or false if invalid
 */
type DatePipeFunction = (conformedValue: string) => DatePipeResult | false;

Usage Examples:

// Basic date pipe
const datePipe = createAutoCorrectedDatePipe('mm/dd/yyyy');

// Custom format with time
const dateTimePipe = createAutoCorrectedDatePipe('dd.mm.yyyy HH:MM');

// Limited year range
const modernDatePipe = createAutoCorrectedDatePipe('mm/dd/yyyy', {
  minYear: 1900,
  maxYear: 2030
});

// European format
const europeanDatePipe = createAutoCorrectedDatePipe('dd/mm/yyyy');

Auto-Correction Behavior:

  • Single-digit optimization: Typing "4" in month position becomes "04"
  • Invalid date prevention: Prevents "32/13/2023" or "31/02/2023"
  • Smart year handling: Validates years within the specified range
  • Component validation: Each date/time component is validated against realistic limits
  • Caret positioning: Provides information about auto-corrected positions for UI feedback

Important Note:

For createAutoCorrectedDatePipe to work properly, the Text Mask component must be configured with keepCharPositions: true.

Types and Constants

/** RegExp for matching single digits (/\d/) - used internally by mask functions */
const digitRegExp: RegExp;

/** Special character for caret positioning in masks ('[]') - used internally by mask functions */
const caretTrap: string;

/** 
 * Mask function type - takes raw input and returns formatting mask
 * @param {string} rawValue - Current input value
 * @returns {Array<string | RegExp>} Array of strings and RegExp patterns for masking
 */
type MaskFunction = (rawValue: string) => Array<string | RegExp>;

/**
 * Pipe function type - takes formatted value and returns corrected value
 * @param {string} conformedValue - Value after mask is applied
 * @param {PipeConfig} config - Text Mask configuration
 * @returns {string | false} Corrected value or false to reject input
 */  
type PipeFunction = (conformedValue: string, config: PipeConfig) => string | false;