Skip to content

morrelinko/iguid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

IGUID

Incremental Global Unique Identifier

Installation

$ npm i iguid

Usage

const uid = require('iguid');

uid(); // 1530430595960
uid(); // 1530430595961
uid(); // 1530430595962


console.log(
  [...new Array(100)].map(() => ({ id: uid()}))
)

// [
//   { id: '1530430595963' },
//   { id: '1530430595964' },
//   .....
// ]

Customized Instance

By default iguid uses the the timestamp when the process was started as prefix. This is used to achieve some level of uniqueness when called from multiple node instances.

However you can spin-off a new instance of the generator passing a custom prefix.

const uid = require('iguid').newUp('c')

console.log(uid()) // c0
console.log(uid()) // c1
console.log(uid()) // c2
console.log(uid()) // c3

Pass a falsey value to remove prefix altogether

const uid = require('iguid').newUp(null)

console.log(uid()) // 0
console.log(uid()) // 1

NOTE:

Nothing really!

That's all folks :)

Credits