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