Xander1233 / SnowflakeGenerator

Twitter snowflake generator in pure javascript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SnowflakeGenerator

Twitter snowflake generator in pure javascript.

Tutorial

  1. Create a new SnowflakeGenerator object
  2. 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

Docs

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

Properties

.id

Get one new generated snowflake

Returns: string

Example:

const snowflake = snowflakeGenerator.id;

Methods

.next([amount])

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();

.generateId()

Generate a new snowflake as a bigint

Returns: bigint

Example:

const snowflake = snowflakeGenerator.generateId();

About

Twitter snowflake generator in pure javascript

License:MIT License


Languages

Language:TypeScript 51.1%Language:JavaScript 48.9%