-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
79 lines (73 loc) · 1.95 KB
/
mod.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
import * as dictionary from "./words.ts";
import { v, type BaseSchema } from "./deps.ts";
const getRandomWord = (words: string[]): string => {
return words[Math.floor(Math.random() * words.length)];
};
export type NametagConfig = {
delimiter?: string;
words?: number;
categories?: ("animals" | "food" | "adjectives")[];
};
const CATEGORIES = ["adjectives", "animals", "food"];
const inputVal = v.optional(
v.object({
categories: v.optional(v.array(v.picklist(CATEGORIES)), CATEGORIES),
words: v.optional(v.number(), 3),
delimiter: v.optional(v.string(), "-"),
}),
{
categories: ["animals", "food", "adjectives"],
words: 3,
delimiter: "-",
}
) as BaseSchema<
NametagConfig,
{
delimiter: string;
categories: string[];
words: number;
}
>;
export const nametag = (config?: NametagConfig): string => {
const { words, categories, delimiter } = v.parse(inputVal, config);
const output = [];
for (let i = 1; i <= words ?? 3; i++) {
if (categories.includes("adjectives")) {
if (i === 1) {
output.push(getRandomWord(dictionary.adjectives));
} else if (i === words) {
const length = categories.filter((i) => i !== "adjectives").length;
output.push(
getRandomWord(
dictionary[
categories[
Math.floor(Math.random() * length)
] as keyof typeof dictionary
]
)
);
} else {
output.push(
getRandomWord(
dictionary[
categories[
Math.floor(Math.random() * categories.length)
] as keyof typeof dictionary
]
)
);
}
} else {
output.push(
getRandomWord(
dictionary[
categories[
Math.floor(Math.random() * categories.length)
] as keyof typeof dictionary
]
)
);
}
}
return output.join(delimiter);
};