From dd96f62c7423c280748df23b9a17f742ff9588c0 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sun, 29 Sep 2024 19:00:40 -0300 Subject: sugar: findIndexOrEnd Unused, but a nice utility to have for some other time --- src/util/sugar.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src/util/sugar.js') 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 -- cgit 1.3.0-6-gf8a5