npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@pedrobslisboa/js-string-mask

v0.2.0

Published

A light vanilla javascript string mask tool

Downloads

33

Readme

@pedrobslisboa/string-mask

A light weight string mask library for JavaScript.

Badges

/npm/v/express

Table of contents

Installation

npm install @pedrobslisboa/string-mask

# or

yarn add @pedrobslisboa/string-mask

Usage

import StringMask from '@pedrobslisboa/string-mask';

const phoneMaskedValues = new StringMask({mask: '+55 (00) 00000-0000');
phoneMaskedValues.apply('11999999999');

console.log(phoneMaskedValues.maskedValue); // +55 (11) 99999-9999
console.log(phoneMaskedValues.unmaskedValue); // 11999999999

// You can also pass a string with the mask
phoneMaskedValues.apply('+55 (11) 99999-9999');

console.log(phoneMaskedValues.maskedValue); // +55 (11) 99999-9999
console.log(phoneMaskedValues.unmaskedValue); // 11999999999

Tokens

| Token | Description | | ----- | --------------------- | | 0 | Int | | A | Letter | | S | Letter or Int | | * | Any char | | { | Start escape group | | } | End escape group | | [ | Start recursive group | | ] | End recursive group | | $ | Escape next char |

Recursive group

Use recursive groups to repeat a group of tokens; use this pattern at the end of the mask. Having a recursive group in the middle of the mask will result in a misbehavior.

Maybe in future versions this will be able to be used in the middle of the mask.

import StringMask from '@pedrobslisboa/string-mask';

const priceMask = new StringMask({ mask: '[.000],00', reverse: true });

// The mask will be applied from right to left
// the recursive group will be applied until the end of the string
// So, the result will be 1.234.567,89.
// Mapped to mask:      [   1][.234][.567],89
//                      [.000][.000][.000],00
priceMask.apply('123456789');

console.log(maskedPrice.maskedValue); // 1.234.567,89

Api

type MaskedValue = {
  maskedValue: string;
  unmaskedValue: string;
};

interface IStringMask {
  // The masked value
  readonly maskedValue: string;
  // The unmasked value
  readonly unmaskedValue: string;

  // The options used to create the mask
  readonly options: Options;

  // Apply the mask to a string
  apply(
    value: string,
    options?: {
      onProcessFixedChar?: () => void;
      onProcessInputChar?: () => void;
    },
  ): MaskedValue;

  // Change the options of the mask
  updateOptions(options: Options): MaskedValue;
}

Constructor

constructor(options?: Options);

| Option | Description | Default | | --------- | ------------------------------- | ------- | | mask | The mask to be applied | - | | reverse | Apply the mask in reverse order | false |

Examples

Phone mask

import StringMask from '@pedrobslisboa/string-mask';

const phoneMaskedValues = new StringMask({ mask: '+55 (00) 00000-0000' });
phoneMaskedValues.apply('11999999999');

console.log(phoneMaskedValues.maskedValue); // +55 (11) 99999-9999

CPF mask

import StringMask from '@pedrobslisboa/string-mask';

const cpfMaskedValues = new StringMask({ mask: '000.000.000-00' });
cpfMaskedValues.apply('12345678909');

console.log(cpfMaskedValues.maskedValue); // 123.456.789-09

Price mask

import StringMask from '@pedrobslisboa/string-mask';

const priceMask = new StringMask({ mask: '[.000],00', reverse: true });
priceMask.apply('123456789');

console.log(priceMask.maskedValue); // 1.234.567,89

Date mask

import StringMask from '@pedrobslisboa/string-mask';

const dateMask = new StringMask({ mask: '00/00/0000' });
dateMask.apply('01012021');

console.log(dateMask.maskedValue); // 01/01/2021

Credit card mask

import StringMask from '@pedrobslisboa/string-mask';

const creditCardMask = new StringMask({ mask: '0000 0000 0000 0000' });
creditCardMask.apply('1234567890123456');

console.log(creditCardMask.maskedValue); // 1234 5678 9012 3456

License

MIT License