« get me outta code hell

Initial commit - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/upd8-util.js
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2019-11-13 09:53:20 -0400
committerFlorrie <towerofnix@gmail.com>2019-11-13 09:53:20 -0400
commit3418e18492d814e832d63f20dc46ff6cb2ac816f (patch)
tree6ea34b5f3b2d44128e2cd19092e01883a05aa4d9 /upd8-util.js
Initial commit
Diffstat (limited to 'upd8-util.js')
-rw-r--r--upd8-util.js45
1 files changed, 45 insertions, 0 deletions
diff --git a/upd8-util.js b/upd8-util.js
new file mode 100644
index 0000000..fb2acb6
--- /dev/null
+++ b/upd8-util.js
@@ -0,0 +1,45 @@
+// This is used by upd8.js! It's part of the 8ackend. Read the notes there if
+// you're curious.
+//
+// Friendly(!) disclaimer: these utility functions haven't 8een tested all that
+// much. Do not assume it will do exactly what you want it to do in all cases.
+// It will likely only do exactly what I want it to, and only in the cases I
+// decided were relevant enough to 8other handling.
+
+'use strict';
+
+// Apparently JavaScript doesn't come with a function to split an array into
+// chunks! Weird. Anyway, this is an awesome place to use a generator, even
+// though we don't really make use of the 8enefits of generators any time we
+// actually use this. 8ut it's still awesome, 8ecause I say so.
+module.exports.splitArray = function*(array, fn) {
+    let lastIndex = 0;
+    while (lastIndex < array.length) {
+        let nextIndex = array.findIndex((item, index) => index >= lastIndex && fn(item));
+        if (nextIndex === -1) {
+            nextIndex = array.length;
+        }
+        yield array.slice(lastIndex, nextIndex);
+        // Plus one because we don't want to include the dividing line in the
+        // next array we yield.
+        lastIndex = nextIndex + 1;
+    }
+};
+
+// This function's name is a joke. Jokes! Hahahahahahahaha. Funny.
+module.exports.joinNoOxford = function(array) {
+    if (array.length === 0) {
+        // ????????
+        return '';
+    }
+
+    if (array.length === 1) {
+        return array[0];
+    }
+
+    if (array.length === 2) {
+        return `${array[0]} and ${array[1]}`;
+    }
+
+    return `${array.slice(0, -1).join(', ')} and ${array[array.length - 1]}`;
+};