Giter Site home page Giter Site logo

nas5w / random-word-slugs Goto Github PK

View Code? Open in Web Editor NEW
56.0 2.0 11.0 132 KB

A handy utility to create those random word slugs (e.g., generous-pink-biscuit) you see all over the place.

Home Page: https://www.npmjs.com/package/random-word-slugs

License: MIT License

TypeScript 99.76% JavaScript 0.24%
random typescript javascript

random-word-slugs's Introduction

Random Word Slugs

A handy utility to create those random word slugs (e.g., generous-pink-biscuit) you see all over the place.

run Codecov Status


Installation

Install with npm

npm i random-word-slugs

Install with yarn

yarn add random-word-slugs

Usage

The random-word-slugs package can be used without any parameters and defaults to a three-word, kebab-cased slug. Currently, the default configuration has 30,021,543 unique slug combinations.

import { generateSlug } from "random-word-slugs";

const slug = generateSlug();
console.log(slug);
// "elegant-green-coat"

The generateSlug function takes up to two arguments. The first argument is the numberOfWords in the slug (defaulting to three) and the second argument is the package options. The following example makes use of both parameters and provides an option to title-case the output:

const slug = generateSlug(4, { format: "title" });
console.log(slug);
// "Elegant Happy Green Coat"

Available Options

The options object can have any partial set of the following key/value pairs:

{
  format: "kebab" | "camel" | "sentence" | "lower" | "title",
  partsOfSpeech: ("adjective" | "noun")[],
  categories: {
    adjective: ("color" | "appearance" | etc...)[],
    noun: ("people" | "animals" | etc...)[]
  }
}

Note that, if provided, partsOfSpeech must be an array the same length as the number of words you're requesting. If using Typescript, the compiler will check this for you.

An example of a completed options object might look like this for a three-word slug:

const options = {
  format: "camel",
  partsOfSpeech: ["adjective", "noun", "adjective"],
  categories: {
    adjective: ["color", "appearance"],
    noun: ["animals"],
  },
};

Based on these options, our output might look something like blueBearTall.

Typescript Support for Options

The package exposes a RandomWordOptions<N> type, with N being the number of words in the slug. If you want to use this type to specify an options object, it might look something like this (although a Partial options object is certainly allowed and probably more common):

import { RandomWordOptions } from "random-word-slugs";

const options: RandomWordOptions<3> = {
  format: "title",
  categories: {
    noun: ["animals", "place"],
    adjective: ["color", "personality"],
  },
  partsOfSpeech: ["adjective", "noun", "adjective"],
};

Importantly, the generic 3 here will enforce partsOfSpeech being a three-element tuple.

Categories

The categories option allows you to generate your random slug from a subset of categories. Perhaps you only want colorful animals! You can specify one or many categories for the adjectives and nouns that comprise your random slug. The following is a list of categories currently in the repository:

Adjective Categories:

  • appearance
  • color
  • condition
  • personality
  • quantity
  • shapes
  • size
  • sounds
  • taste
  • time
  • touch

Noun Categories:

  • animals
  • business
  • education
  • family
  • food
  • health
  • media
  • people
  • place
  • profession
  • religion
  • science
  • sports
  • technology
  • thing
  • time
  • transportation

Assessing the Combinatorics

When using the package, you might be curious about how many different slug combinations exist. The package exposes a function to help with this called totalUniqueSlugs. This function can be used without arguments and will assume a three-slug adjective-adjective-noun format:

import { totalUniqueSlugs } from "random-word-slugs";

const totalSlugs = totalUniqueSlugs();
console.log(totalSlugs);
// 100000

Note: The 100000 number shown here is just an example and not a representation of the total number of slugs in the package at any moment (that evolves as words are added).

You can also assess the combinatoric space if you have a different number of words, word ordering, or a subset of categories. In the following example, we'll assume a four-word slug, in the order adjective-noun-adjective-noun, with only color adjectives and animal nouns:

import { totalUniqueSlugs } from "random-word-slugs";

const totalSlugs = totalUniqueSlugs(4, {
  partsOfSpeech: ["adjective", "noun", "adjective", "noun"],
  categories: {
    adjective: ["color"],
    noun: ["animals"],
  },
});
console.log(totalSlugs);
// 1000

Again, this 1000 is just an example. Importantly, this could help you determine that you're not comfortable with this limited combinatoric space and you can choose to add additional categories.

random-word-slugs's People

Contributors

a1nkur avatar cereal916 avatar deleplace avatar dependabot[bot] avatar nas5w avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

random-word-slugs's Issues

More words!

This library could always benefit from:

  • More words
  • Better categorization

Since one big selling point of the package is the large combination set, more words would be helpful!

Add custom lint rules

There are a few custom lint rules that would help ensure the integrity of the word list:

  • Make sure no words have special characters (-, spaces, *, etc)
  • Make sure no words repeat
  • Make sure word lists are alphabetical

Automate stat calculations and available categories in the readme file

Right now you manually have to run totalWordSlugs() and update the readme file directly when new words or categories are updated. This should be automated! Notional flow:

  • Have a readme template file with placeholders for stats and categories
  • Have a script that calculates the stats and overwrites the readme file
  • Execute that script on a pre-commit hook

Randomness

Hello, thank you for the nice library.

It seems that it is used in the wild to generate catchy identifiers e.g. "my-collection/bikes/elegant-green-coat" but also sometimes to generate easy to remember passwords. How exactly the library will be used is not fully under the control of the library author. However, there is an actionable mitigation to avoid to the users the "footgun" of inadvertently opening a security hole: use crypto.getRandomValues() (instead of Math.random()).

Would you like me to open a PR?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.