Skip to main content

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 integer millisecond timestamp.

Parameters:

  • id (string, optional): If provided, extract timestamp from this ID string.

Returns:

  • JavaScript: string (on generate) or number (on parse).
  • TypeScript: string (on generate) or number (on parse) with full type safety.
  • Python: str (on generate) or int (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:
    1. Time Component (First 8 characters): Derived from the current millisecond timestamp (Date.now() % 36**8).
    2. Random Component (Last 8 characters): Derived from cryptographically secure random bytes (crypto.randomBytes in JS/TS, os.urandom in Python).

Notes

  • Timezone: All timestamps are processed in UTC.
  • Random Quality: Uses cryptographic-grade random generators (crypto.randomBytes in JS/TS, os.urandom in 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.

  1. Fork the repository.
  2. Create a feature branch (git checkout -b feature/your-feature).
  3. Commit your changes (git commit -m 'Add your feature').
  4. Push to the branch (git push origin feature/your-feature).
  5. 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

lazyid-3.0.1.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lazyid-3.0.1-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

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

Hashes for lazyid-3.0.1.tar.gz
Algorithm Hash digest
SHA256 8a2425565b258d2b7c23014df1962144ce1659581bc38cad233a811bd90dd8fc
MD5 c27da5a772fb8ae08b5e5e3e115ab1fb
BLAKE2b-256 d987aab6a3c72a6529172756b0c6e0061581231726ee0c9123243a07133266b6

See more details on using hashes here.

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

Hashes for lazyid-3.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1ead77cecdacef7a4186b648de98136852d622d6f9eba8fb53e06d40b6c7aa83
MD5 cd1e2754fcf319d8cb7a6217215cee68
BLAKE2b-256 18ba2bb99039ccf92e2233e8c37ea7a572370d2f9de99978cce187709c623b3b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page