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

1.0.2 • Public • Published

Snowflake Generator

Does what it says on the tin.

Usage

The function accepts an optional parameter which is a Unix timestamp (ms) to use as the epoch for the snowflake. This defaults to Discord's value (1420070400000).

import { generateSnowflake } from 'snowflake-gen';

generateSnowflake() // 1119762871952658432
generateSnowflake(0) // 1119781265757814784

How does it work?

Snowflakes are split into three sections:

  • A 41-bit Unix epoch
  • A 10-bit machine ID (we use randomness because I'm lazy)
  • A 12-bit sequence

This library uses bitshifting to construct the snowflake. See the JSDoc and source code for details.

Readme

Keywords

none

Package Sidebar

Install

npm i snowflake-gen

Weekly Downloads

3

Version

1.0.2

License

ISC

Unpacked Size

3.84 kB

Total Files

6

Last publish

Collaborators

  • not-nullptr