1 | 'use strict';
|
---|
2 | var $ = require('../internals/export');
|
---|
3 | var isArray = require('../internals/is-array');
|
---|
4 | var isConstructor = require('../internals/is-constructor');
|
---|
5 | var isObject = require('../internals/is-object');
|
---|
6 | var toAbsoluteIndex = require('../internals/to-absolute-index');
|
---|
7 | var lengthOfArrayLike = require('../internals/length-of-array-like');
|
---|
8 | var toIndexedObject = require('../internals/to-indexed-object');
|
---|
9 | var createProperty = require('../internals/create-property');
|
---|
10 | var wellKnownSymbol = require('../internals/well-known-symbol');
|
---|
11 | var arrayMethodHasSpeciesSupport = require('../internals/array-method-has-species-support');
|
---|
12 | var nativeSlice = require('../internals/array-slice');
|
---|
13 |
|
---|
14 | var HAS_SPECIES_SUPPORT = arrayMethodHasSpeciesSupport('slice');
|
---|
15 |
|
---|
16 | var SPECIES = wellKnownSymbol('species');
|
---|
17 | var $Array = Array;
|
---|
18 | var max = Math.max;
|
---|
19 |
|
---|
20 | // `Array.prototype.slice` method
|
---|
21 | // https://tc39.es/ecma262/#sec-array.prototype.slice
|
---|
22 | // fallback for not array-like ES3 strings and DOM objects
|
---|
23 | $({ target: 'Array', proto: true, forced: !HAS_SPECIES_SUPPORT }, {
|
---|
24 | slice: function slice(start, end) {
|
---|
25 | var O = toIndexedObject(this);
|
---|
26 | var length = lengthOfArrayLike(O);
|
---|
27 | var k = toAbsoluteIndex(start, length);
|
---|
28 | var fin = toAbsoluteIndex(end === undefined ? length : end, length);
|
---|
29 | // inline `ArraySpeciesCreate` for usage native `Array#slice` where it's possible
|
---|
30 | var Constructor, result, n;
|
---|
31 | if (isArray(O)) {
|
---|
32 | Constructor = O.constructor;
|
---|
33 | // cross-realm fallback
|
---|
34 | if (isConstructor(Constructor) && (Constructor === $Array || isArray(Constructor.prototype))) {
|
---|
35 | Constructor = undefined;
|
---|
36 | } else if (isObject(Constructor)) {
|
---|
37 | Constructor = Constructor[SPECIES];
|
---|
38 | if (Constructor === null) Constructor = undefined;
|
---|
39 | }
|
---|
40 | if (Constructor === $Array || Constructor === undefined) {
|
---|
41 | return nativeSlice(O, k, fin);
|
---|
42 | }
|
---|
43 | }
|
---|
44 | result = new (Constructor === undefined ? $Array : Constructor)(max(fin - k, 0));
|
---|
45 | for (n = 0; k < fin; k++, n++) if (k in O) createProperty(result, n, O[k]);
|
---|
46 | result.length = n;
|
---|
47 | return result;
|
---|
48 | }
|
---|
49 | });
|
---|