simple-snowflake
v1.0.2
Published
Yet another snowflake sequence id generator.
Downloads
4
Maintainers
Readme
Description
Yet another snowflake sequence id generator.
Inspired By
This moduled is inspired by:
- https://developer.twitter.com/en/docs/basics/twitter-ids
- https://www.callicoder.com/distributed-unique-id-sequence-number-generator/
- 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.