Skip to content

Commit

Permalink
Implement Iterator.zip() - static method for zipping iterators
Browse files Browse the repository at this point in the history
PR-URL: #342
  • Loading branch information
belochub committed Jul 10, 2020
1 parent 11beabd commit dcf69d2
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ and this project adheres to
(finding minimum, maximum, or using a custom condition appropriately).
- `Iterator#partition()` to allow splitting iterator values into
multiple arrays.
- `Iterator.zip()` - static method for zipping iterators.

### Changed

Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ $ npm install @metarhia/common
- [Int64.prototype.xor](#int64prototypexorb)
- [Iterator](#class-iterator)
- [Iterator.range](#iteratorrangestart-stop-step)
- [Iterator.zip](#iteratorzipiterators)
- [Iterator.prototype.constructor](#iteratorprototypeconstructorbase)
- [Iterator.prototype.apply](#iteratorprototypeapplyfn)
- [Iterator.prototype.chain](#iteratorprototypechainiterators)
Expand Down Expand Up @@ -1058,6 +1059,14 @@ _Returns:_ `<Iterator>`

Create iterator iterating over the range

#### Iterator.zip(...iterators)

- `iterators`: [`<Array>`][array]

_Returns:_ `<Iterator>`

Create iterator by zipping multiple provided iterators into one

#### Iterator.prototype.constructor(base)

#### Iterator.prototype.apply(fn)
Expand Down
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 dcf69d2

Please sign in to comment.