UNPKG

froebel

Version:
30 lines (28 loc) 699 B
/** * 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 = (n, list) => [...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(n, list) { let i = 0; for (const el of list) { if (i++ >= n) return; yield el; } }