Twitter snowflake generator in pure javascript.
- Create a new
SnowflakeGenerator
object - Call
id
to generate a new id
const snowflakeGenerator = new SnowflakeGenerator();
snowflakeGenerator.id; // New generated snowflake
snowflakeGenerator.id; // New generated snowflake. Different to the snowflake that got generated before
Key | Type | Required | Default | Description |
---|---|---|---|---|
epoch | bigint | no | 1609459201000n | The epoch that indicates the start of the snowflakes timestamp |
worker | bigint | no | 1n | The worker's id. The ID of the datacenter or server can be used as well |
process | bigint | no | 1n | The process id. |
accumulator | bigint | no | 0n | The increment id or accumulator that indicates the snowflake's id from their respectiv process, worker and epoch |
Get one new generated snowflake
Returns: string
Example:
const snowflake = snowflakeGenerator.id;
Generate 1 or more snowflakes
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
amount | number | no | 1 | The amount of snowflakes you want to generate |
Returns: Array<string> | string
Example:
const snowflake = snowflakeGenerator.next();
Generate a new snowflake as a bigint
Returns: bigint
Example:
const snowflake = snowflakeGenerator.generateId();