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 🙏

© 2024 – Pkg Stats / Ryan Hefner

custom-uuid

v3.1.1

Published

Generate string UUIDs with custom formats.

Downloads

834

Readme

custom-uuid

Package Version Dependencies Requires Node

Tests Test Coverage Maintainability Score

Generate custom and cryptographically-secure string UUIDs (universally unique identifiers).

custom-uuid is a lightweight package with zero dependencies. To generate string UUIDs it uses the crypto module that comes pre-installed in Node.js.

This package should be used only when you need to use strings as identifiers. Some examples of use cases are:

  • keys for a key-value DB
  • unique file identifiers
  • user-facing identifiers
  • or generally any use case where you need a unique string composed of random characters, and not a random pile of bits

You can simply create a new string UUID by specifying the dictionary of characters and the length of the string to be generated.

Quickstart

To create a custom UUID...

1. Install

npm install custom-uuid

2. Create a custom UUID (or use a predefined UUID)

ES6 module syntax:

import { generateCustomUuid, generateShortUuid, generateStrongCompactUuid } from "custom-uuid";
generateCustomUuid("123456789ABC", 20); // ⇨ 'C12B1B2A9382A488B43A'
generateShortUuid(); // ⇨ 'yT1xoeCt6fvdDf6a'
generateStrongCompactUuid(); // ⇨ 'BYFGhRjnn83hHCarT09H'

CommonJS syntax:

const { generateCustomUuid, generateShortUuid, generateStrongCompactUuid } = require('custom-uuid');
generateCustomUuid("123456789ABC", 20); // ⇨ 'B5B6699247862A569998'
generateShortUuid(); // ⇨ 'DMDvkPec8QUyV9O1'
generateStrongCompactUuid(); // ⇨ 'xRC4JggRQQFdPwn6MhZs'

3. Make sure the custom UUID is unique enough for your use case

With the custom UUID example we are using a dictionary with 12 different characters and a length of 20. Therefore, the total number of unique identifiers is 12^20 = 3.83e+21.
A large pool of identifiers makes it very unlikely to create duplicates.

You can then also calculate the average number of UUIDs to be generated before having the first collision as sqrt(pi*0.5*(number_of_identifers)).
Using the dictionary and the length of our example we get sqrt(pi*0.5*(12^20)) = 7.76e+10. So, in this example, on average, we have to create 77 billion identifiers before having the first collision.

Or you can use one of the pre-defined UUIDs that have strong guarantees of uniqueness.

Pre-defined UUIDs

| Method | Length | Example output | |-----------------------------------------------------------------------------------------------|--------|----------------------------------------| | generateShortUuid() | 16 | 14usBY8xSYXGPvsA | | generateStrongCompactUuid() | 20 | 6ptGBhTKkxTMCMEiiHiw | | generateShortLowercaseUuid() | 20 | 15amp61jbnu6dzmhxa0i | | generateLongLowercaseUuid() | 36 | e3703960-ca2d-4802-b426-88467e0e9b98 | | generateProfanitySafeUuid() | 20 | 4a8g6z1w7d1a8d1o9o3o |

generateShortUuid()

Generates a short cryptographically-strong random UUID using numbers and letters.
Use this identifier when you need a strong but very short universally unique identifier.

  • Length: 16
  • Dictionary: 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
  • Example output: 14usBY8xSYXGPvsA
  • Average UUIDs to be generated before having the first collision: 2.73e+14

generateStrongCompactUuid()

Generates a short cryptographically-strong random UUID using numbers and letters.
Use this identifier when you need a very strong but still compact universally unique id.

  • Length: 20
  • Dictionary: 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
  • Example output: 6ptGBhTKkxTMCMEiiHiw
  • Average UUIDs to be generated before having the first collision: 1.05e+18

generateShortLowercaseUuid()

Generates a short cryptographically-strong random UUID using numbers and lowercase letters.
Use this identifier when you need a strong but short universally unique id that does not contain uppercase letters.

  • Length: 20
  • Dictionary: 0123456789abcdefghijklmnopqrstuvwxyz
  • Example output: 15amp61jbnu6dzmhxa0i
  • Average UUIDs to be generated before having the first collision: 4.58e+15

generateLongLowercaseUuid(shouldRemoveHyphens: boolean = false)

Generates a long cryptographically-strong random UUID using the RFC 4122 version 4 UUID (128 bits in which 122 are random, converted to a hex string).
Use this identifier when you need a very strong universally unique id, but having a long string is not a problem.

  • Length: 36
  • Dictionary: 0123456789abcdef (bits are converted to hex)
  • Example output: e3703960-ca2d-4802-b426-88467e0e9b98
  • Average UUIDs to be generated before having the first collision: 2.88e+18

Note: generateLongLowercaseUuid() provides RFC4122 v4-compliant UUIDs by using crypto.randomUUID(), crypto.randomUUID() is only available in node >= 14.17.0.

generateProfanitySafeUuid(customLength: number = 20)

Generates a short cryptographically-strong random UUID by alternating a number with a letter, this alternation is done to avoid the creation of profanity inside the UUID (if we use letters it could create a bad word by chance).
Use this identifier when you need a universally unique id that is frequently presented to users or associated to users.

  • Length: 20
  • Dictionary: 123456789 + abcdefghijklmnopqrstuvwxyz
  • Example output: 4a8g6z1w7d1a8d1o9o3o
  • Average UUIDs to be generated before having the first collision: 8.79e+11

License

Apache Version 2.0

See LICENSE