1 | import { IterableX } from './iterablex';
|
2 | export class SliceIterable extends IterableX {
|
3 | constructor(source, begin, end) {
|
4 | super();
|
5 | this._source = source;
|
6 | this._begin = begin;
|
7 | this._end = end;
|
8 | }
|
9 | *[Symbol.iterator]() {
|
10 | let it = this._source[Symbol.iterator](), begin = this._begin, next;
|
11 | while (begin > 0 && !(next = it.next()).done) {
|
12 | begin--;
|
13 | }
|
14 | let end = this._end;
|
15 | if (end > 0) {
|
16 | while (!(next = it.next()).done) {
|
17 | yield next.value;
|
18 | if (--end === 0) {
|
19 | break;
|
20 | }
|
21 | }
|
22 | }
|
23 | }
|
24 | }
|
25 | export function slice(source, begin, end = Infinity) {
|
26 | return new SliceIterable(source, begin, end);
|
27 | }
|
28 |
|
29 |
|