summaryrefslogtreecommitdiffstats
path: root/utils.mjs
blob: 61e61b191e03cd5077efe1c33163c58451030429 (plain)
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
function randomItem(array) {
    return array[Math.floor(Math.random() * array.length)]
}

function ordinalSuffix(v) {
    let n = Number(v)
    if (n > 20) {
	n = n % 10
    }

    switch (n) {
    case 1:
	return 'st'
    case 2:
        return 'nd'
    case 3:
        return 'rd'
    default:
        return 'th'
    }
}

function testOrdinalSuffix() {
    const tests = {1: 'st',
                   2: 'nd',
                   3: 'rd',
                   4: 'th',
                   10: 'th',
                   11: 'th',
                   12: 'th',
                   13: 'th',
                   14: 'th',
                   20: 'th',
                   21: 'st',
                   32: 'nd',
                   43: 'rd',
                   44: 'th',
                   100: 'th',
                   101: 'st'}
    for (const t of Object.keys(tests)) {
        const got = ordinalSuffix(t)
        const want = tests[t]
        console.assert(got === want,
                       `bad suffix for ${t}: ${got} !== ${want}`)
    }
}

export { ordinalSuffix, testOrdinalSuffix, randomItem }