mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
683132f8d9
Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com> Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
44 lines
1.1 KiB
TypeScript
44 lines
1.1 KiB
TypeScript
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
// This module is browser compatible.
|
|
|
|
/**
|
|
* Returns all elements in the given collection until the first element that
|
|
* does not match the given predicate.
|
|
*
|
|
* @experimental **UNSTABLE**: New API, yet to be vetted.
|
|
*
|
|
* @typeParam T The type of the elements in the iterable.
|
|
*
|
|
* @param iterable The iterable to take elements from.
|
|
* @param predicate The predicate function to determine if an element should be
|
|
* included.
|
|
*
|
|
* @returns An array containing all elements until the first element that
|
|
* does not match the predicate.
|
|
*
|
|
* @example Basic usage
|
|
* ```ts
|
|
* import { takeWhile } from "@std/collections/take-while";
|
|
* import { assertEquals } from "@std/assert";
|
|
*
|
|
* const numbers = [1, 2, 3, 4, 5, 6];
|
|
*
|
|
* const result = takeWhile(numbers, (number) => number < 4);
|
|
*
|
|
* assertEquals(result, [1, 2, 3]);
|
|
* ```
|
|
*/
|
|
export function takeWhile<T>(
|
|
iterable: Iterable<T>,
|
|
predicate: (el: T) => boolean,
|
|
): T[] {
|
|
const result: T[] = [];
|
|
for (const element of iterable) {
|
|
if (!predicate(element)) {
|
|
break;
|
|
}
|
|
result.push(element);
|
|
}
|
|
return result;
|
|
}
|