@fendy3002/id-generate
v0.0.4
Published
``` bash $ npm i --save @fendy3002/id-generate ```
Downloads
9
Readme
A tools helps to generate ID based on template or format given. Usually by adding year / month / date in it and random number. And also to generate sequential number divided by yearly / month / date.
Installation
$ npm i --save @fendy3002/id-generate
Generate by format
import {generate} from '@fendy3002/id-generate';
let id = await generate("INV/{{_date('YYYY/MM')}}/{{_randomNumber(6)}}");
// id = INV/2000/03/193820
Generate accept nunjucks
format as first parameter. The generate function also add some helpers that can be used by nunjucks
started with underscore:
_date
: acceptmoment
date format. It will return today's local date. ex:date('YYYY/MM/DD')
_randomNumber
: acceptnumber
, length of characters. ex:_randomNumber(8)
for 8-characters_uuid
: accept nothing. It will return randomly generated uuid
Additional parameters
Additional parameter can be used as id generation, by injecting it as second parameter. For example:
import {generate} from '@fendy3002/id-generate';
let id = await generate("INV/{{tag}}/{{_date('YYYY/MM')}}/{{_randomNumber(6)}}". {tag: "CUST"});
// id = INV/CUST/2000/03/193820
Generate partial
Useful for generating sequential id that can be divided by year / month / date / anything desired.
import {partialGenerate} from '@fendy3002/id-generate';
let id = await partialGenerate(
"INV/{{tag}}/{{_date('YYYY/MM')}}/{{_id(4)}}/X993",
async (payload) => {
// get from database based on format, or anything by desire
return 7; // return next id / to be inserted id
}, {
data: {
tag: "CUST"
}
});
// id = INV/CUST/2000/03/0007/X993
- First parameter is
nunjucks
format. It also has access to_date
and_randomNumber
. - Second parameter is next id handler. It accept a id format information and expect next (new id) to be generated as return
- Third optional parameter is option. The
data
property will be used as additional data to be rendered.
New id handle parameter
The payload passed into second argument (handler) has properties of:
prefix
: characters before idsuffix
: characters after idregex
: usable regex syntaxsql
: usable sql like wildcard syntax
For example, if the format is INV/{{tag}}/{{_date('YYYY/MM')}}/{{_id(4)}}/X993
, the payload is:
prefix
: INV/CUST/2019/10/suffix
: /X993regex
: INV/CUST/2019/10/(\d*)/X993sql
: INV/CUST/2019/10/%/X993