From 0aed66f6ef5d026e01980957f5a27fb5dc98b041 Mon Sep 17 00:00:00 2001 From: Angus MacDonald Date: Fri, 27 Nov 2020 11:51:12 -0600 Subject: [PATCH] Add arrayf.filterUntilFalse --- src/arrayf/arrayf.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/arrayf/arrayf.ts b/src/arrayf/arrayf.ts index c53ee4696a..0b9d188044 100644 --- a/src/arrayf/arrayf.ts +++ b/src/arrayf/arrayf.ts @@ -28,5 +28,21 @@ export class arrayf { return output; } - -} \ No newline at end of file + /** + * Return every value before the first occurrence of a value that evaluates + * to false when passed to the conditionFn. + * ``` + * // Returns [1, 3, 7] + * arrayf.filterUntilFalse([1,3,7,40,2,6], (x) => x < 10); + * ``` + */ + static filterUntilFalse( + values: T[], conditionFn: (value: T, index: number) => boolean + ): T[] { + let index: number = 0; + while (index < values.length && conditionFn(values[index], index)) { + index++; + } + return values.slice(0, index); + } +}