Created
March 13, 2021 17:56
-
-
Save MichaelLeeHobbs/c0e61c04d9d35c7bc7e1672e08857e23 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/behnammodi/polyfill/blob/master/array.polyfill.js | |
/** | |
* Array.prototype.copyWithin() | |
* version 0.0.0 | |
* Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
* Basic support 45 32 (No) 32 9 12 | |
* ------------------------------------------------------------------------------- | |
*/ | |
if (!Array.prototype.copyWithin) { | |
Array.prototype.copyWithin = function (target, start /*, end*/) { | |
// 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-8. | |
var relativeTarget = target >> 0; | |
var to = | |
relativeTarget < 0 | |
? Math.max(len + relativeTarget, 0) | |
: Math.min(relativeTarget, len); | |
// Steps 9-11. | |
var relativeStart = start >> 0; | |
var from = | |
relativeStart < 0 | |
? Math.max(len + relativeStart, 0) | |
: Math.min(relativeStart, len); | |
// Steps 12-14. | |
var end = arguments[2]; | |
var relativeEnd = end === undefined ? len : end >> 0; | |
var final = | |
relativeEnd < 0 | |
? Math.max(len + relativeEnd, 0) | |
: Math.min(relativeEnd, len); | |
// Step 15. | |
var count = Math.min(final - from, len - to); | |
// Steps 16-17. | |
var direction = 1; | |
if (from < to && to < from + count) { | |
direction = -1; | |
from += count - 1; | |
to += count - 1; | |
} | |
// Step 18. | |
while (count > 0) { | |
if (from in O) { | |
O[to] = O[from]; | |
} else { | |
delete O[to]; | |
} | |
from += direction; | |
to += direction; | |
count--; | |
} | |
// Step 19. | |
return O; | |
}; | |
} | |
/** | |
* Array.prototype.fill() | |
* version 0.0.0 | |
* Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
* Basic support 45 31 (No) (No) 7.1 (Yes) | |
* ------------------------------------------------------------------------------- | |
*/ | |
if (!Array.prototype.fill) { | |
Object.defineProperty(Array.prototype, 'fill', { | |
value: function (value) { | |
// 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; | |
} | |
}); | |
} | |
/** | |
* Array.prototype.includes() | |
* version 0.0.0 | |
* Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
* Basic support 47 43 (No) 34 9 14 | |
* ------------------------------------------------------------------------------- | |
*/ | |
if (!Array.prototype.includes) { | |
Object.defineProperty(Array.prototype, 'includes', { | |
value: function (searchElement, fromIndex) { | |
// 1. Let O be ? ToObject(this value). | |
if (this == null) { | |
throw new TypeError('"this" is null or not defined'); | |
} | |
var o = Object(this); | |
// 2. Let len be ? ToLength(? Get(O, "length")). | |
var len = o.length >>> 0; | |
// 3. If len is 0, return false. | |
if (len === 0) { | |
return false; | |
} | |
// 4. Let n be ? ToInteger(fromIndex). | |
// (If fromIndex is undefined, this step produces the value 0.) | |
var n = fromIndex | 0; | |
// 5. If n ≥ 0, then | |
// a. Let k be n. | |
// 6. Else n < 0, | |
// a. Let k be len + n. | |
// b. If k < 0, let k be 0. | |
var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0); | |
function sameValueZero(x, y) { | |
return ( | |
x === y || | |
(typeof x === 'number' && | |
typeof y === 'number' && | |
isNaN(x) && | |
isNaN(y)) | |
); | |
} | |
// 7. Repeat, while k < len | |
while (k < len) { | |
// a. Let elementK be the result of ? Get(O, ! ToString(k)). | |
// b. If SameValueZero(searchElement, elementK) is true, return true. | |
// c. Increase k by 1. | |
if (sameValueZero(o[k], searchElement)) { | |
return true; | |
} | |
k++; | |
} | |
// 8. Return false | |
return false; | |
} | |
}); | |
} | |
/** | |
* Array.prototype.keys() | |
* version 0.0.0 | |
* Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
* Basic support 38 28 (No) 25 7.1 (Yes) | |
* ------------------------------------------------------------------------------- | |
*/ | |
/** | |
* Array.prototype.lastIndexOf() | |
* version 0.0.0 | |
* Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
* Basic support (Yes) (Yes) 9 (Yes) (Yes) ? | |
* ------------------------------------------------------------------------------- | |
*/ | |
if (!Array.prototype.lastIndexOf) { | |
Array.prototype.lastIndexOf = function (searchElement /*, fromIndex*/) { | |
'use strict'; | |
if (this === void 0 || this === null) { | |
throw new TypeError(); | |
} | |
var n, | |
k, | |
t = Object(this), | |
len = t.length >>> 0; | |
if (len === 0) { | |
return -1; | |
} | |
n = len - 1; | |
if (arguments.length > 1) { | |
n = Number(arguments[1]); | |
if (n != n) { | |
n = 0; | |
} else if (n != 0 && n != 1 / 0 && n != -(1 / 0)) { | |
n = (n > 0 || -1) * Math.floor(Math.abs(n)); | |
} | |
} | |
for (k = n >= 0 ? Math.min(n, len - 1) : len - Math.abs(n); k >= 0; k--) { | |
if (k in t && t[k] === searchElement) { | |
return k; | |
} | |
} | |
return -1; | |
}; | |
} | |
/** | |
* Array.prototype.entries() | |
* version 0.0.0 | |
* Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
* Basic support 38 28 (No) 25 7.1 ? | |
* ------------------------------------------------------------------------------- | |
*/ | |
/** | |
* Array.prototype.every() | |
* version 0.0.0 | |
* Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
* Basic support (Yes) 1.5 9 (Yes) (Yes) ? | |
* ------------------------------------------------------------------------------- | |
*/ | |
if (!Array.prototype.every) { | |
Array.prototype.every = function (callbackfn, thisArg) { | |
'use strict'; | |
var T, k; | |
if (this == null) { | |
throw new TypeError('this is null or not defined'); | |
} | |
// 1. Let O be the result of calling ToObject passing the this | |
// value as the argument. | |
var O = Object(this); | |
// 2. Let lenValue be the result of calling the Get internal method | |
// of O with the argument "length". | |
// 3. Let len be ToUint32(lenValue). | |
var len = O.length >>> 0; | |
// 4. If IsCallable(callbackfn) is false, throw a TypeError exception. | |
if (typeof callbackfn !== 'function') { | |
throw new TypeError(); | |
} | |
// 5. If thisArg was supplied, let T be thisArg; else let T be undefined. | |
if (arguments.length > 1) { | |
T = thisArg; | |
} | |
// 6. Let k be 0. | |
k = 0; | |
// 7. Repeat, while k < len | |
while (k < len) { | |
var kValue; | |
// a. Let Pk be ToString(k). | |
// This is implicit for LHS operands of the in operator | |
// b. Let kPresent be the result of calling the HasProperty internal | |
// method of O with argument Pk. | |
// This step can be combined with c | |
// c. If kPresent is true, then | |
if (k in O) { | |
// i. Let kValue be the result of calling the Get internal method | |
// of O with argument Pk. | |
kValue = O[k]; | |
// ii. Let testResult be the result of calling the Call internal method | |
// of callbackfn with T as the this value and argument list | |
// containing kValue, k, and O. | |
var testResult = callbackfn.call(T, kValue, k, O); | |
// iii. If ToBoolean(testResult) is false, return false. | |
if (!testResult) { | |
return false; | |
} | |
} | |
k++; | |
} | |
return true; | |
}; | |
} | |
/** | |
* Array.prototype.flat() | |
* version 0.0.0 | |
* Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
* Basic support 69 62 (No) 56 12 (No) | |
* ------------------------------------------------------------------------------- | |
*/ | |
if (!Array.prototype.flat) { | |
Array.prototype.flat = function () { | |
const stack = [].concat(this); | |
const result = []; | |
while (stack.length) { | |
const next = stack.pop(); | |
if (Array.isArray(next)) stack.push.apply(stack, next); | |
else result.push(next); | |
} | |
return result.reverse(); | |
}; | |
} | |
/** | |
* Array.prototype.flatMap() | |
* version 0.0.0 | |
* Feature Chrome Firefox Internet Explorer Opera Safari Edge | |
* Basic support 69 62 (No) 56 12 (No) | |
* ------------------------------------------------------------------------------- | |
*/ | |
if (!Array.prototype.flatMap) { | |
Array.prototype.flatMap = function () { | |
return Array.prototype.map.apply(this, arguments).flat(1); | |
}; | |
} | |
// Based on https://github.com/FabioVergani/js-Polyfill_String-trimStart | |
if (!String.prototype.trimStart) { | |
String.prototype.trimStart = function () { | |
var str = this.valueOf() | |
var r = /^\s+/ | |
return str.replace(r, '') | |
}; | |
} | |
if (!String.prototype.trimEnd) { | |
String.prototype.trimEnd = function () { | |
var str = this.valueOf() | |
var r = /\s+$/ | |
return str.replace(r, '') | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment