Snowflake ID

September 19, 2024 (2mo ago)

GitHub | npm version: 1.0.5

Snowflake ID Generator

A simple and customizable Snowflake ID generator for JavaScript and TypeScript projects, now with an additional random ID generation feature.

Installation

npm install @grkndev/snowflakeid

Usage

JavaScript

const { generateSnowflakeId, parseSnowflakeId, randomId } = require('@grkndev/snowflakeid');
 
// Generate a Snowflake ID with default options
const id = generateSnowflakeId();
console.log(id);
//=> '79796401721711616'
 
// Generate a Snowflake ID with custom options
const customId = generateSnowflakeId({ nodeId: 5, epoch: 1609459200000 });
console.log(customId);
 
// Parse a Snowflake ID
const parsedId = parseSnowflakeId(id);
console.log(parsedId);
 
// Generate a random ID
const randomID = randomId();
console.log(randomID);
//=> '283946'
 
// Generate a random ID with custom options
const customRandomID = randomId(8, { useChars: true, useNumbers: true });
console.log(customRandomID);
//=> 'a3Bf9x2Z'
 
// Generate a random UUID with default options
const uuId = uuid();
console.log(uuid)
//=> 'ABCD-EFG3-HJK8-BMS1
 
// Generate a random UUID with options
const uuId = uuid(split: 6);
console.log(uuid)
//=> 'ABCD-EFG3-HJK8-BMS1-KSJ2-128A
 

TypeScript

import { generateSnowflakeId, parseSnowflakeId, SnowflakeOptions, randomId, RandomIdOptions } from '@grkndev/snowflakeid';
 
// Generate a Snowflake ID with default options
const id = generateSnowflakeId();
console.log(id);
 
// Generate a Snowflake ID with custom options
const options: SnowflakeOptions = { nodeId: 5, epoch: 1609459200000 };
const customId = generateSnowflakeId(options);
console.log(customId);
 
// Parse a Snowflake ID
const parsedId = parseSnowflakeId(id);
console.log(parsedId);
 
// Generate a random ID with default options
const randomID = randomId();
console.log(randomID);
 
// Generate a random ID with custom options
const randomOptions: RandomIdOptions = { useChars: true, useNumbers: true };
const customRandomID = randomId(8, randomOptions);
console.log(customRandomID);
 
// Generate a random UUID
const uuId = uuid();
console.log(uuid)
 
// Generate a random UUID with options
const uuId = uuid(split: 6);
console.log(uuid)

API

generateSnowflakeId(options?: SnowflakeOptions): string

Generates a Snowflake ID.

Options:

Returns a string representation of the generated Snowflake ID.

parseSnowflakeId(id: string, epoch?: number): { timestamp: Date, nodeId: number, sequence: number }

Parses a Snowflake ID into its component parts.

Parameters:

Returns an object containing the parsed components of the Snowflake ID:

randomId(length?: number, options?: RandomIdOptions): string

Generates a random ID.

Parameters:

Returns a string representation of the generated random ID.

Error Handling

The generateSnowflakeId function may throw errors in the following cases:

The randomId function may throw an error if neither useChars nor useNumbers is set to true in the options.

License

MIT