Skip to content
This repository was archived by the owner on Feb 9, 2022. It is now read-only.

Fix for issue #25 where zipLongest sometimes returns extra item #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions test/test-zipLongest.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
const wu = require("../wu");
const assert = require("../assert");
describe("wu.zipLongest", () => {
it("should stop with the longer iterable", () => {
const arr1 = [];
arr1[1] = 2;
const arr2 = [];
arr2[1] = 3;
assert.eqArray([["a", 1], arr1, arr2],
wu.zipLongest("a", [1, 2, 3]));

var tests =[
{args: ["a", [1,2,3]], expected: [["a", 1], [,2], [,3]]},
{args: [["a"], [1,2,3]], expected: [["a", 1], [,2], [,3]]},
{args: [["a","b","c"], [1,2]], expected: [["a", 1], ["b",2], ["c", ]]},
{args: ["a", [1,2,3], [7,8,9,10]], expected: [["a", 1, 7], [ , 2, 8], [ , 3, 9], [ , , 10]]},
{args: [["a"], [1,2,3,4], [7,8,9]], expected: [["a", 1, 7], [ , 2, 8], [ , 3, 9], [ , 4, ]]}
];

tests.forEach(test => {
it('should stop with the longest iterable', () =>
{
assert.eqArray(test.expected,
wu.zipLongest(...test.args));
})
});
});

7 changes: 3 additions & 4 deletions wu.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,10 @@ const _zip = rewrap(function* (iterables, longest=false) {

const iters = iterables.map(getIterator);
const numIters = iterables.length;
let numFinished = 0;
let finished = false;

while (!finished) {
while (true) {
let zipped = [];
let numFinished = 0;

for (let it of iters) {
let { value, done } = it.next();
Expand All @@ -348,7 +347,7 @@ const _zip = rewrap(function* (iterables, longest=false) {
return;
}
if (++numFinished == numIters) {
finished = true;
return;
}
}
if (value === undefined) {
Expand Down