Skip to content

A random slug generator package for JSR / typescript environments

License

Notifications You must be signed in to change notification settings

xiwcx/random-slug

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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)

About

A random slug generator package for JSR / typescript environments

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published