Skip to content

Commit

Permalink
Add more words and automagically write stats to readme (#13)
Browse files Browse the repository at this point in the history
* Start adding additional words

* Add more words

* Test pre-commit

* Test lint-staged

* Test lint-staged

* Remove attempts at pre-commit hooks

* More words

* Update readme

* Fix package-lock
  • Loading branch information
nas5w authored Nov 7, 2021
1 parent 82468f5 commit 9d346cd
Show file tree
Hide file tree
Showing 6 changed files with 264 additions and 21 deletions.
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ 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 19,572,525 unique slug combinations**.
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**.

```javascript
import { generateSlug } from "random-word-slugs";
Expand Down Expand Up @@ -97,37 +97,37 @@ The `categories` option allows you to generate your random slug from a subset of

Adjective Categories:

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

Noun Categories:

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

# Assessing the Combinatorics

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "random-word-slugs",
"version": "0.1.5",
"version": "0.1.6",
"description": "A random word slug generator pre-loaded with many words",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"test": "jest --coverage",
"prepublish": "tsc"
"prepublish": "tsc",
"populate-readme": "npx ts-node ./scripts/populateReadme.ts"
},
"keywords": [
"random words",
Expand Down
136 changes: 136 additions & 0 deletions scripts/README_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# Random Word Slugs

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

[![Build Status](https://travis-ci.org/nas5w/random-word-slugs.svg?branch=master)](https://travis-ci.org/nas5w/random-word-slugs) [![Codecov Status](https://codecov.io/gh/nas5w/random-word-slugs/branch/master/graph/badge.svg)](https://codecov.io/gh/nas5w/random-word-slugs/branch/master)

<hr />

# Installation

Install with npm

```bash
npm i random-word-slugs
```

Install with yarn

```bash
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 {{uniqueCombinations}} unique slug combinations**.

```javascript
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:

```javascript
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:

```javascript
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):

```typescript
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:

{{adjectiveCategories}}

Noun Categories:

{{nounCategories}}

# 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:

```javascript
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:

```javascript
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.
32 changes: 32 additions & 0 deletions scripts/populateReadme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import fs from "fs";
import { totalUniqueSlugs } from "../index";
import { WordList, wordList } from "../words";

console.log("Adding stats to README");

// Populate slug count
const combos = totalUniqueSlugs().toLocaleString("en-US");
const readme = fs.readFileSync("./scripts/README_TEMPLATE.md", "utf-8");

// Populate categories
function listToUnique(list: WordList[keyof WordList]) {
const unique = new Set<string>();
list.forEach(({ categories }: WordList[keyof WordList][number]) => {
categories.forEach(
(category: WordList[keyof WordList][number]["categories"][number]) =>
unique.add(category)
);
});
return "- " + [...unique].sort().join("\n- ");
}

const adjectiveCategories = listToUnique(wordList.adjective);
const nounCategories = listToUnique(wordList.noun);

const replaced = readme
.replace("{{uniqueCombinations}}", combos)
.replace("{{adjectiveCategories}}", adjectiveCategories)
.replace("{{nounCategories}}", nounCategories);

// Write final README
fs.writeFileSync("./README.md", replaced);
78 changes: 76 additions & 2 deletions words.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,43 @@ export type PartsOfSpeech = keyof typeof wordList;
export const wordList = {
noun: [
{ word: "accountant", categories: ["profession"] },
{ word: "ability", categories: ["thing"] },
{ word: "accident", categories: ["thing"] },
{ word: "account", categories: ["thing"] },
{ word: "action", categories: ["thing"] },
{ word: "activity", categories: ["thing"] },
{ word: "actor", categories: ["profession"] },
{ word: "ad", categories: ["media"] },
{ word: "addition", categories: ["thing"] },
{ word: "address", categories: ["thing"] },
{ word: "adult", categories: ["people"] },
{ word: "advantage", categories: ["thing"] },
{ word: "advertisement", categories: ["media"] },
{ word: "afternoon", categories: ["time"] },
{ word: "airport", categories: ["transportation"] },
{ word: "agency", categories: ["thing"] },
{ word: "agent", categories: ["people"] },
{ word: "air", categories: ["thing"] },
{ word: "airline", categories: ["transportation"] },
{ word: "airplane", categories: ["transportation"] },
{ word: "airport", categories: ["transportation", "place"] },
{ word: "alarm", categories: ["thing"] },
{ word: "alligator", categories: ["animals"] },
{ word: "ambulance", categories: ["health"] },
{ word: "analyst", categories: ["profession"] },
{ word: "angle", categories: ["thing"] },
{ word: "animal", categories: ["animals"] },
{ word: "answer", categories: ["thing"] },
{ word: "apartment", categories: ["place"] },
{ word: "apple", categories: ["food"] },
{ word: "application", categories: ["thing"] },
{ word: "appointment", categories: ["thing"] },
{ word: "architect", categories: ["profession"] },
{ word: "argument", categories: ["thing"] },
{ word: "area", categories: ["thing"] },
{ word: "arm", categories: ["thing"] },
{ word: "army", categories: ["thing"] },
{ word: "art", categories: ["thing"] },
{ word: "article", categories: ["thing"] },
{ word: "artist", categories: ["profession"] },
{ word: "australia", categories: ["place"] },
{ word: "author", categories: ["profession"] },
Expand Down Expand Up @@ -185,7 +211,7 @@ export const wordList = {
{ word: "mother", categories: ["people", "family"] },
{ word: "motherboard", categories: ["thing", "technology"] },
{ word: "motorcycle", categories: ["transportation"] },
{ word: "mouse", categories: ["thing", "technology", "animal"] },
{ word: "mouse", categories: ["thing", "technology", "animals"] },
{ word: "musician", categories: ["profession"] },
{ word: "nail", categories: ["thing"] },
{ word: "napkin", categories: ["thing"] },
Expand Down Expand Up @@ -305,30 +331,78 @@ export const wordList = {
{ word: "zoo", categories: ["animals"] },
] as const,
adjective: [
{ word: "abandoned", categories: ["condition"] },
{ word: "abiding", categories: ["personality"] },
{ word: "able", categories: ["condition"] },
{ word: "abrasive", categories: ["condition", "personality"] },
{ word: "abnormal", categories: ["condition"] },
{ word: "absurd", categories: ["condition"] },
{ word: "abundant", categories: ["quantity"] },
{ word: "acceptable", categories: ["condition"] },
{ word: "acidic", categories: ["taste"] },
{ word: "acoustic", categories: ["sounds"] },
{ word: "acrid", categories: ["condition"] },
{ word: "adamant", categories: ["personality"] },
{ word: "adorable", categories: ["personality"] },
{ word: "adventurous", categories: ["personality"] },
{ word: "aggressive", categories: ["personality"] },
{ word: "agitated", categories: ["personality"] },
{ word: "agreeable", categories: ["personality"] },
{ word: "alert", categories: ["condition"] },
{ word: "alive", categories: ["condition"] },
{ word: "aloof", categories: ["personality"] },
{ word: "ambitious", categories: ["personality"] },
{ word: "ancient", categories: ["time"] },
{ word: "angry", categories: ["personality"] },
{ word: "annoyed", categories: ["personality"] },
{ word: "antsy", categories: ["personality"] },
{ word: "anxious", categories: ["personality"] },
{ word: "appalling", categories: ["personality"] },
{ word: "appetizing", categories: ["taste"] },
{ word: "apprehensive", categories: ["personality"] },
{ word: "arrogant", categories: ["personality"] },
{ word: "ashamed", categories: ["personality"] },
{ word: "astonishing", categories: ["personality"] },
{ word: "attractive", categories: ["appearance"] },
{ word: "average", categories: ["appearance"] },
{ word: "bad", categories: ["condition"] },
{ word: "bald", categories: ["appearance"] },
{ word: "bashful", categories: ["personality"] },
{ word: "beautiful", categories: ["appearance"] },
{ word: "beefy", categories: ["appearance"] },
{ word: "belligerent", categories: ["personality"] },
{ word: "bent", categories: ["condition"] },
{ word: "better", categories: ["condition"] },
{ word: "best", categories: ["condition"] },
{ word: "better", categories: ["condition"] },
{ word: "bewildered", categories: ["personality"] },
{ word: "big", categories: ["size"] },
{ word: "billions", categories: ["quantity"] },
{ word: "billowy", categories: ["appearance"] },
{ word: "bitter", categories: ["taste"] },
{ word: "black", categories: ["color"] },
{ word: "bland", categories: ["taste"] },
{ word: "blue", categories: ["color"] },
{ word: "blushing", categories: ["appearance"] },
{ word: "bored", categories: ["personality"] },
{ word: "boring", categories: ["personality"] },
{ word: "boundless", categories: ["personality"] },
{ word: "brainy", categories: ["personality"] },
{ word: "brash", categories: ["personality"] },
{ word: "brave", categories: ["personality"] },
{ word: "breezy", categories: ["touch"] },
{ word: "brief", categories: ["time"] },
{ word: "bright", categories: ["appearance"] },
{ word: "broad", categories: ["shapes"] },
{ word: "broken", categories: ["condition"] },
{ word: "brown", categories: ["color"] },
{ word: "bulky", categories: ["appearance"] },
{ word: "bumpy", categories: ["touch"] },
{ word: "burly", categories: ["appearance"] },
{ word: "busy", categories: ["personality"] },
{ word: "cagey", categories: ["personality"] },
{ word: "calm", categories: ["personality"] },
{ word: "callous", categories: ["personality"] },
{ word: "careful", categories: ["condition"] },
{ word: "chilly", categories: ["touch"] },
{ word: "chubby", categories: ["appearance"] },
Expand Down

0 comments on commit 9d346cd

Please sign in to comment.