1 | import { IterableX } from './iterablex';
|
2 | import { identity } from '../util/identity';
|
3 | import { returnIterator } from '../util/returniterator';
|
4 | export class ZipIterable extends IterableX {
|
5 | constructor(sources, fn) {
|
6 | super();
|
7 | this._sources = sources;
|
8 | this._fn = fn;
|
9 | }
|
10 |
|
11 | *[Symbol.iterator]() {
|
12 | const fn = this._fn;
|
13 | const sourcesLength = this._sources.length;
|
14 | const its = this._sources.map(x => x[Symbol.iterator]());
|
15 | while (sourcesLength > 0) {
|
16 | const values = new Array(sourcesLength);
|
17 | for (let index = -1; ++index < sourcesLength;) {
|
18 | const result = its[index].next();
|
19 | if (result.done) {
|
20 | its.forEach(returnIterator);
|
21 | return undefined;
|
22 | }
|
23 | values[index] = result.value;
|
24 | }
|
25 | yield fn(values);
|
26 | }
|
27 | }
|
28 | }
|
29 | export function zip(...sources) {
|
30 | let fn = (sources.shift() || identity);
|
31 | if (fn && typeof fn !== 'function') {
|
32 | sources.unshift(fn);
|
33 | fn = identity;
|
34 | }
|
35 | return new ZipIterable(sources, fn);
|
36 | }
|
37 |
|
38 |
|