stamp-id
v1.0.2
Published
Library to generate and validate ID by pattern
Downloads
478
Maintainers
Readme
About
Library to generate and validate ID by pattern.
Install
npm i stamp-id
Examples
General usage
import stampId from '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.
| Param | Type | Default | Description | | --- | --- | --- | --- | | [pattern] | string | "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.
| Param | Type | Default | Description | | --- | --- | --- | --- | | id | string | | Id to test. | | [pattern] | string | "zzzzzzzz" | Text pattern. | | [flags] | string | | Flags string ("i" means ignore case). |
setAlphabet(code, abc)
Add new or update existing alphabet.
Kind: global function
| Param | Type | Description | | --- | --- | --- | | code | string | One character which will be used in patterns. | | abc | string | Alphabet chars. |