diff options
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 |
commit | dd96f62c7423c280748df23b9a17f742ff9588c0 (patch) | |
tree | b2dfd7f8a043ba3a7574786d7bd651c61b9d2782 | |
parent | 1978f9a53b398b60bcffb582bf5b1183aff48bcc (diff) |
sugar: findIndexOrEnd
Unused, but a nice utility to have for some other time
-rw-r--r-- | src/util/sugar.js | 12 |
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 |