You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
2.1 KiB
50 lines
2.1 KiB
2 weeks ago
|
'use strict';
|
||
|
var $ = require('../internals/export');
|
||
|
var isArray = require('../internals/is-array');
|
||
|
var isConstructor = require('../internals/is-constructor');
|
||
|
var isObject = require('../internals/is-object');
|
||
|
var toAbsoluteIndex = require('../internals/to-absolute-index');
|
||
|
var lengthOfArrayLike = require('../internals/length-of-array-like');
|
||
|
var toIndexedObject = require('../internals/to-indexed-object');
|
||
|
var createProperty = require('../internals/create-property');
|
||
|
var wellKnownSymbol = require('../internals/well-known-symbol');
|
||
|
var arrayMethodHasSpeciesSupport = require('../internals/array-method-has-species-support');
|
||
|
var nativeSlice = require('../internals/array-slice');
|
||
|
|
||
|
var HAS_SPECIES_SUPPORT = arrayMethodHasSpeciesSupport('slice');
|
||
|
|
||
|
var SPECIES = wellKnownSymbol('species');
|
||
|
var $Array = Array;
|
||
|
var max = Math.max;
|
||
|
|
||
|
// `Array.prototype.slice` method
|
||
|
// https://tc39.es/ecma262/#sec-array.prototype.slice
|
||
|
// fallback for not array-like ES3 strings and DOM objects
|
||
|
$({ target: 'Array', proto: true, forced: !HAS_SPECIES_SUPPORT }, {
|
||
|
slice: function slice(start, end) {
|
||
|
var O = toIndexedObject(this);
|
||
|
var length = lengthOfArrayLike(O);
|
||
|
var k = toAbsoluteIndex(start, length);
|
||
|
var fin = toAbsoluteIndex(end === undefined ? length : end, length);
|
||
|
// inline `ArraySpeciesCreate` for usage native `Array#slice` where it's possible
|
||
|
var Constructor, result, n;
|
||
|
if (isArray(O)) {
|
||
|
Constructor = O.constructor;
|
||
|
// cross-realm fallback
|
||
|
if (isConstructor(Constructor) && (Constructor === $Array || isArray(Constructor.prototype))) {
|
||
|
Constructor = undefined;
|
||
|
} else if (isObject(Constructor)) {
|
||
|
Constructor = Constructor[SPECIES];
|
||
|
if (Constructor === null) Constructor = undefined;
|
||
|
}
|
||
|
if (Constructor === $Array || Constructor === undefined) {
|
||
|
return nativeSlice(O, k, fin);
|
||
|
}
|
||
|
}
|
||
|
result = new (Constructor === undefined ? $Array : Constructor)(max(fin - k, 0));
|
||
|
for (n = 0; k < fin; k++, n++) if (k in O) createProperty(result, n, O[k]);
|
||
|
result.length = n;
|
||
|
return result;
|
||
|
}
|
||
|
});
|