Skip to content

Commit 8032154

Browse files
committed
update Array#flatten and Array#flatMap
- use `isArray` instead of `isConcatSpreadable` - set the target length explicitly
1 parent ba35bf9 commit 8032154

9 files changed

+15
-39
lines changed

client/core.js

+4-12
Original file line numberDiff line numberDiff line change
@@ -3587,28 +3587,20 @@ module.exports = function (it) {
35873587

35883588
// https://tc39.github.io/proposal-flatMap/#sec-FlattenIntoArray
35893589
var isArray = __webpack_require__(51);
3590-
var isObject = __webpack_require__(4);
35913590
var toLength = __webpack_require__(8);
35923591
var ctx = __webpack_require__(18);
3593-
var IS_CONCAT_SPREADABLE = __webpack_require__(5)('isConcatSpreadable');
35943592

35953593
function flattenIntoArray(target, original, source, sourceLen, start, depth, mapper, thisArg) {
35963594
var targetIndex = start;
35973595
var sourceIndex = 0;
35983596
var mapFn = mapper ? ctx(mapper, thisArg, 3) : false;
3599-
var element, spreadable;
3597+
var element;
36003598

36013599
while (sourceIndex < sourceLen) {
36023600
if (sourceIndex in source) {
36033601
element = mapFn ? mapFn(source[sourceIndex], sourceIndex, original) : source[sourceIndex];
36043602

3605-
spreadable = false;
3606-
if (isObject(element)) {
3607-
spreadable = element[IS_CONCAT_SPREADABLE];
3608-
spreadable = spreadable !== undefined ? !!spreadable : isArray(element);
3609-
}
3610-
3611-
if (spreadable && depth > 0) {
3603+
if (isArray(element) && depth > 0) {
36123604
targetIndex = flattenIntoArray(target, original, element, toLength(element.length), targetIndex, depth - 1) - 1;
36133605
} else {
36143606
if (targetIndex >= 0x1fffffffffffff) throw TypeError();
@@ -7134,7 +7126,7 @@ $export($export.P, 'Array', {
71347126
aFunction(callbackfn);
71357127
sourceLen = toLength(O.length);
71367128
A = arraySpeciesCreate(O, 0);
7137-
flattenIntoArray(A, O, O, sourceLen, 0, 1, callbackfn, arguments[1]);
7129+
A.length = flattenIntoArray(A, O, O, sourceLen, 0, 1, callbackfn, arguments[1]);
71387130
return A;
71397131
}
71407132
});
@@ -7162,7 +7154,7 @@ $export($export.P, 'Array', {
71627154
var O = toObject(this);
71637155
var sourceLen = toLength(O.length);
71647156
var A = arraySpeciesCreate(O, 0);
7165-
flattenIntoArray(A, O, O, sourceLen, 0, depthArg === undefined ? 1 : toInteger(depthArg));
7157+
A.length = flattenIntoArray(A, O, O, sourceLen, 0, depthArg === undefined ? 1 : toInteger(depthArg));
71667158
return A;
71677159
}
71687160
});

client/core.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/core.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

library/modules/_flatten-into-array.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,20 @@
11
'use strict';
22
// https://tc39.github.io/proposal-flatMap/#sec-FlattenIntoArray
33
var isArray = require('./_is-array');
4-
var isObject = require('./_is-object');
54
var toLength = require('./_to-length');
65
var ctx = require('./_ctx');
7-
var IS_CONCAT_SPREADABLE = require('./_wks')('isConcatSpreadable');
86

97
function flattenIntoArray(target, original, source, sourceLen, start, depth, mapper, thisArg) {
108
var targetIndex = start;
119
var sourceIndex = 0;
1210
var mapFn = mapper ? ctx(mapper, thisArg, 3) : false;
13-
var element, spreadable;
11+
var element;
1412

1513
while (sourceIndex < sourceLen) {
1614
if (sourceIndex in source) {
1715
element = mapFn ? mapFn(source[sourceIndex], sourceIndex, original) : source[sourceIndex];
1816

19-
spreadable = false;
20-
if (isObject(element)) {
21-
spreadable = element[IS_CONCAT_SPREADABLE];
22-
spreadable = spreadable !== undefined ? !!spreadable : isArray(element);
23-
}
24-
25-
if (spreadable && depth > 0) {
17+
if (isArray(element) && depth > 0) {
2618
targetIndex = flattenIntoArray(target, original, element, toLength(element.length), targetIndex, depth - 1) - 1;
2719
} else {
2820
if (targetIndex >= 0x1fffffffffffff) throw TypeError();

library/modules/esnext.array.flat-map.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ $export($export.P, 'Array', {
1414
aFunction(callbackfn);
1515
sourceLen = toLength(O.length);
1616
A = arraySpeciesCreate(O, 0);
17-
flattenIntoArray(A, O, O, sourceLen, 0, 1, callbackfn, arguments[1]);
17+
A.length = flattenIntoArray(A, O, O, sourceLen, 0, 1, callbackfn, arguments[1]);
1818
return A;
1919
}
2020
});

library/modules/esnext.array.flatten.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ $export($export.P, 'Array', {
1313
var O = toObject(this);
1414
var sourceLen = toLength(O.length);
1515
var A = arraySpeciesCreate(O, 0);
16-
flattenIntoArray(A, O, O, sourceLen, 0, depthArg === undefined ? 1 : toInteger(depthArg));
16+
A.length = flattenIntoArray(A, O, O, sourceLen, 0, depthArg === undefined ? 1 : toInteger(depthArg));
1717
return A;
1818
}
1919
});

modules/_flatten-into-array.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,20 @@
11
'use strict';
22
// https://tc39.github.io/proposal-flatMap/#sec-FlattenIntoArray
33
var isArray = require('./_is-array');
4-
var isObject = require('./_is-object');
54
var toLength = require('./_to-length');
65
var ctx = require('./_ctx');
7-
var IS_CONCAT_SPREADABLE = require('./_wks')('isConcatSpreadable');
86

97
function flattenIntoArray(target, original, source, sourceLen, start, depth, mapper, thisArg) {
108
var targetIndex = start;
119
var sourceIndex = 0;
1210
var mapFn = mapper ? ctx(mapper, thisArg, 3) : false;
13-
var element, spreadable;
11+
var element;
1412

1513
while (sourceIndex < sourceLen) {
1614
if (sourceIndex in source) {
1715
element = mapFn ? mapFn(source[sourceIndex], sourceIndex, original) : source[sourceIndex];
1816

19-
spreadable = false;
20-
if (isObject(element)) {
21-
spreadable = element[IS_CONCAT_SPREADABLE];
22-
spreadable = spreadable !== undefined ? !!spreadable : isArray(element);
23-
}
24-
25-
if (spreadable && depth > 0) {
17+
if (isArray(element) && depth > 0) {
2618
targetIndex = flattenIntoArray(target, original, element, toLength(element.length), targetIndex, depth - 1) - 1;
2719
} else {
2820
if (targetIndex >= 0x1fffffffffffff) throw TypeError();

modules/esnext.array.flat-map.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ $export($export.P, 'Array', {
1414
aFunction(callbackfn);
1515
sourceLen = toLength(O.length);
1616
A = arraySpeciesCreate(O, 0);
17-
flattenIntoArray(A, O, O, sourceLen, 0, 1, callbackfn, arguments[1]);
17+
A.length = flattenIntoArray(A, O, O, sourceLen, 0, 1, callbackfn, arguments[1]);
1818
return A;
1919
}
2020
});

modules/esnext.array.flatten.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ $export($export.P, 'Array', {
1313
var O = toObject(this);
1414
var sourceLen = toLength(O.length);
1515
var A = arraySpeciesCreate(O, 0);
16-
flattenIntoArray(A, O, O, sourceLen, 0, depthArg === undefined ? 1 : toInteger(depthArg));
16+
A.length = flattenIntoArray(A, O, O, sourceLen, 0, depthArg === undefined ? 1 : toInteger(depthArg));
1717
return A;
1818
}
1919
});

0 commit comments

Comments
 (0)