simple-snowflake

1.0.2 • Public • Published

Description

Yet another snowflake sequence id generator.

Inspired By

This moduled is inspired by:

  1. https://developer.twitter.com/en/docs/basics/twitter-ids
  2. https://www.callicoder.com/distributed-unique-id-sequence-number-generator/
  3. https://www.npmjs.com/package/flake-idgen

Why?

This moduled is implemented as a weekend project in order to understand the Twitter Snowflake ID generator. You can find various Snowflake implementation on the net, others were too complicated to understand, So I decided to write a simple and readble one.

npm install simple-snowflake
 

How to use

javascript

 
const SnowFlake = require('simple-snowflake');
 
const snowFlakeGen = new SnowFlake({ epoch: new Date().getTime(), machine: 15, worker: 10 });
 
const { binary, hex, err } = snowFlakeGen.next();
 
/*
  binary ==> {
    raw: '0000000000000000000000000000000000000000000111101010000000001010',
    splited: '0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0001 1110 1010 0000 0000 1010'
  }
  hex ==> 00000000001EA00A
*/
 

License

This project is licensed under the terms of the MIT license.

Package Sidebar

Install

npm i simple-snowflake

Weekly Downloads

2

Version

1.0.2

License

MIT

Unpacked Size

7.79 kB

Total Files

6

Last publish

Collaborators

  • saeed89