From 3418e18492d814e832d63f20dc46ff6cb2ac816f Mon Sep 17 00:00:00 2001 From: Florrie Date: Wed, 13 Nov 2019 09:53:20 -0400 Subject: Initial commit --- upd8-util.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 upd8-util.js (limited to 'upd8-util.js') 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]}`; +}; -- cgit 1.3.0-6-gf8a5