Provide Array.fill in common stdlib

KT-32359
This commit is contained in:
Jake Wharton
2019-07-19 10:09:48 -04:00
committed by Ilya Gorbunov
parent 83e422fc3b
commit 1a6069382e
14 changed files with 677 additions and 84 deletions
@@ -249,6 +249,49 @@ if (typeof ArrayBuffer.isView === "undefined") {
};
}
if (typeof Array.prototype.fill === "undefined") {
// Polyfill from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill#Polyfill
Array.prototype.fill = function() {
// Steps 1-2.
if (this == null) {
throw new TypeError('this is null or not defined');
}
var O = Object(this);
// Steps 3-5.
var len = O.length >>> 0;
// Steps 6-7.
var start = arguments[1];
var relativeStart = start >> 0;
// Step 8.
var k = relativeStart < 0 ?
Math.max(len + relativeStart, 0) :
Math.min(relativeStart, len);
// Steps 9-10.
var end = arguments[2];
var relativeEnd = end === undefined ?
len : end >> 0;
// Step 11.
var final = relativeEnd < 0 ?
Math.max(len + relativeEnd, 0) :
Math.min(relativeEnd, len);
// Step 12.
while (k < final) {
O[k] = value;
k++;
}
// Step 13.
return O;
};
}
(function() {
function normalizeOffset(offset, length) {
if (offset < 0) return Math.max(0, offset + length);
@@ -266,6 +309,9 @@ if (typeof ArrayBuffer.isView === "undefined") {
var arrays = [Int8Array, Int16Array, Uint16Array, Int32Array, Float32Array, Float64Array];
for (var i = 0; i < arrays.length; ++i) {
var TypedArray = arrays[i];
if (typeof TypedArray.prototype.fill === "undefined") {
TypedArray.prototype.fill = Array.prototype.fill;
}
if (typeof TypedArray.prototype.slice === "undefined") {
Object.defineProperty(TypedArray.prototype, 'slice', {
value: typedArraySlice