Skip to content

Commit 12b441d

Browse files
committed
Fix Iterator.zip and Iterator.zipKeyed behavior with mode: 'longest' option (#1469)
1 parent ce89bd7 commit 12b441d

File tree

5 files changed

+157
-1
lines changed

5 files changed

+157
-1
lines changed

packages/core-js/internals/iterator-zip.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ var IteratorProxy = createIteratorProxy(function () {
7979
iters[i] = null;
8080
result = padding[i];
8181
}
82+
push(results, result);
8283
}
83-
push(results, result);
8484
}
8585

8686
return finishResults ? finishResults(results) : results;

tests/unit-global/esnext.iterator.zip-keyed.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { createIterator } from '../helpers/helpers.js';
22
import { DESCRIPTORS } from '../helpers/constants.js';
33

4+
function nullProto(obj) {
5+
return Object.assign(Object.create(null), obj);
6+
}
7+
48
QUnit.test('Iterator.zipKeyed', assert => {
59
const { zipKeyed } = Iterator;
610
const { from } = Array;
@@ -56,4 +60,41 @@ QUnit.test('Iterator.zipKeyed', assert => {
5660
assert.same(result[2][bar], 6);
5761
assert.same(result[2].baz, 9);
5862
}
63+
64+
{
65+
const result = zipKeyed({
66+
a: [0, 1, 2],
67+
b: [3, 4, 5, 6, 7],
68+
c: [8, 9],
69+
}, {
70+
mode: "longest",
71+
});
72+
73+
assert.deepEqual(from(result), [
74+
nullProto({ a: 0, b: 3, c: 8 }),
75+
nullProto({ a: 1, b: 4, c: 9 }),
76+
nullProto({ a: 2, b: 5, c: undefined }),
77+
nullProto({ a: undefined, b: 6, c: undefined }),
78+
nullProto({ a: undefined, b: 7, c: undefined }),
79+
]);
80+
}
81+
82+
{
83+
const result = zipKeyed({
84+
a: [0, 1, 2],
85+
b: [3, 4, 5, 6, 7],
86+
c: [8, 9],
87+
}, {
88+
mode: "longest",
89+
padding: { a: 'A', b: 'B', c: 'C' },
90+
});
91+
92+
assert.deepEqual(from(result), [
93+
nullProto({ a: 0, b: 3, c: 8 }),
94+
nullProto({ a: 1, b: 4, c: 9 }),
95+
nullProto({ a: 2, b: 5, c: 'C' }),
96+
nullProto({ a: 'A', b: 6, c: 'C' }),
97+
nullProto({ a: 'A', b: 7, c: 'C' }),
98+
]);
99+
}
59100
});

tests/unit-global/esnext.iterator.zip.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,41 @@ QUnit.test('Iterator.zip', assert => {
6262
assert.throws(() => from(result), Error);
6363
assert.true(it2.called, 'iterator return called #4');
6464
}
65+
66+
{
67+
const result = zip([
68+
[0, 1, 2],
69+
[3, 4, 5, 6, 7],
70+
[8, 9],
71+
], {
72+
mode: "longest",
73+
});
74+
75+
assert.deepEqual(from(result), [
76+
[0, 3, 8],
77+
[1, 4, 9],
78+
[2, 5, undefined],
79+
[undefined, 6, undefined],
80+
[undefined, 7, undefined]
81+
]);
82+
}
83+
84+
{
85+
const result = zip([
86+
[0, 1, 2],
87+
[3, 4, 5, 6, 7],
88+
[8, 9],
89+
], {
90+
mode: "longest",
91+
padding: ["A", "B", "C"],
92+
});
93+
94+
assert.deepEqual(from(result), [
95+
[0, 3, 8],
96+
[1, 4, 9],
97+
[2, 5, 'C'],
98+
['A', 6, 'C'],
99+
['A', 7, 'C']
100+
]);
101+
}
65102
});

tests/unit-pure/esnext.iterator.zip-keyed.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import from from 'core-js-pure/es/array/from';
66
import Symbol from 'core-js-pure/full/symbol';
77
import zipKeyed from 'core-js-pure/full/iterator/zip-keyed';
88

9+
function nullProto(obj) {
10+
return Object.assign(Object.create(null), obj);
11+
}
12+
913
QUnit.test('Iterator.zipKeyed', assert => {
1014
assert.isFunction(zipKeyed);
1115
assert.arity(zipKeyed, 1);
@@ -55,4 +59,41 @@ QUnit.test('Iterator.zipKeyed', assert => {
5559
assert.same(result[2][bar], 6);
5660
assert.same(result[2].baz, 9);
5761
}
62+
63+
{
64+
const result = zipKeyed({
65+
a: [0, 1, 2],
66+
b: [3, 4, 5, 6, 7],
67+
c: [8, 9],
68+
}, {
69+
mode: "longest",
70+
});
71+
72+
assert.deepEqual(from(result), [
73+
nullProto({ a: 0, b: 3, c: 8 }),
74+
nullProto({ a: 1, b: 4, c: 9 }),
75+
nullProto({ a: 2, b: 5, c: undefined }),
76+
nullProto({ a: undefined, b: 6, c: undefined }),
77+
nullProto({ a: undefined, b: 7, c: undefined }),
78+
]);
79+
}
80+
81+
{
82+
const result = zipKeyed({
83+
a: [0, 1, 2],
84+
b: [3, 4, 5, 6, 7],
85+
c: [8, 9],
86+
}, {
87+
mode: "longest",
88+
padding: { a: 'A', b: 'B', c: 'C' },
89+
});
90+
91+
assert.deepEqual(from(result), [
92+
nullProto({ a: 0, b: 3, c: 8 }),
93+
nullProto({ a: 1, b: 4, c: 9 }),
94+
nullProto({ a: 2, b: 5, c: 'C' }),
95+
nullProto({ a: 'A', b: 6, c: 'C' }),
96+
nullProto({ a: 'A', b: 7, c: 'C' }),
97+
]);
98+
}
5899
});

tests/unit-pure/esnext.iterator.zip.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,41 @@ QUnit.test('Iterator.zip', assert => {
6060
assert.throws(() => from(result), Error);
6161
assert.true(it2.called, 'iterator return called #4');
6262
}
63+
64+
{
65+
const result = zip([
66+
[0, 1, 2],
67+
[3, 4, 5, 6, 7],
68+
[8, 9],
69+
], {
70+
mode: "longest",
71+
});
72+
73+
assert.deepEqual(from(result), [
74+
[0, 3, 8],
75+
[1, 4, 9],
76+
[2, 5, undefined],
77+
[undefined, 6, undefined],
78+
[undefined, 7, undefined]
79+
]);
80+
}
81+
82+
{
83+
const result = zip([
84+
[0, 1, 2],
85+
[3, 4, 5, 6, 7],
86+
[8, 9],
87+
], {
88+
mode: "longest",
89+
padding: ["A", "B", "C"],
90+
});
91+
92+
assert.deepEqual(from(result), [
93+
[0, 3, 8],
94+
[1, 4, 9],
95+
[2, 5, 'C'],
96+
['A', 6, 'C'],
97+
['A', 7, 'C']
98+
]);
99+
}
63100
});

0 commit comments

Comments
 (0)