3.5.5 โ€ข Published 5 months ago

@sapphire/snowflake v3.5.5

Weekly downloads
115
License
MIT
Repository
github
Last release
5 months ago

Sapphire Logo

@sapphire/snowflake

Deconstruct and generate snowflake IDs using BigInts.

GitHub codecov npm bundle size npm Depfu

Table of Contents

Description

There is often a need to get a unique ID for entities, be that for Discord messages/channels/servers, keys in a database or many other similar examples. There are many ways to get such a unique ID, and one of those is using a so-called "snowflake". You can read more about snowflake IDs in this Medium article.

Features

  • Written in TypeScript
  • Bundled with Rollup so it can be used in NodeJS and browsers
  • Offers CommonJS, ESM and UMD bundles
  • Offers predefined epochs for Discord and Twitter
  • Fully tested

Installation

yarn add @sapphire/snowflake
# npm install @sapphire/snowflake

Usage

Note: While this section uses require, the imports match 1:1 with ESM imports. For example const { Snowflake } = require('@sapphire/snowflake') equals import { Snowflake } from '@sapphire/snowflake'.

Constructing snowflakes

Snowflakes with custom epoch

// Import the Snowflake class
const { Snowflake } = require('@sapphire/snowflake');

// Define a custom epoch
const epoch = new Date('2000-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();

Snowflake with Discord epoch constant

// Import the Snowflake class
const { DiscordSnowflake } = require('@sapphire/snowflake');

// Create an instance of Snowflake
const discordSnowflake = new DiscordSnowflake();

// Generate a snowflake with Discord's epoch
const uniqueId = discordSnowflake.generate();

// Alternatively, you can use the method directly
const uniqueId = DiscordSnowflake.generate();

Snowflake with Twitter epoch constant

// Import the Snowflake class
const { TwitterSnowflake } = require('@sapphire/snowflake');

// Create an instance of Snowflake
const twitterSnowflake = new TwitterSnowflake();

// Generate a snowflake with Twitter's epoch
const uniqueId = twitterSnowflake.generate();

// Alternatively, you can use the method directly
const uniqueId = TwitterSnowflake.generate();

Deconstructing snowflakes

Snowflakes with custom epoch

// Import the Snowflake class
const { Snowflake } = require('@sapphire/snowflake');

// Define a custom epoch
const epoch = new Date('2000-01-01T00:00:00.000Z');

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

// Deconstruct a snowflake with the given epoch
const uniqueId = snowflake.deconstruct('3971046231244935168');

Snowflake with Discord epoch constant

// Import the Snowflake class
const { DiscordSnowflake } = require('@sapphire/snowflake');

// Create an instance of Snowflake
const discordSnowflake = new DiscordSnowflake();

// Deconstruct a snowflake with Discord's epoch
const uniqueId = discordSnowflake.deconstruct('3971046231244935168');

// Alternatively, you can use the method directly
const uniqueId = DiscordSnowflake.deconstruct('3971046231244935168');

Snowflake with Twitter epoch constant

// Import the Snowflake class
const { TwitterSnowflake } = require('@sapphire/snowflake');

// Create an instance of Snowflake
const twitterSnowflake = new TwitterSnowflake();

// Deconstruct a snowflake with Twitter's epoch
const uniqueId = twitterSnowflake.deconstruct('3971046231244935168');

// Alternatively, you can use the method directly
const uniqueId = TwitterSnowflake.deconstruct('3971046231244935168');

API Documentation

For the full API documentation please refer to the TypeDoc generated documentation.

Buy us some doughnuts

Sapphire Community is and always will be open source, even if we don't get donations. That being said, we know there are amazing people who may still want to donate just to show their appreciation. Thank you very much in advance!

We accept donations through Open Collective, Ko-fi, PayPal, Patreon and GitHub Sponsorships. You can use the buttons below to donate through your method of choice.

Donate WithAddress
Open CollectiveClick Here
Ko-fiClick Here
PatreonClick Here
PayPalClick Here

Contributors โœจ

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!

3.5.6-next.354bdda8

12 months ago

3.5.6-next.5d7bdbbc

10 months ago

3.5.6-next.15bf4d53

10 months ago

3.5.6-next.61eebdff

10 months ago

3.5.6-next.3d59d8ea

11 months ago

3.5.5

1 year ago

3.5.6-next.c73fb3fc

10 months ago

3.5.6-next.44fccf00

12 months ago

3.5.6-next.0f753b92

10 months ago

3.5.6-next.548200f1

10 months ago

3.5.6-next.ab1708e3

10 months ago

3.5.6-next.86690e35

11 months ago

3.5.6-next.2782a0d7

12 months ago

3.5.6-next.270038c3

11 months ago

3.5.6-next.ff2c792d

10 months ago

3.5.6-next.6e846dbf

12 months ago

3.5.6-next.068a617d

12 months ago

3.5.6-next.7f26c18f

11 months ago

3.5.6-next.42db0795

10 months ago

3.5.6-next.3b1a353c

11 months ago

3.5.6-next.8a922c42

10 months ago

3.5.6-next.51415597

10 months ago

3.5.6-next.598c24a3

10 months ago

3.5.6-next.318b047f

10 months ago

3.5.6-next.da31f25c

11 months ago

3.5.6-next.0e0a0033

12 months ago

3.5.6-next.4844417e

12 months ago

3.5.6-next.51274450

12 months ago

3.5.6-next.5da72061

10 months ago

3.5.6-next.156b9b1c

10 months ago

3.5.6-next.d5bed290

11 months ago

3.5.6-next.ec05fe56

10 months ago

3.5.6-next.80c79c3f

11 months ago

3.5.6-next.246fc0d6

10 months ago

3.5.4

1 year ago

3.5.3

2 years ago

3.5.2

2 years ago

3.5.1

3 years ago

3.5.0

3 years ago

3.4.3

3 years ago

3.4.2

3 years ago

3.4.1

3 years ago

3.4.0

3 years ago

3.3.0

3 years ago

3.2.2

4 years ago

3.2.1

4 years ago

3.2.0

4 years ago

3.1.0

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.1.4

4 years ago

2.1.3

4 years ago

2.1.2

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.0

4 years ago

1.3.6

4 years ago

1.3.5

4 years ago

1.3.4

4 years ago

1.3.3

4 years ago

1.3.2

4 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.8

5 years ago

1.2.7

5 years ago

1.2.6

5 years ago

1.2.5

5 years ago

1.2.4

5 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago