slice
slice() function
Returns Iterable/AsyncIterable of the given elements from startIndex(inclusive) to endIndex(exclusive).
Signature:declare function slice<T>(start: number, iterable: Iterable<T>): IterableIterator<T>;
declare function slice<T>(start: number, iterable: AsyncIterable<T>): AsyncIterableIterator<T>;
declare function slice<T>(start: number, end: number, iterable: Iterable<T>): IterableIterator<T>;
declare function slice<T>(start: number, end: number, iterable: AsyncIterable<T>): AsyncIterableIterator<T>;
declare function slice<A extends Iterable<unknown> | AsyncIterable<unknown>>(start: number): (iterable: A) => ReturnIterableIteratorType<A>;
declare function slice<A extends Iterable<unknown> | AsyncIterable<unknown>>(start: number, end: number): (iterable: A) => ReturnIterableIteratorType<A>;
Example
const iter1 = slice(1, 3, ['a', 'b', 'c', 'd', 'e']);
iter1.next(); // {value: 'b', done: false};
iter1.next(); // {value: 'c', done: false};
iter1.next(); // {value: undefined, done: true};
const iter2 = slice(3, ['a', 'b', 'c', 'd', 'e']);
iter2.next(); // {value: 'd', done: false};
iter2.next(); // {value: 'e', done: false};
iter2.next(); // {value: undefined, done: true};
const iter3 = slice(3, "abcde");
iter3.next(); // {value: 'd', done: false};
iter3.next(); // {value: 'e', done: false};
iter3.next(); // {value: undefined, done: true};
// with pipe
pipe(
[1, 2, 3, 4, 5],
slice(3),
toArray,
); // [4, 5]
pipe(
[1, 2, 3, 4, 5],
slice(1, 3),
toArray,
); // [2, 3]