GoScrobble/web/node_modules/core-js/modules/es.array-buffer.slice.js

37 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

2022-04-25 02:47:15 +00:00
'use strict';
var $ = require('../internals/export');
var fails = require('../internals/fails');
var ArrayBufferModule = require('../internals/array-buffer');
var anObject = require('../internals/an-object');
var toAbsoluteIndex = require('../internals/to-absolute-index');
var toLength = require('../internals/to-length');
var speciesConstructor = require('../internals/species-constructor');
var ArrayBuffer = ArrayBufferModule.ArrayBuffer;
var DataView = ArrayBufferModule.DataView;
var nativeArrayBufferSlice = ArrayBuffer.prototype.slice;
var INCORRECT_SLICE = fails(function () {
return !new ArrayBuffer(2).slice(1, undefined).byteLength;
});
// `ArrayBuffer.prototype.slice` method
// https://tc39.es/ecma262/#sec-arraybuffer.prototype.slice
$({ target: 'ArrayBuffer', proto: true, unsafe: true, forced: INCORRECT_SLICE }, {
slice: function slice(start, end) {
if (nativeArrayBufferSlice !== undefined && end === undefined) {
return nativeArrayBufferSlice.call(anObject(this), start); // FF fix
}
var length = anObject(this).byteLength;
var first = toAbsoluteIndex(start, length);
var fin = toAbsoluteIndex(end === undefined ? length : end, length);
var result = new (speciesConstructor(this, ArrayBuffer))(toLength(fin - first));
var viewSource = new DataView(this);
var viewTarget = new DataView(result);
var index = 0;
while (first < fin) {
viewTarget.setUint8(index++, viewSource.getUint8(first++));
} return result;
}
});