npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

snowflake-generator

v1.0.2

Published

A 64-bit Snowflake generator written in JavaScript

Downloads

164

Readme

snowflake-generator

A lightweight Twitter Snowflake generation package utilising TypeScript.


Getting Started

Install the npm package

npm install snowflake-generator

Require the package into your code

const { Generator } = require('snowflake-generator');

Construct a new Snowflake Generator with the EPOCH timestamp in milliseconds

const SnowflakeGenerator = new Generator(1420070400000); 
// Thursday, 1 January 2015 00:00:00

Generate a Snowflake

const Snowflake = SnowflakeGenerator.generate();

Generator (class)

Constructor

new Generator(epoch?: Date|number, shardID?: number);

| param | type | optional | default | description | | :------------- | :----------- | :------- | :----------- | :---------- | | epoch | Date|number | true | 946684800000 | The epoch timestamp to generate from. | shardID | number | true | 1 | Useful when running multiple generators at the same time to prevent duplicates.

Properties

.EPOCH

The generators epoch timestamp in milliseconds.@type number

.SHARD_ID

The id of the shard running this generator.@type number

.INCREMENT

The current increment iteration this generator is on.@type number

Methods

Generates snowflakes.

Generator.generate(amount?: number, timestamp?: Date|number);

| parameter | type | optional | default | description | | :-------- | :----------- | :------- | :------- | :---------- | | amount | number | true | 1 | Amount of snowflakes to generate, recommended not to go above 1024 or duplicates will arise. | timestamp | Date|number | true | Date.now | Timestamp to generate from

@returns bigint|bigint[]

Deconstruct a snowflake to its values using the Generator.epoch.

Generator.deconstruct(snowflake: SnowflakeResolvable);

| parameters | type | description | | :--------- | :------------- | :---------- | | snowflake | SnowflakeResolvable | Snowflake(s) to deconstruct

@returns DeconstructedSnowflake


Types & Interfaces

SnowflakeResolvable

Resolvable value types for a valid Snowflake.

  • string
  • number
  • bigint

DeconstructedSnowflake

Interface of a Snowflake after Generator.deconstruct().

  • snowflake - Snowflake deconstructed from@type bigint
  • timestamp - The timestamp the snowflake was generated@type bigint
  • shard_id - The shard_id used when generating@type bigint
  • increment - The increment of this snowflake@type bigint
  • binary - The 64Bit snowflake binary string@type string

Extra Credit

Generating more than 1024 snowflakes

let snowflakes = [];
let ts = Date.now();
let amount = 20000;
for (let i = 0; i < (amount / 1024); i++) {
    // this could be improved, but is proof of concept.
    let new_amount = i + 1 >= (amount / 1024) ? amount % 1024 : 1024;
    snowflakes = snowflakes.concat(generator.generate(new_amount, ts + i));
}

Note: When parsing this array through a duplication checking function it returns 0 found duplicates.

> console.log(generator) after running script.
> Note: increment == amount.

Generator { epoch: 1420070400000, shard_id: 1, increment: 20000 }