UNPKG

2.66 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/*-----------------------------------------------------------------------------
4| Copyright (c) 2014-2017, PhosphorJS Contributors
5|
6| Distributed under the terms of the BSD 3-Clause License.
7|
8| The full license is in the file LICENSE, distributed with this software.
9|----------------------------------------------------------------------------*/
10var iter_1 = require("./iter");
11/**
12 * Iterate several iterables in lockstep.
13 *
14 * @param objects - The iterable or array-like objects of interest.
15 *
16 * @returns An iterator which yields successive tuples of values where
17 * each value is taken in turn from the provided iterables. It will
18 * be as long as the shortest provided iterable.
19 *
20 * #### Example
21 * ```typescript
22 * import { zip, toArray } from '@phosphor/algorithm';
23 *
24 * let data1 = [1, 2, 3];
25 * let data2 = [4, 5, 6];
26 *
27 * let stream = zip(data1, data2);
28 *
29 * toArray(stream); // [[1, 4], [2, 5], [3, 6]]
30 * ```
31 */
32function zip() {
33 var objects = [];
34 for (var _i = 0; _i < arguments.length; _i++) {
35 objects[_i] = arguments[_i];
36 }
37 return new ZipIterator(objects.map(iter_1.iter));
38}
39exports.zip = zip;
40/**
41 * An iterator which iterates several sources in lockstep.
42 */
43var ZipIterator = /** @class */ (function () {
44 /**
45 * Construct a new zip iterator.
46 *
47 * @param source - The iterators of interest.
48 */
49 function ZipIterator(source) {
50 this._source = source;
51 }
52 /**
53 * Get an iterator over the object's values.
54 *
55 * @returns An iterator which yields the object's values.
56 */
57 ZipIterator.prototype.iter = function () {
58 return this;
59 };
60 /**
61 * Create an independent clone of the iterator.
62 *
63 * @returns A new independent clone of the iterator.
64 */
65 ZipIterator.prototype.clone = function () {
66 return new ZipIterator(this._source.map(function (it) { return it.clone(); }));
67 };
68 /**
69 * Get the next value from the iterator.
70 *
71 * @returns The next value from the iterator, or `undefined`.
72 */
73 ZipIterator.prototype.next = function () {
74 var result = new Array(this._source.length);
75 for (var i = 0, n = this._source.length; i < n; ++i) {
76 var value = this._source[i].next();
77 if (value === undefined) {
78 return undefined;
79 }
80 result[i] = value;
81 }
82 return result;
83 };
84 return ZipIterator;
85}());
86exports.ZipIterator = ZipIterator;