1 | 'use strict';
|
---|
2 | var $ = require('../internals/export');
|
---|
3 | var fails = require('../internals/fails');
|
---|
4 | var isArray = require('../internals/is-array');
|
---|
5 | var isObject = require('../internals/is-object');
|
---|
6 | var toObject = require('../internals/to-object');
|
---|
7 | var lengthOfArrayLike = require('../internals/length-of-array-like');
|
---|
8 | var doesNotExceedSafeInteger = require('../internals/does-not-exceed-safe-integer');
|
---|
9 | var createProperty = require('../internals/create-property');
|
---|
10 | var arraySpeciesCreate = require('../internals/array-species-create');
|
---|
11 | var arrayMethodHasSpeciesSupport = require('../internals/array-method-has-species-support');
|
---|
12 | var wellKnownSymbol = require('../internals/well-known-symbol');
|
---|
13 | var V8_VERSION = require('../internals/environment-v8-version');
|
---|
14 |
|
---|
15 | var IS_CONCAT_SPREADABLE = wellKnownSymbol('isConcatSpreadable');
|
---|
16 |
|
---|
17 | // We can't use this feature detection in V8 since it causes
|
---|
18 | // deoptimization and serious performance degradation
|
---|
19 | // https://github.com/zloirock/core-js/issues/679
|
---|
20 | var IS_CONCAT_SPREADABLE_SUPPORT = V8_VERSION >= 51 || !fails(function () {
|
---|
21 | var array = [];
|
---|
22 | array[IS_CONCAT_SPREADABLE] = false;
|
---|
23 | return array.concat()[0] !== array;
|
---|
24 | });
|
---|
25 |
|
---|
26 | var isConcatSpreadable = function (O) {
|
---|
27 | if (!isObject(O)) return false;
|
---|
28 | var spreadable = O[IS_CONCAT_SPREADABLE];
|
---|
29 | return spreadable !== undefined ? !!spreadable : isArray(O);
|
---|
30 | };
|
---|
31 |
|
---|
32 | var FORCED = !IS_CONCAT_SPREADABLE_SUPPORT || !arrayMethodHasSpeciesSupport('concat');
|
---|
33 |
|
---|
34 | // `Array.prototype.concat` method
|
---|
35 | // https://tc39.es/ecma262/#sec-array.prototype.concat
|
---|
36 | // with adding support of @@isConcatSpreadable and @@species
|
---|
37 | $({ target: 'Array', proto: true, arity: 1, forced: FORCED }, {
|
---|
38 | // eslint-disable-next-line no-unused-vars -- required for `.length`
|
---|
39 | concat: function concat(arg) {
|
---|
40 | var O = toObject(this);
|
---|
41 | var A = arraySpeciesCreate(O, 0);
|
---|
42 | var n = 0;
|
---|
43 | var i, k, length, len, E;
|
---|
44 | for (i = -1, length = arguments.length; i < length; i++) {
|
---|
45 | E = i === -1 ? O : arguments[i];
|
---|
46 | if (isConcatSpreadable(E)) {
|
---|
47 | len = lengthOfArrayLike(E);
|
---|
48 | doesNotExceedSafeInteger(n + len);
|
---|
49 | for (k = 0; k < len; k++, n++) if (k in E) createProperty(A, n, E[k]);
|
---|
50 | } else {
|
---|
51 | doesNotExceedSafeInteger(n + 1);
|
---|
52 | createProperty(A, n++, E);
|
---|
53 | }
|
---|
54 | }
|
---|
55 | A.length = n;
|
---|
56 | return A;
|
---|
57 | }
|
---|
58 | });
|
---|