Skip to content

neki-dev/digital-mask

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

âš¡ Digital mask

Npm package version Small size Testing Building

Easy digital mask for string and inputs

.

  • Install

npm i digital-mask
  • Usage

/**
 * Return masked value from source string
 */
applyStringMask(
  // Unformatted string
  source: string,
  // Mask for format
  format: string,
  // Сhar from replace
  def: string = '_'
): string

/**
 * Update input value to masked
 */
applyInputMask(
  // Input
  input: HTMLInputElement,
  // Mask for format
  format: string,
  // Сhar from replace
  def: string = '_'
): void

/**
 * Update input value to masked on `input` event
 */
bindInputMask(
  // Input
  input: HTMLInputElement,
  // Mask for format
  format: string,
  // Сhar from replace
  def: string = '_'
): Function
  • Example for string

const { applyStringMask } = require('digital-mask');

const result1 = applyStringMask('1234', '___-___');
console.log(result1); // Output: 123-4__

const result2 = applyStringMask('1234', '***-***', '*');
console.log(result2); // Output: 123-4**

const result3 = applyStringMask('chars1234and56', '___-___');
console.log(result3); // Output: 123-456
  • Example for input

const { bindInputMask } = require('digital-mask');

const input = document.getElementById('inputPhone');
const unbind = bindInputMask(input, '___-___');
// ...
unbind();
const { applyInputMask } = require('digital-mask');

const input = document.getElementById('inputPhone');
input.addEventListener('input', () => {
  applyInputMask(input, '___-___');
});