deno.land / x / froebel@v0.23.2 / take.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
/** * Takes `n` elements from the iterable `list` and returns them as an array. * * @example * ``` * take(5, repeat(1, 2)) // -> [1, 2, 1, 2, 1] * take(3, [1, 2, 3, 4]) // -> [1, 2, 3] * take(3, [1, 2]) // -> [1, 2] * ``` */export const takeList = <T>(n: number, list: Iterable<T>): T[] => [ ...takeGenerator(n, list),];
/** * Takes `n` elements from the iterable `list` and returns them as a generator. * * @example * ``` * [...take(5, repeat(1, 2))] // -> [1, 2, 1, 2, 1] * [...take(3, [1, 2, 3, 4])] // -> [1, 2, 3] * [...take(3, [1, 2])] // -> [1, 2] * ``` */export function* takeGenerator<T>(n: number, list: Iterable<T>): Generator<T> { let i = 0; for (const el of list) { if (i++ >= n) return; yield el; }}
froebel

Version Info

Tagged at
a year ago