Skip to main content

Class: Snowflake

snowflake/src.Snowflake

A class for generating and deconstructing Twitter snowflakes.

A Twitter snowflake is a 64-bit unsigned integer with 4 fields that have a fixed epoch value.

If we have a snowflake 266241948824764416 we can represent it as binary:

64                                          22     17     12          0
000000111011000111100001101001000101000000 00001 00000 000000000000
number of ms since epoch worker pid increment

Constructors

constructor

new Snowflake(epoch)

Parameters

NameTypeDescription
epochnumber | bigint | Datethe epoch to use

Defined in

projects/utilities/packages/snowflake/src/lib/Snowflake.ts:39

Properties

#epoch

Private #epoch: bigint

Internal reference of the epoch passed in the constructor

internal

Defined in

projects/utilities/packages/snowflake/src/lib/Snowflake.ts:28


#increment

Private #increment: bigint

Internal incrementor for generating snowflakes

internal

Defined in

projects/utilities/packages/snowflake/src/lib/Snowflake.ts:22


decode

decode: (id: string | bigint) => DeconstructedSnowflake

Type declaration

▸ (id): DeconstructedSnowflake

Deconstructs a snowflake given a snowflake ID

example

const epoch = new Date('2000-01-01T00:00:00.000Z');
const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');
Parameters
NameTypeDescription
idstring | bigintthe snowflake to deconstruct
Returns

DeconstructedSnowflake

a deconstructed snowflake

Defined in

projects/utilities/packages/snowflake/src/lib/Snowflake.ts:34

Accessors

epoch

get epoch(): bigint

The epoch for this snowflake.

Returns

bigint

Defined in

projects/utilities/packages/snowflake/src/lib/Snowflake.ts:46

Methods

deconstruct

deconstruct(id): DeconstructedSnowflake

Deconstructs a snowflake given a snowflake ID

example

const epoch = new Date('2000-01-01T00:00:00.000Z');
const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');

Parameters

NameTypeDescription
idstring | bigintthe snowflake to deconstruct

Returns

DeconstructedSnowflake

a deconstructed snowflake

Defined in

projects/utilities/packages/snowflake/src/lib/Snowflake.ts:89


generate

generate(options?): bigint

Generates a snowflake given an epoch and optionally a timestamp

example

const epoch = new Date('2000-01-01T00:00:00.000Z');
const snowflake = new Snowflake(epoch).generate();

Parameters

NameTypeDescription
optionsSnowflakeGenerateOptionsoptions to pass into the generator, see SnowflakeGenerateOptions note when increment is not provided it defaults to the private increment of the instance

Returns

bigint

A unique snowflake

Defined in

projects/utilities/packages/snowflake/src/lib/Snowflake.ts:62


timestampFrom

timestampFrom(id): number

Retrieves the timestamp field's value from a snowflake.

Parameters

NameTypeDescription
idstring | bigintThe snowflake to get the timestamp value from.

Returns

number

The UNIX timestamp that is stored in id.

Defined in

projects/utilities/packages/snowflake/src/lib/Snowflake.ts:106