Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add totalmax/min char count support #23

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,23 @@ console.log(totalSlugs);
```

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.


# Character Count Constraints

You can provide a `totalMax` and/or a `totalMin` to specify the number of characters for the generated slug:

```javascript
const shortSlug = generateSlug(2, {
categories: {
noun: ["animals"],
adjective: ["color"],
},
partsOfSpeech: ["adjective", "noun"],
charCount: {
totalMax: 15,
totalMin: 10,
},
});
// green-alligator
```
49 changes: 42 additions & 7 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ interface FixedLengthArray<T extends any, L extends number> extends Array<T> {

type Case = "kebab" | "camel" | "title" | "lower" | "sentence";

type CharCountOptions = {
totalMax?: number;
totalMin?: number;
};

type Options<T, L extends number> = {
partsOfSpeech: FixedLengthArray<T, L>;
categories: Partial<
{
[K in PartsOfSpeech]: Categories[K][];
}
>;
categories: Partial<{
[K in PartsOfSpeech]: Categories[K][];
}>;
format: Case;
charCount?: CharCountOptions;
};

export type RandomWordOptions<N extends number> = Partial<
Expand All @@ -32,13 +36,14 @@ export function generateSlug<N extends number>(
partsOfSpeech: getDefaultPartsOfSpeech(numWords),
categories: {},
format: "kebab",
charCount: {},
};
const opts: Options<PartsOfSpeech, typeof numWords> = {
...defaultOptions,
...options,
};

const words = [];
let words = [];

for (let i = 0; i < numWords; i++) {
const partOfSpeech = opts.partsOfSpeech[i];
Expand All @@ -50,7 +55,36 @@ export function generateSlug<N extends number>(
words.push(rand);
}

return formatter(words, opts.format);
let slug = formatter(words, opts.format);

if (
opts.charCount?.totalMax !== undefined &&
slug.length > opts.charCount.totalMax
) {
while (slug.length > opts.charCount.totalMax) {
words.pop();
slug = formatter(words, opts.format);
}
}
if (
opts.charCount?.totalMin !== undefined &&
slug.length < opts.charCount.totalMin
) {
while (slug.length < opts.charCount.totalMin) {
const partOfSpeech: PartsOfSpeech =
opts.partsOfSpeech[words.length % numWords];
const candidates: string[] = getWordsByCategory(
partOfSpeech,
opts.categories[partOfSpeech] as Categories[typeof partOfSpeech][]
);
const rand: string =
candidates[Math.floor(Math.random() * candidates.length)];
words.push(rand);
slug = formatter(words, opts.format);
}
}

return slug;
}

function getDefaultPartsOfSpeech<N extends number>(length: N) {
Expand Down Expand Up @@ -119,3 +153,4 @@ export function totalUniqueSlugs<N extends number>(
}
return combos;
}

4 changes: 2 additions & 2 deletions package-lock.json

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

33 changes: 33 additions & 0 deletions test/randomWordSlugs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,38 @@ describe("wordList", () => {
});
});

describe("generateSlug", () => {
it("respects totalMax character count", () => {
const maxChars = 15;
const slug = generateSlug(2, {
categories: {
noun: ["animals"],
adjective: ["color"],
},
partsOfSpeech: ["adjective", "noun"],
charCount: {
totalMax: maxChars,
},
});
expect(slug.length).toBeLessThanOrEqual(maxChars);
});

it("respects totalMin character count", () => {
const minChars = 10;
const slug = generateSlug(2, {
categories: {
noun: ["animals"],
adjective: ["color"],
},
partsOfSpeech: ["adjective", "noun"],
charCount: {
totalMin: minChars,
},
});
expect(slug.length).toBeGreaterThanOrEqual(minChars);
});
});

describe("generateSlug", () => {
test("generates three random kebab-cased words by default", () => {
const slug = generateSlug();
Expand Down Expand Up @@ -184,3 +216,4 @@ describe("totalUniqueSlugs", () => {
expect(num).toBe(actualTotal);
});
});