id-gen
Generator ids in sequence using defaults or a custom id generator function.
$ npm i id-gen --save
Usage
Create a new generator and use the defaults:
// require in the IdGeneratorvar IdGenerator = ; // create a new instance of a generatorvar generator = ; // start using the generator to create// default ids in sequence, e.g. 001, 002, 003, 004var id = generatornext;
Create a generator with named groups
// require in the IdGeneratorvar IdGenerator = ; // create a new instance of a generatorvar generator = options; // create some named groups// use 2 digits instead of 3generator; // use 5 digits instead of 3 and add a prefix e.g. page-00001generator; // start using the generator to create// default ids in sequence, e.g. 01, 02, 03, 04var id = generatornext; // specify the named group to use the group options// page ids in sequence, e.g. page-00001, page-00002, page-00003var pageId = generatornext'pages';
Create an id generator using a custom generator function.
// require in the IdGeneratorvar IdGenerator = ; // create custom generator that takes a group name and options { // just use an id value on the options if specified options = options || {}; return optionsid;} // create a new instance of a generatorvar generator = generator; // start using the generator to create// default ids in sequence, e.g. 01, 02, 03, 04var id = generatornext; // specify the options with an id to return the actual id// e.g. {id: 'foo'} => 'foo'var id = generatornextid: 'foo';
API
IdGenerator
Create a new IdGenerator passing in an optional generator function that does the actual work.
Params
generator
{Function}: Optional generator function used to generate new IDs
.create
Create a new group to segment IDs
Params
groupName
{String}: Name of the group to create.options
{Object}: Additional options to define how the IDs are generated.returns
{Object}this
: to enable chaining
.next
Get the next ID by groupName
Params
groupName
{String}: Optional name of group to generate the ID for.options
{String}: Additional options to pass to the generatorcb
{Function}: Optional callback function to get the next id asyncreturns
{String}: Generated ID
Author
License
Copyright © 2015 Brian Woodward Released under the MIT license.
This file was generated by verb-cli on May 28, 2015.