Minimal 16-character URL-safe unique ID generator based on a millisecond timestamp and cryptographically secure random bits.
Project description
LazyId
Minimal 16-character URL-safe unique ID generator based on a millisecond timestamp and cryptographically secure random bits.
Overview
LazyId is a lightweight library for generating and parsing 16-character, URL-safe unique identifiers. Each ID consists of two parts: a time component derived from a millisecond timestamp (UTC) and a cryptographically secure random component, which are then encoded using Base36 (0-9, a-z).
No external dependencies. Available for JavaScript, TypeScript, and Python.
Features
- Compact: Generates 16-character IDs using a URL-safe Base36 alphabet.
- Timestamp-Based: The first 8 characters are derived from a millisecond timestamp (since the Unix epoch, UTC).
- Cryptographic Random: The last 8 characters are derived from secure random data.
- Cross-Platform: Available for JavaScript, TypeScript, and Python with identical output.
- Reversible: Extract the original millisecond timestamp from any ID.
- No Dependencies: Uses only standard libraries.
- Practically Collision-Free: Safe for use at a massive scale.
Installation
JavaScript
Install via npm:
npm install lazyid
TypeScript
Install via npm (scoped package):
npm install lazyid
Python
Install via pip:
pip install lazyid
Usage
JavaScript
// CommonJS
const lazyid = require("lazyid");
// ES Modules
import lazyid from "lazyid";
// Generate a new ID
let id = lazyid();
// -> Example: 'k2vtbmo2j5ah57z1'
// Extract the timestamp from an ID
let timestamp = lazyid(id);
// -> Example: 1728494747234 (this is an integer millisecond timestamp)
TypeScript
import lazyid from "lazyid";
// Generate a new ID
const id: string = lazyid();
// -> Example: 'k2vtbmo2j5ah57z1'
// Extract the timestamp from an ID
const timestamp: number = lazyid(id);
// -> Example: 1728494747234 (this is an integer millisecond timestamp)
Python
from lazyid import lazyid
# Generate a new ID
new_id = lazyid()
# -> Example: 'k2vtbmo2j5ah57z1'
# Extract the timestamp from an ID
timestamp = lazyid(new_id)
# -> Example: 1728494747234 (this is an integer millisecond timestamp)
API Reference
lazyid([id])
- Generate: Call with no arguments to create a new LazyId string.
- Parse: Call with an ID string to extract the original
integermillisecond timestamp.
Parameters:
id(string, optional): If provided, extract timestamp from this ID string.
Returns:
- JavaScript:
string(on generate) ornumber(on parse). - TypeScript:
string(on generate) ornumber(on parse) with full type safety. - Python:
str(on generate) orint(on parse).
Throws: Throws an error if the input string is invalid.
Structure of a LazyId
- Total Length: 16 characters.
- Encoding: Base36 (
0123456789abcdefghijklmnopqrstuvwxyz). - Composition: The ID is formed from the combination of two numerical components, encoded into Base36:
- Time Component (First 8 characters): Derived from the current millisecond timestamp (
Date.now() % 36**8). - Random Component (Last 8 characters): Derived from cryptographically secure random bytes (
crypto.randomBytesin JS/TS,os.urandomin Python).
- Time Component (First 8 characters): Derived from the current millisecond timestamp (
Notes
- Timezone: All timestamps are processed in UTC.
- Random Quality: Uses cryptographic-grade random generators (
crypto.randomBytesin JS/TS,os.urandomin Python) with no fallback. - Interoperability: IDs generated in any language can be correctly parsed in any other supported language.
Collision Resistance
By combining a time component with millisecond precision and a random component with ~2.8 trillion (36^8) possibilities, LazyId collisions are practically impossible, even in distributed systems generating thousands of IDs per millisecond. It is safe for use as database primary keys, URL slugs, filenames, or event IDs.
Contributing
Contributions are welcome! Please submit issues or pull requests to the GitHub repository.
- Fork the repository.
- Create a feature branch (
git checkout -b feature/your-feature). - Commit your changes (
git commit -m 'Add your feature'). - Push to the branch (
git push origin feature/your-feature). - Open a Pull Request.
Issues
Report bugs or request features at https://github.com/niefdev/lazyid/issues.
License
MIT © niefdev
Author
Developed by niefdev
Repository
Source code: https://github.com/niefdev/lazyid
Homepage
Learn more: https://github.com/niefdev/lazyid#readme
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file lazyid-3.0.1.tar.gz.
File metadata
- Download URL: lazyid-3.0.1.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8a2425565b258d2b7c23014df1962144ce1659581bc38cad233a811bd90dd8fc
|
|
| MD5 |
c27da5a772fb8ae08b5e5e3e115ab1fb
|
|
| BLAKE2b-256 |
d987aab6a3c72a6529172756b0c6e0061581231726ee0c9123243a07133266b6
|
File details
Details for the file lazyid-3.0.1-py3-none-any.whl.
File metadata
- Download URL: lazyid-3.0.1-py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1ead77cecdacef7a4186b648de98136852d622d6f9eba8fb53e06d40b6c7aa83
|
|
| MD5 |
cd1e2754fcf319d8cb7a6217215cee68
|
|
| BLAKE2b-256 |
18ba2bb99039ccf92e2233e8c37ea7a572370d2f9de99978cce187709c623b3b
|