Skip to main content

Built and signed on GitHub Actions

A random slug generator for typescript environments

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
4 months ago (0.1.2)

Random Slugs

There are some cases where it is convenient to have a human readable slug instead of some kind of unreadable id. The most annoying part is probably compiling that list of words, so here it is done for you.

API

Default usage will get you a three-word slug, separated by hyphens:

const slug = generateRandomSlug();
// 'permission-zodiac-guitar'

generateRandomSlug(generateSlugArguments)

generateSlugArguments

Optional object with the following values:

  • separator (optional): the string that will separate each word in the slug
    • type: string
    • default: -
  • wordList (optional): this allows you to override the default list with your own custom list
    • type: string[]
    • default: (view source)
  • wordQuantity (optional): this allows you to generate slugs of different lengths
    • type: number
    • default: 3
const customSlug = generateRandomSlug({
  separator: "_",
  wordList: ["foo", "bar", "baz"],
  wordQuantity: 2;
});
// 'baz_foo'

Details

The list is 682 words long. For three word slugs this results in 315,821,241 potential combinations.

As a bonus, this is 15 times faster (but less featured) than the most popular NPM package:

    CPU | Apple M1 Pro
Runtime | Deno 2.1.2 (aarch64-apple-darwin)

benchmark                 time/iter (avg)        iter/s      (min … max)           p75      p99     p995
------------------------- ----------------------------- --------------------- --------------------------
iwc/random-slug                  599.7 ns     1,668,000 (458.1 ns … 792.2 ns) 633.8 ns 792.2 ns 792.2 ns
nas5w/random-word-slugs            9.1 µs       109,600 (  8.2 µs … 262.5 µs)   8.7 µs  52.2 µs  62.8 µs

(thanks Deno bench)

Built and signed on
GitHub Actions
View transparency log

Add Package

deno add jsr:@iwc/random-slug

Import symbol

import * as random_slug from "@iwc/random-slug";

---- OR ----

Import directly with a jsr specifier

import * as random_slug from "jsr:@iwc/random-slug";

Add Package

npx jsr add @iwc/random-slug

Import symbol

import * as random_slug from "@iwc/random-slug";

Add Package

yarn dlx jsr add @iwc/random-slug

Import symbol

import * as random_slug from "@iwc/random-slug";

Add Package

pnpm dlx jsr add @iwc/random-slug

Import symbol

import * as random_slug from "@iwc/random-slug";

Add Package

bunx jsr add @iwc/random-slug

Import symbol

import * as random_slug from "@iwc/random-slug";