digital-mask
TypeScript icon, indicating that this package has built-in type declarations

1.1.7 • Public • Published

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, '___-___');
});

Package Sidebar

Install

npm i digital-mask

Weekly Downloads

5

Version

1.1.7

License

MIT

Unpacked Size

9.85 kB

Total Files

12

Last publish

Collaborators

  • neki-development