1.0.2 • Published 7 years ago

@polygonal/seq-id v1.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
7 years ago

seq-id

types styled with prettier Travis Coveralls Dependencies All Contributors Donate

simple sequential ID generator. 747 bytes if minified.

Installation

Installation with npm

npm install @polygonal/seq-id

Features

Generated IDs:

  • may have a prefix and/or suffix
  • can be in base 2 to 36
  • can increment at specified steps
  • can restart at zero once going over a specified limit

Usage

Documentation

import seqIDGeneratorFactory from 'seq-id'

// configure your ID generator
const idGenerator = seqIDGeneratorFactory()
// produces IDs
const id0 = idGenerator() // => '0'
const id1 = idGenerator() // => '1'
const id2 = idGenerator() // => '2'

// configure your ID generator to use a prefix
const idGeneratorWithPrefix = seqIDGeneratorFactory('prefix-')
// produces IDs
const id0 = idGeneratorWithPrefix() // => 'prefix-0'
const id1 = idGeneratorWithPrefix() // => 'prefix-1'
const id2 = idGeneratorWithPrefix() // => 'prefix-2'

// configure your ID generator to use a Suffix
const idGeneratorWithSuffix = seqIDGeneratorFactory(null, '-suffix')
// produces IDs
const id0 = idGeneratorWithSuffix() // => '0-suffix'
const id1 = idGeneratorWithSuffix() // => '1-suffix'
const id2 = idGeneratorWithSuffix() // => '2-suffix'

// configure your ID generator to use a radix of 16
const idGeneratorWithBase16 = seqIDGeneratorFactory(null, null, 16)
// produces IDs
const id0 = idGeneratorWithBase16() // => '0'
const id1 = idGeneratorWithBase16() // => '1'
// ...
const id15 = idGeneratorWithBase16() // => 'f'
const id16 = idGeneratorWithBase16() // => '10'

// configure your ID generator to have a max value of 8
const idGeneratorWithMax8 = seqIDGeneratorFactory(null, null, 10, 8)
// produces IDs
const id0 = idGeneratorWithMax8() // => '0'
const id1 = idGeneratorWithMax8() // => '1'
// ...
const id7 = idGeneratorWithMax8() // => '7'
const id8 = idGeneratorWithMax8() // => '8'
const id9 = idGeneratorWithMax8() // => '0' NOTE: not unique!

// configure your ID generator to have a max value of 8 and throw and exception
// if asked to create an ID beyond 8
const idGeneratorWithMax8NoCycle = seqIDGeneratorFactory(null, null, 10, 8, 0, 1, false)
// produces IDs
const id0 = idGeneratorWithMax8NoCycle() // => '0'
const id1 = idGeneratorWithMax8NoCycle() // => '1'
// ...
const id7 = idGeneratorWithMax8NoCycle() // => '7'
const id8 = idGeneratorWithMax8NoCycle() // => '8'
const id9 = idGeneratorWithMax8NoCycle()
// Uncaught RangeError: sequential id is out of range (maximum value is 8)

// configure your ID generator to start at 4
const idGeneratorStartsAt4 = seqIDGeneratorFactory(null, null, 10, undefined, 4)
// produces IDs
const id0 = idGeneratorStartsAt4() // => '4'
const id1 = idGeneratorStartsAt4() // => '5'
const id2 = idGeneratorStartsAt4() // => '6

// configure your ID generator to increment by 2
const idGeneratorStep2 = seqIDGeneratorFactory(null, null, 10, undefined, 0, 2)
// produces IDs
const id0 = idGeneratorStep2() // => '0'
const id1 = idGeneratorStep2() // => '2'
const id2 = idGeneratorStep2() // => '4'

License

ISC

Credits

Made with :heart: by @PolygonalTech and all these wonderful contributors (emoji key):

Fernando G. Vilar💻 ⚠️ 📖

This project follows the all-contributors specification. Contributions of any kind are welcome!

Attribution

This package was made much easier to publish thanks to: typescript-library-starter, all-contributors-cli, weallcontribute and Contributor Covenant.

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago