@codroid/snowflake
v1.0.0
Published
Lightweight Snowflake-style ID generator based on timestamp
Readme
Here’s a clean README.md suitable for publishing your Snowflake ID generator as an npm package:
# snowflakejs
Generate and parse Discord-style Snowflake IDs in JavaScript/TypeScript. This package allows you to create unique IDs based on timestamps, worker IDs, and sequences, as well as extract timestamps and convert them to local or UNIX time.
---
## Installation
```bash
npm install snowflakejs
# or
yarn add snowflakejsUsage
Import
// CommonJS
const { generateSnowflake, getTimestamp, snowflakeToLocalTime, snowflakeToUnix } = require('snowflakejs');
// ESM
import { generateSnowflake, getTimestamp, snowflakeToLocalTime, snowflakeToUnix } from 'snowflakejs';Generate a Snowflake ID
const id = generateSnowflake(); // Default workerId = 1
console.log(id); // e.g., "175928847299117063"You can also specify a worker ID (0–1023):
const id = generateSnowflake(42);Extract timestamp from a Snowflake
const date = getTimestamp(id);
console.log(date); // JavaScript Date objectConvert a Snowflake to local time string
const localTime = snowflakeToLocalTime(id);
console.log(localTime); // e.g., "9/22/2025, 10:30:15 PM"Convert a Snowflake to UNIX timestamp (seconds)
const unixTime = snowflakeToUnix(id);
console.log(unixTime); // e.g., 1695419415API
| Function | Parameters | Returns | Description |
| --------------------------------------------------- | ------------------------------ | -------- | -------------------------------------------------- |
| generateSnowflake(workerId?: number) | workerId (0–1023, default 1) | string | Generates a new Discord-style Snowflake ID. |
| getTimestamp(snowflake: string \| bigint) | snowflake | Date | Extracts the timestamp from a Snowflake. |
| snowflakeToLocalTime(snowflake: string \| bigint) | snowflake | string | Converts a Snowflake to a local time string. |
| snowflakeToUnix(snowflake: string \| bigint) | snowflake | number | Converts a Snowflake to UNIX timestamp in seconds. |
About
This library is inspired by Discord's Snowflake ID system. It provides a simple way to generate unique IDs with embedded timestamps, and allows you to convert them back into readable date formats.
License
MIT
I can also make a **shorter, copy-paste ready version** specifically optimized for npm with badges, keywords, and usage examples if you want.
Do you want me to create that version too?