forked from nas5w/random-word-slugs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
121 lines (106 loc) · 2.95 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { getWordsByCategory, PartsOfSpeech, Categories } from "./words";
const DEFAULT_NUMBER_OF_WORDS = 3;
interface FixedLengthArray<T extends any, L extends number> extends Array<T> {
0: T;
length: L;
}
type Case = "kebab" | "camel" | "title" | "lower" | "sentence";
type Options<T, L extends number> = {
partsOfSpeech: FixedLengthArray<T, L>;
categories: Partial<
{
[K in PartsOfSpeech]: Categories[K][];
}
>;
format: Case;
};
export type RandomWordOptions<N extends number> = Partial<
Options<PartsOfSpeech, N>
>;
export function generateSlug<N extends number>(
numberOfWords?: N,
options?: Partial<Options<PartsOfSpeech, N>>
) {
const numWords = numberOfWords || DEFAULT_NUMBER_OF_WORDS;
const defaultOptions: Options<PartsOfSpeech, typeof numWords> = {
partsOfSpeech: getDefaultPartsOfSpeech(numWords),
categories: {},
format: "kebab",
};
const opts: Options<PartsOfSpeech, typeof numWords> = {
...defaultOptions,
...options,
};
const words = [];
for (let i = 0; i < numWords; i++) {
const partOfSpeech = opts.partsOfSpeech[i];
const candidates = getWordsByCategory(
opts.partsOfSpeech[i],
opts.categories[partOfSpeech]
);
const rand = candidates[Math.floor(Math.random() * candidates.length)];
words.push(rand);
}
return formatter(words, opts.format);
}
function getDefaultPartsOfSpeech<N extends number>(length: N) {
const partsOfSpeech = [];
for (let i = 0; i < length - 1; i++) {
partsOfSpeech.push("adjective");
}
partsOfSpeech.push("noun");
return partsOfSpeech as FixedLengthArray<PartsOfSpeech, N>;
}
function formatter(arr: string[], format: Case) {
if (format === "kebab") {
return arr.join("-").toLowerCase();
}
if (format === "camel") {
return arr
.map((el, i) => {
if (i === 0) return el.toLowerCase();
return el[0].toUpperCase() + el.slice(1).toLowerCase();
})
.join("");
}
if (format === "lower") {
return arr.join(" ").toLowerCase();
}
if (format === "sentence") {
return arr
.map((el, i) => {
if (i === 0) {
return el[0].toUpperCase() + el.slice(1).toLowerCase();
}
return el;
})
.join(" ");
}
return arr
.map((el) => {
return el[0].toUpperCase() + el.slice(1).toLowerCase();
})
.join(" ");
}
export function totalUniqueSlugs<N extends number>(
numberOfWords?: N,
options?: RandomWordOptions<N>
) {
const numAdjectives = getWordsByCategory(
"adjective",
options?.categories?.adjective
).length;
const numNouns = getWordsByCategory("noun", options?.categories?.noun).length;
const nums = {
adjective: numAdjectives,
noun: numNouns,
};
const numWords = numberOfWords || DEFAULT_NUMBER_OF_WORDS;
const partsOfSpeech =
options?.partsOfSpeech || getDefaultPartsOfSpeech(numWords);
let combos = 1;
for (let i = 0; i < numWords; i++) {
combos *= nums[partsOfSpeech[i]];
}
return combos;
}