UNPKG

1.22 kBJavaScriptView Raw
1import { IterableX } from './iterablex';
2import { identity } from '../internal/identity';
3import { returnIterator } from '../internal/returniterator';
4export class ZipIterable extends IterableX {
5 constructor(sources, fn) {
6 super();
7 this._sources = sources;
8 this._fn = fn;
9 }
10 *[Symbol.iterator]() {
11 const fn = this._fn;
12 const sourcesLength = this._sources.length;
13 const its = this._sources.map(x => x[Symbol.iterator]());
14 while (sourcesLength > 0) {
15 const values = new Array(sourcesLength);
16 for (let index = -1; ++index < sourcesLength;) {
17 const result = its[index].next();
18 if (result.done) {
19 its.forEach(returnIterator);
20 return undefined;
21 }
22 values[index] = result.value;
23 }
24 yield fn(values);
25 }
26 }
27}
28/* tslint:enable:max-line-length */
29export 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//# sourceMappingURL=zip.mjs.map