0.2.0 • Published 5 years ago

incremental-js-identifier v0.2.0

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

incremental-js-identifier

NPM Version

Utility to create incremental JS identifiers

Installation

# npm
npm install incremental-js-identifier --save-dev

# yarn
yarn add incremental-js-identifier --dev

Usage

const idGenerator = require('incremental-js-identifier');
const nextID = idGenerator();

nextID(); // -> A
nextID(); // -> B
nextID(); // -> C
...
nextID(); // -> _
nextID(); // -> AA
nextID(); // -> BA
...
nextID(); // -> $_
nextID(); // -> __
nextID(); // -> A0

API

idGenerator(options) ⇒ function

Returns a function that will return a new, incrementing identifier each time it is called without arguments, or an identifier created from a number defined as the first parameter.

Will throw if passed a number that was already used.

The incremental mode will skip any number that was already created.

ParamTypeDescription
options.validFirstCharactersstringThe characters that are valid at the start of the identifier. Defaults to ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz$_.Must not contain duplicate characters.
options.validCharactersstringThe characters that are valid from the second position in the identifier. Defaults to ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz$_0123456789.Must not contain duplicate characters.

Example

const idGenerator = require('incremental-js-identifier');

const nextPrefixedID = idGenerator({validFirstCharacters: '_'});
nextPrefixedID(); // -> _A
nextPrefixedID(); // -> _B
nextPrefixedID(); // -> _C