« get me outta code hell

sugar: findIndexOrEnd - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/util
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-09-29 19:00:40 -0300
committer(quasar) nebula <qznebula@protonmail.com>2024-09-29 19:01:08 -0300
commitdd96f62c7423c280748df23b9a17f742ff9588c0 (patch)
treeb2dfd7f8a043ba3a7574786d7bd651c61b9d2782 /src/util
parent1978f9a53b398b60bcffb582bf5b1183aff48bcc (diff)
sugar: findIndexOrEnd
Unused, but a nice utility to have for some other time
Diffstat (limited to 'src/util')
-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