summaryrefslogtreecommitdiffstats
path: root/utils.mjs
diff options
context:
space:
mode:
Diffstat (limited to 'utils.mjs')
-rw-r--r--utils.mjs48
1 files changed, 48 insertions, 0 deletions
diff --git a/utils.mjs b/utils.mjs
new file mode 100644
index 0000000..61e61b1
--- /dev/null
+++ b/utils.mjs
@@ -0,0 +1,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 }