-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
take_while.ts
34 lines (34 loc) · 1013 Bytes
/
take_while.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* Takes elements from the iterable while the predicate is true.
*
* Use {@linkcode https://jsr.io/@core/iterutil/doc/take/~/take take} to take a specific number of elements.
* Use {@linkcode https://jsr.io/@core/iterutil/doc/drop/~/drop drop} to drop a specific number of elements.
* Use {@linkcode https://jsr.io/@core/iterutil/doc/async/take-while/~/takeWhile takeWhile} to take elements asynchronously.
*
* @param iterable The iterable to take elements from.
* @param fn The predicate to take elements with.
* @returns The taken iterable.
*
* @example
* ```ts
* import { takeWhile } from "@core/iterutil/take-while";
*
* const iter = takeWhile(
* [1, 2, 3, 4, 5],
* (v) => v < 4,
* );
* console.log(Array.from(iter)); // [1, 2, 3]
* ```
*/
export function* takeWhile<T>(
iterable: Iterable<T>,
fn: (value: T, index: number) => boolean,
): Iterable<T> {
let index = 0;
for (const value of iterable) {
if (!fn(value, index++)) {
break;
}
yield value;
}
}