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

id-tools

v1.0.4

Published

A comprehensive library for generating and validating unique IDs in JavaScript/TypeScript.

Downloads

29

Readme

id-tools

A comprehensive library for generating and validating unique IDs in JavaScript/TypeScript. Supports various ID formats including UUID, ULID, and MongoDB ObjectIds.

Installation

npm install id-tools

Features

  • Generate random numbers and strings
  • Create custom-formatted IDs
  • Generate ULIDs (Universally Unique Lexicographically Sortable Identifier)
  • Generate MongoDB ObjectIds
  • Validate various ID formats
  • TypeScript support included

Usage

Random Numbers

import { randomNumber } from 'id-tools';

// Generate a random number between 1 and 100
const num = randomNumber(1, 100);

Random Strings and IDs

import { 
  randomLetter, 
  randomSpecialChar, 
  idByFormat,
  randomString,
  randomStringFromSet,
  randomStringFromSetAsync,
  randomStringFromSetSync
} from 'id-tools';

// Generate a random letter (lowercase by default)
const letter = randomLetter();

// Generate a random special character
const special = randomSpecialChar();

// Generate a random string with options
const str = randomString(10, {
  upper: true,      // Include uppercase letters
  numbers: true,    // Include numbers
  specials: true    // Include special characters
});

// Generate an ID using a custom format
// Format: 'x' = letter, 'n' = digit, 's' = special, others = literal
const id = idByFormat('XXX-###-!!', {
  upper: true,      // Use uppercase letters
  numbers: true,    // Include numbers
  specials: true,   // Include special characters
  unique: true      // Ensure all characters are unique
});

ULID Generation and Validation

import { 
  generateUlid, 
  decodeUlid, 
  isUlid, 
  ulidTime, 
  ulidTimestamp,
  ulidToUuid,
  uuidToUlid
} from 'id-tools';

// Generate a ULID
const ulid = generateUlid();

// Check if a string is a valid ULID
const isValid = isUlid('01H9Z7K3Y5FJXG2N4V6D8T0W1');

// Decode a ULID to get timestamp and random parts
const decoded = decodeUlid('01H9Z7K3Y5FJXG2N4V6D8T0W1');
// Returns: { timestamp: 1681234567890, random: '5FJXG2N4V6D8T0W1' }

// Convert between ULID and UUID
const uuid = ulidToUuid(ulid);
const newUlid = uuidToUlid(uuid);

MongoDB ObjectId

import { 
  generateMongoId, 
  isMongoId,
  mongoIdTimestamp,
  mongoIdToUuid,
  uuidToMongoId
} from 'id-tools';

// Generate a new MongoDB ObjectId
const objectId = generateMongoId();

// Check if a string is a valid MongoDB ObjectId
const isValid = isMongoId('507f1f77bcf86cd799439011', {
  upper: false,             // Allow uppercase hex letters
  checkTimestamp: true,     // Validate timestamp range
  strictLength: true,       // Require exactly 24 characters
  checkRandomPart: true     // Ensure random part is not all zeros
});

// Get the timestamp from a MongoDB ObjectId
const timestamp = mongoIdTimestamp('507f1f77bcf86cd799439011');

// Convert between MongoDB ObjectId and UUID
const uuid = mongoIdToUuid(objectId);
const newObjectId = uuidToMongoId(uuid);

API Reference

Random Generation

  • randomNumber(min: number, max: number): number

    • Generate a random integer between min and max (inclusive)
  • randomLetter(upper: boolean = false): string

    • Generate a random letter (a-z or A-Z)
  • randomSpecialChar(): string

    • Generate a random special character from "!@#$%^&*()_+[]{}|;:',.<>?/"
  • randomString(length: number, options?: RandomStringOptions): string

    • Generate a random string with specified options
    • Options:
      • upper?: boolean - Include uppercase letters (default: false)
      • numbers?: boolean - Include numbers (default: false)
      • specials?: boolean - Include special characters (default: false)

ID Generation

  • idByFormat(format: string, options?: IdByFormatOptions): string
    • Generate an ID based on a format string
    • Format: 'x' = letter, 'n' = digit, 's' = special, others = literal
    • Options:
      • upper?: boolean - Use uppercase letters (default: false)
      • numbers?: boolean - Include numbers (default: true)
      • specials?: boolean - Include special characters (default: false)
      • unique?: boolean - Ensure all characters are unique (default: false)

ULID Functions

  • generateUlid(time?: number): string

    • Generate a ULID (time defaults to current time if not provided)
  • decodeUlid(ulid: string): { timestamp: number, random: string }

    • Decode a ULID into its timestamp and random parts
  • isUlid(value: string): boolean

    • Check if a string is a valid ULID
  • ulidTime(ulid: string): number

    • Get the timestamp from a ULID
  • ulidTimestamp(ulid: string): number

    • Alias for ulidTime
  • ulidToUuid(ulid: string): string

    • Convert a ULID to a UUID
  • uuidToUlid(uuid: string): string

    • Convert a UUID to a ULID

MongoDB ObjectId Functions

  • generateMongoId(time?: number): string

    • Generate a new MongoDB ObjectId
  • isMongoId(id: string, options?: IsMongoIDOptions): boolean

    • Check if a string is a valid MongoDB ObjectId
    • Options:
      • upper?: boolean - Allow uppercase hex letters (default: false)
      • checkTimestamp?: boolean - Validate timestamp range (default: false)
      • maxTimestamp?: number - Maximum allowed timestamp in seconds (default: now + 1 day)
      • minTimestamp?: number - Minimum allowed timestamp in seconds (default: 0)
      • strictLength?: boolean - Require exactly 24 characters (default: true)
      • checkRandomPart?: boolean - Ensure random part is not all zeros (default: false)
  • mongoIdTimestamp(id: string): number

    • Get the timestamp from a MongoDB ObjectId in milliseconds
  • mongoIdToUuid(id: string): string

    • Convert a MongoDB ObjectId to a UUID
  • uuidToMongoId(uuid: string): string

    • Convert a UUID to a MongoDB ObjectId

License

MIT