1.0.1 • Published 2 years ago

stamp-id v1.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
2 years ago

About

Library to generate and validate ID by pattern.

no dependencies

Install

npm i stamp-id

Examples

General usage

const stampId = require('stamp-id');

// generate
const id = stampId.generate();

// validate
const isValid = stampId.validate(id);

Generate examples

// 1
const id = stampId.generate('ddd-dd-dddd');
// --> "253-15-7920

// 2
stampId.setAlphabet('N', '23456789');
const id = stampId.generate('+1 Ndd-Ndd-dddd');
// --> "+1 517-724-3835"

// 3
stampId.setAlphabet('R', 'AAAAAAAAAB');
const id = stampId.generate('R');
// --> "A" (90% probability) or "B" (10% probability)

Built-in alphabets

'b': '01',
'd': '0123456789',
'h': '0123456789abcdef',
'H': '0123456789ABCDEF',
'c': 'abcdefghijklmnopqrstuvwxyz',
'C': 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'a': 'abcdefghijklmnopqrstuvwxyz0123456789',
'A': 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',
'z': 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',

Validate examples

// 1
const id = 'FRIDAY13';
const isValid = stampId.validate(id, 'CCCCCC13');
// --> true

// 1
stampId.setAlphabet('?', '-+./');
const id = 'a0+b1/z9.gg-zz';
const isValid = stampId.validate(id, 'aa?aa?aa?aa?aa');
// --> true

API

generate(pattern) ⇒ string

Generate new random id.

Kind: global function
Returns: string - Random id.

ParamTypeDefaultDescription
patternstring"zzzzzzzz"Text pattern of desired id.

validate(id, pattern, flags) ⇒ boolean

Validate id against pattern.

Kind: global function
Returns: boolean - True if the id could be generated with the pattern.

ParamTypeDefaultDescription
idstringId to test.
patternstring"zzzzzzzz"Text pattern.
flagsstringFlags string ("i" means ignore case).

setAlphabet(code, abc)

Add new or update existing alphabet.

Kind: global function

ParamTypeDescription
codestringOne character which will be used in patterns.
abcstringAlphabet chars.