Skip to content

Instantly share code, notes, and snippets.

@MichaelLeeHobbs
Last active March 13, 2021 18:00
Show Gist options
  • Save MichaelLeeHobbs/8888395f6ada28df992aadeff92d074f to your computer and use it in GitHub Desktop.
Save MichaelLeeHobbs/8888395f6ada28df992aadeff92d074f to your computer and use it in GitHub Desktop.
// https://github.com/behnammodi/polyfill/blob/master/array.polyfill.js
/**
* 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