Skip to content

Commit

Permalink
feat: implement Iterator.zip() - static method for zipping iterators
Browse files Browse the repository at this point in the history
ORIGINAL-PR-URL: metarhia/common#342
  • Loading branch information
belochub authored and lundibundi committed Sep 13, 2020
1 parent 2a2ea3a commit f2a3c1c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
8 changes: 8 additions & 0 deletions lib/iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,14 @@ class Iterator {
return iter(res && res[Symbol.iterator] ? res : [res]);
}

// Create iterator by zipping multiple provided iterators into one
// Signature: ...iterators
// iterators <Array>
// Returns: <Iterator>
static zip(base, ...iterators) {
return new ZipIterator(toIterator(base), iterators);
}

// Create iterator iterating over the range
// Signature: start, stop[, step]
// start <number>
Expand Down
22 changes: 20 additions & 2 deletions test/iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,14 @@ metatests.test('Iterator.flatMap with thisArg', test => {
test.end();
});

metatests.test('Iterator.zip with single iterator', test => {
metatests.test('Iterator#zip with single iterator', test => {
const it = iter(array).take(1);
const toZip = iter(array).skip(2);
test.strictSame(it.zip(toZip).toArray(), [[1, 3]]);
test.end();
});

metatests.test('Iterator.zip with multiple iterators', test => {
metatests.test('Iterator#zip with multiple iterators', test => {
const it = iter(array);
const itr = iter(array).take(3);
const iterator = iter(array).take(2);
Expand All @@ -297,6 +297,24 @@ metatests.test('Iterator.zip with multiple iterators', test => {
test.end();
});

metatests.test('Iterator.zip with single iterator', test => {
const it1 = iter(array).take(1);
const it2 = iter(array).skip(2);
test.strictSame(Iterator.zip(it1, it2).toArray(), [[1, 3]]);
test.end();
});

metatests.test('Iterator.zip with multiple iterators', test => {
const it1 = iter(array);
const it2 = iter(array).take(3);
const it3 = iter(array).take(2);
test.strictSame(Iterator.zip(it1, it2, it3).toArray(), [
[1, 1, 1],
[2, 2, 2],
]);
test.end();
});

metatests.test('Iterator.chain', test => {
const it = iter(array).take(1);
const itr = iter(array)
Expand Down

0 comments on commit f2a3c1c

Please sign in to comment.