-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnth.ts
40 lines (37 loc) · 881 Bytes
/
nth.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
// credit: https://stackoverflow.com/a/20426113
const special = [
"zeroth",
"first",
"second",
"third",
"fourth",
"fifth",
"sixth",
"seventh",
"eighth",
"ninth",
"tenth",
"eleventh",
"twelfth",
"thirteenth",
"fourteenth",
"fifteenth",
"sixteenth",
"seventeenth",
"eighteenth",
"nineteenth",
];
const deca = ["twent", "thirt", "fort", "fift", "sixt", "sevent", "eight", "ninet"];
export const nthify = (n: number): string => {
if (n > 99) {
throw new Error("nthify only supports numbers up to 99");
}
if (n < 20) {
return special[n];
}
if (n % 10 == 0) {
return deca[Math.floor(n / 10) - 2] + "ieth";
}
return deca[Math.floor(n / 10) - 2] + "y-" + special[n % 10];
};
export const capitalize = (s: string) => s && s[0].toUpperCase() + s.slice(1);