@sodiumlabs/snowflake

Discord server npm version npm downloads Last commit

About

Utility package to generate and deconstruct snowflakes. Snowflakes are unique IDs sortable by time and containing their creation timestamp within themselves. Read more about snowflakes on Wikipedia.

Installation

Node.js 18 or newer is required.

npm install @sodiumlabs/snowflake

Usage

Using snowflakes

import { Snowflake } from "@sodiumlabs/snowflake";

const epoch = new Date("2020-01-01T00:00:00.000Z");

// Create an instance of Snowflake
const snowflake = new Snowflake(epoch);

// Generate a snowflake with the given epoch
const uniqueId = snowflake.generate();

Snowflakes with Discord

import { discordSnowflake } from "@sodiumlabs/snowflake";

// Extract only the timestamp from a snowflake
const timestamp = discordSnowflake.timestampFrom("716068012414730320");
// 1590794318060

// Deconstruct the snowflake
const data = discordSnowflake.deconstruct("716068012414730320");
// {
//   id: 716068012414730320n,
//   timestamp: 1590794318060n,
//   workerId: 0n,
//   processId: 0n,
//   increment: 80n,
//   epoch: 1420070400000n
// }

Links

Help

Need help with the module? Ask on our support server!

Credits

This project also partially contains code derived or copied from @sapphire/snowflake.