« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/util/sugar.js12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/util/sugar.js b/src/util/sugar.js
index 3c5c2e9a..3fcf9261 100644
--- a/src/util/sugar.js
+++ b/src/util/sugar.js
@@ -93,6 +93,18 @@ export function atOffset(array, index, offset, {
   return array[index + offset];
 }
 
+// Gets the index of the first item that satisfies the provided function,
+// or, if none does, returns the length of the array (the index just past the
+// final item).
+export function findIndexOrEnd(array, fn) {
+  const index = array.findIndex(fn);
+  if (index >= 0) {
+    return index;
+  } else {
+    return array.length;
+  }
+}
+
 // Sums the values in an array, optionally taking a function which maps each
 // item to a number (handy for accessing a certain property on an array of like
 // objects). This also coalesces null values to zero, so if the mapping function