Generate names in sequence using defaults or a custom name generator function.
MIT License
Generator ids in sequence using defaults or a custom id generator function.
$ npm i id-gen --save
Create a new generator and use the defaults:
// require in the IdGenerator
var IdGenerator = require('id-gen');
// create a new instance of a generator
var generator = new IdGenerator();
// start using the generator to create
// default ids in sequence, e.g. 001, 002, 003, 004
var id = generator.next();
Create a generator with named groups
// require in the IdGenerator
var IdGenerator = require('id-gen');
// create a new instance of a generator
var generator = new IdGenerator(options);
// create some named groups
// use 2 digits instead of 3
generator.create('default', { digits: 2 });
// use 5 digits instead of 3 and add a prefix e.g. page-00001
generator.create('page', { digits: 5, prefix: 'page-' });
// start using the generator to create
// default ids in sequence, e.g. 01, 02, 03, 04
var id = generator.next();
// specify the named group to use the group options
// page ids in sequence, e.g. page-00001, page-00002, page-00003
var pageId = generator.next('pages');
Create an id generator using a custom generator function.
// require in the IdGenerator
var IdGenerator = require('id-gen');
// create custom generator that takes a group name and options
function generator (groupName, options) {
// just use an id value on the options if specified
options = options || {};
return options.id;
}
// create a new instance of a generator
var generator = new IdGenerator(generator);
// start using the generator to create
// default ids in sequence, e.g. 01, 02, 03, 04
var id = generator.next();
// specify the options with an id to return the actual id
// e.g. {id: 'foo'} => 'foo'
var id = generator.next({id: 'foo'});
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 IDsCreate 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 chainingGet 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 IDCopyright © 2015 Brian Woodward Released under the MIT license.
This file was generated by verb-cli on May 28, 2015.