summaryrefslogtreecommitdiffstats
path: root/utils.mjs
diff options
context:
space:
mode:
authorBrian Cully <bjc@kublai.com>2021-01-03 18:34:19 -0500
committerBrian Cully <bjc@kublai.com>2021-02-06 10:39:32 -0500
commite92ad9f4b19a0670a80cdd293970c3a08c27a8b4 (patch)
treee8659dcdbf5f7ba3c55a118909d82dd8f0d0bcbd /utils.mjs
downloadmolsim-e92ad9f4b19a0670a80cdd293970c3a08c27a8b4.tar.gz
molsim-e92ad9f4b19a0670a80cdd293970c3a08c27a8b4.zip
Initial commit.
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 }