Skip to content

Commit 4403c04

Browse files
committed
add conditional zipSize calculation for zipAll in zipWithFactory
1 parent e84cebb commit 4403c04

File tree

4 files changed

+307
-291
lines changed

4 files changed

+307
-291
lines changed

dist/immutable.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,9 +1807,13 @@ function maxCompare(comparator, a, b) {
18071807
comp > 0;
18081808
}
18091809

1810-
function zipWithFactory(keyIter, zipper, iters) {
1810+
function zipWithFactory(keyIter, zipper, iters, zipAll) {
18111811
var zipSequence = makeSequence(keyIter);
18121812
zipSequence.size = new ArraySeq(iters).map(function (i) { return i.size; }).min();
1813+
var sizes = new ArraySeq(iters).map(function (i) { return i.size; });
1814+
var zipSize = !!zipAll ? sizes.max() : sizes.min();
1815+
zipSequence.size = zipSize;
1816+
18131817
// Note: this a generic base implementation of __iterate in terms of
18141818
// __iterator which may be more generically useful in the future.
18151819
zipSequence.__iterate = function(fn, reverse) {
@@ -1838,28 +1842,32 @@ function zipWithFactory(keyIter, zipper, iters) {
18381842
}
18391843
return iterations;
18401844
};
1845+
18411846
zipSequence.__iteratorUncached = function(type, reverse) {
1842-
var iterators = iters.map(
1843-
function (i) { return ((i = Collection(i)), getIterator(reverse ? i.reverse() : i)); }
1847+
var iterators = iters.map(function (i) { return (i = Collection(i), getIterator(reverse ? i.reverse() : i)); }
18441848
);
18451849
var iterations = 0;
18461850
var isDone = false;
18471851
return new Iterator(function () {
18481852
var steps;
18491853
if (!isDone) {
18501854
steps = iterators.map(function (i) { return i.next(); });
1851-
isDone = steps.some(function (s) { return s.done; });
1855+
if (zipAll) { isDone = steps.every(function (s) { return s.done; }); }
1856+
else { isDone = steps.some(function (s) { return s.done; }); }
18521857
}
1858+
18531859
if (isDone) {
18541860
return iteratorDone();
18551861
}
1862+
18561863
return iteratorValue(
18571864
type,
18581865
iterations++,
1859-
zipper.apply(null, steps.map(function (s) { return s.value; }))
1866+
zipper.apply(null, steps.map(function (s) { return s.done ? undefined : s.value; }))
18601867
);
18611868
});
18621869
};
1870+
18631871
return zipSequence;
18641872
}
18651873

@@ -4975,6 +4983,11 @@ mixin(IndexedCollection, {
49754983
return reify(this, zipWithFactory(this, defaultZipper, collections));
49764984
},
49774985

4986+
zipAll: function zipAll(/*, ...collections */) {
4987+
var collections = [this].concat(arrCopy(arguments));
4988+
return reify(this, zipWithFactory(this, defaultZipper, collections, true));
4989+
},
4990+
49784991
zipWith: function zipWith(zipper /*, ...collections */) {
49794992
var collections = arrCopy(arguments);
49804993
collections[0] = this;

0 commit comments

Comments
 (0)