-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathskip.ts
42 lines (39 loc) · 1.01 KB
/
skip.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
35
36
37
38
39
40
41
42
/*
* @author electricessence / https://github.com/electricessence/
* @license MIT
*/
import empty from '../iterables/empty';
import {IterableFilter} from '../IterableTransform';
import same from './same';
/**
* An iterable filter that bypasses a specified number of elements in a sequence and then returns the remaining elements.
* @param {number} count
* @return {IterableFilter<T>}
*/
export default function skip<T> (count: number): IterableFilter<T> {
if(isNaN(count) || count<=0) return same;
if(!isFinite(count)) return empty;
return function(sequence: Iterable<T>): Iterable<T> {
return {
* [Symbol.iterator] (): Iterator<T>
{
if(sequence instanceof Array)
{
const len = sequence.length;
for(let i = count; i<len; i++)
{
if(len!==sequence.length) throw Error('Array length changed during iteration.');
yield sequence[i];
}
return;
}
let remain = count;
for(const e of sequence)
{
if(0<remain--) continue;
yield e;
}
}
};
};
}