Class: Snowflake
@sapphire/snowflake.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
Name | Type | Description |
---|---|---|
epoch | number | bigint | Date | the epoch to use |
Defined in
Properties
[EpochSymbol]
• Private
Readonly
[EpochSymbol]: bigint
Internal reference of the epoch passed in the constructor
Defined in
[IncrementSymbol]
• Private
[IncrementSymbol]: bigint
Internal incrementor for generating snowflakes
Defined in
[ProcessIdSymbol]
• Private
[ProcessIdSymbol]: bigint
The process ID that will be used by default in the generate method
Defined in
[WorkerIdSymbol]
• Private
[WorkerIdSymbol]: bigint
The worker ID that will be used by default in the generate method
Defined in
decode
• decode: (id
: string
| bigint
) => DeconstructedSnowflake
Type declaration
▸ (id
): DeconstructedSnowflake
Alias for __type
Parameters
Name | Type |
---|---|
id | string | bigint |
Returns
Defined in
Accessors
epoch
• get
epoch(): bigint
The epoch for this snowflake
Returns
bigint
Defined in
processId
• get
processId(): bigint
Gets the configured process ID
Returns
bigint
Defined in
• set
processId(value
): void
Sets the process ID that will be used by default for the generate method
Parameters
Name | Type | Description |
---|---|---|
value | number | bigint | The new value, will be coerced to BigInt and masked with 0b11111n |
Returns
void
Defined in
workerId
• get
workerId(): bigint
Gets the configured worker ID
Returns
bigint
Defined in
• set
workerId(value
): void
Sets the worker ID that will be used by default for the generate method
Parameters
Name | Type | Description |
---|---|---|
value | number | bigint | The new value, will be coerced to BigInt and masked with 0b11111n |
Returns
void
Defined in
Methods
deconstruct
▸ deconstruct(id
): DeconstructedSnowflake
Deconstructs a snowflake given a snowflake ID
Parameters
Name | Type | Description |
---|---|---|
id | string | bigint | the snowflake to deconstruct |
Returns
a deconstructed snowflake
Example
const epoch = new Date('2000-01-01T00:00:00.000Z');
const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');
Defined in
generate
▸ generate(options?
): bigint
Generates a snowflake given an epoch and optionally a timestamp
Parameters
Name | Type | Description |
---|---|---|
options | SnowflakeGenerateOptions | options 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
Example
const epoch = new Date('2000-01-01T00:00:00.000Z');
const snowflake = new Snowflake(epoch).generate();
Defined in
timestampFrom
▸ timestampFrom(id
): number
Retrieves the timestamp field's value from a snowflake.
Parameters
Name | Type | Description |
---|---|---|
id | string | bigint | The snowflake to get the timestamp value from. |
Returns
number
The UNIX timestamp that is stored in id
.
Defined in
compare
▸ Static
compare(a
, b
): 0
| 1
| -1
Returns a number indicating whether a reference snowflake comes before, or after, or is same as the given snowflake in sort order.
Parameters
Name | Type | Description |
---|---|---|
a | string | bigint | The first snowflake to compare. |
b | string | bigint | The second snowflake to compare. |
Returns
0
| 1
| -1
-1
if a
is older than b
, 0
if a
and b
are equals, 1
if a
is newer than b
.
Example
Sort snowflakes in ascending order
const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];
console.log(ids.sort((a, b) => Snowflake.compare(a, b)));
// → ['254360814063058944', '737141877803057244', '1056191128120082432'];
Example
Sort snowflakes in descending order
const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];
console.log(ids.sort((a, b) => -Snowflake.compare(a, b)));
// → ['1056191128120082432', '737141877803057244', '254360814063058944'];