snowflake-generator
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

snowflake-generator

NPM version NPM downloads Patreon

A lightweight Twitter Snowflake generation package utilising TypeScript.


Getting Started

Install the npm package

npm install snowflake-generator

Require the package into your code

const { Generator } = require('snowflake-generator');

Construct a new Snowflake Generator with the EPOCH timestamp in milliseconds

const SnowflakeGenerator = new Generator(1420070400000); 
// Thursday, 1 January 2015 00:00:00

Generate a Snowflake

const Snowflake = SnowflakeGenerator.generate();

Generator (class)

Constructor

new Generator(epoch?: Date|number, shardID?: number);
param type optional default description
epoch Date|number true 946684800000 The epoch timestamp to generate from.
shardID number true 1 Useful when running multiple generators at the same time to prevent duplicates.

Properties

.EPOCH

The generators epoch timestamp in milliseconds.
@type number

.SHARD_ID

The id of the shard running this generator.
@type number

.INCREMENT

The current increment iteration this generator is on.
@type number

Methods

Generates snowflakes.

Generator.generate(amount?: number, timestamp?: Date|number);
parameter type optional default description
amount number true 1 Amount of snowflakes to generate, recommended not to go above 1024 or duplicates will arise.
timestamp Date|number true Date.now Timestamp to generate from

@returns bigint|bigint[]

Deconstruct a snowflake to its values using the Generator.epoch.

Generator.deconstruct(snowflake: SnowflakeResolvable);
parameters type description
snowflake SnowflakeResolvable Snowflake(s) to deconstruct

@returns DeconstructedSnowflake


Types & Interfaces

SnowflakeResolvable

Resolvable value types for a valid Snowflake.

  • string
  • number
  • bigint

DeconstructedSnowflake

Interface of a Snowflake after Generator.deconstruct().

  • snowflake - Snowflake deconstructed from
    @type bigint
  • timestamp - The timestamp the snowflake was generated
    @type bigint
  • shard_id - The shard_id used when generating
    @type bigint
  • increment - The increment of this snowflake
    @type bigint
  • binary - The 64Bit snowflake binary string
    @type string

Extra Credit

Generating more than 1024 snowflakes

let snowflakes = [];
let ts = Date.now();
let amount = 20000;
for (let i = 0; i < (amount / 1024); i++) {
    // this could be improved, but is proof of concept.
    let new_amount = i + 1 >= (amount / 1024) ? amount % 1024 : 1024;
    snowflakes = snowflakes.concat(generator.generate(new_amount, ts + i));
}

Note: When parsing this array through a duplication checking function it returns 0 found duplicates.

> console.log(generator) after running script.
> Note: increment == amount.

Generator { epoch: 1420070400000, shard_id: 1, increment: 20000 }

Package Sidebar

Install

npm i snowflake-generator

Weekly Downloads

13

Version

1.0.2

License

MIT

Unpacked Size

16.9 kB

Total Files

6

Last publish

Collaborators

  • fataussiefatboy