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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@jcendal/objectid

v1.0.6

Published

Lightweight TypeScript library for generating MongoDB-compatible ObjectId strings in the browser. It produces 12-byte identifiers in both hexadecimal (24-character) and compact base64-like formats. Designed for frontend use, it requires no dependencies an

Downloads

62

Readme

ObjectIdGenerator

A lightweight ObjectId generator for frontend applications, designed to mimic MongoDB's 12-byte ObjectId format. This utility is implemented in TypeScript, has no external dependencies, and is fully compatible with modern browsers via the crypto.getRandomValues API.

✨ Features

  • Fully compatible with browser environments
  • Zero dependencies
  • Generates MongoDB-style 12-byte ObjectIds
  • Supports output in:
    • Hexadecimal format (24-character hex string)
    • Slim/Base64-like encoding (using customizable 64-character alphabet)
  • Allows optional timestamp injection for predictable ObjectId generation

📦 Installation

Run npm install @jcendal/objectid to add it to your project. Then import and use it as needed.

📚 Usage

Import the default hex-generator function or the full ObjectId class for more control

import objectIdHex from '@jcendal/objectid';
import { ObjectId } from '@jcendal/objectid';

Generate a standard hex string ObjectId

const id = objectIdHex();
console.log(id); // e.g. "64e6f4b83fb0f83c1b3726f0"

Or via the class:

const id = ObjectId.hex();
console.log(id); // e.g. "64e6f4b83fb0f83c1b3726f0"

Generate a slim string ObjectId (base64-like)

const slimId = ObjectId.slim();
console.log(slimId); // e.g. "-V9MYwMf_OUCoEBuZ1Yv"

Inject a specific timestamp (Unix time in seconds)

const ts = Math.floor(Date.now() / 1000) - 3600; // 1 hour ago
const oldHex = objectIdHex(ts);
const oldSlim = ObjectId.slim(ts);

Use a custom 64-character alphabet for slim encoding

const customAlphabet =
  'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
const customSlimId = ObjectId.slim(undefined, customAlphabet);

🛠 API

Methods

ObjectId.create(options?: { timestamp?: number }): Uint8Array

Generates a raw 12-byte ObjectId. If time is provided (Unix timestamp in seconds), it will be used as the creation time.

ObjectId.toHex(buffer: Uint8Array): string

Converts a 12-byte ObjectId into a 24-character hexadecimal string.

ObjectId.toSlim(buffer: Uint8Array, chars?: string): string

Encodes a 12-byte buffer into a slim (base64-like) string. Supply a 64-character chars alphabet to customize.

ObjectId.hex(timestamp?: number): string

Returns a 24-character hex string ObjectId. Optionally takes a Unix timestamp.

ObjectId.slim(timestamp?: number, chars?: string): string

Returns a slim-encoded ObjectId using the default or custom 64-character alphabet. Optionally takes a Unix timestamp.

⚙️ Implementation Details

The structure of the generated ObjectId mimics MongoDB’s format:

| 4 bytes timestamp | 3 bytes machine ID | 2 bytes process ID | 3 bytes counter |
  • Machine ID: Random 3 bytes per module load
  • Process ID: Simulated with 2 random bytes per instance
  • Counter: 3-byte integer initialized randomly and incremented per ObjectId

📄 License

MIT – Feel free to use, modify, and distribute.