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