mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-12-05 00:57:59 +01:00
104 lines
2.7 KiB
JavaScript
104 lines
2.7 KiB
JavaScript
/**
|
|
* ES6 methods polyfill
|
|
* Sourced from their respective articles on MDN
|
|
*/
|
|
|
|
if (!Array.prototype.find) {
|
|
Array.prototype.find = function(predicate) {
|
|
'use strict';
|
|
if (this == null) {
|
|
throw new TypeError('Array.prototype.find called on null or undefined');
|
|
}
|
|
if (typeof predicate !== 'function') {
|
|
throw new TypeError('predicate must be a function');
|
|
}
|
|
var list = Object(this);
|
|
var length = list.length >>> 0;
|
|
var thisArg = arguments[1];
|
|
var value;
|
|
|
|
for (var i = 0; i < length; i++) {
|
|
value = list[i];
|
|
if (predicate.call(thisArg, value, i, list)) {
|
|
return value;
|
|
}
|
|
}
|
|
return undefined;
|
|
};
|
|
}
|
|
|
|
if (!Array.prototype.findIndex) {
|
|
Array.prototype.findIndex = function(predicate) {
|
|
'use strict';
|
|
if (this == null) {
|
|
throw new TypeError('Array.prototype.findIndex called on null or undefined');
|
|
}
|
|
if (typeof predicate !== 'function') {
|
|
throw new TypeError('predicate must be a function');
|
|
}
|
|
var list = Object(this);
|
|
var length = list.length >>> 0;
|
|
var thisArg = arguments[1];
|
|
var value;
|
|
|
|
for (var i = 0; i < length; i++) {
|
|
value = list[i];
|
|
if (predicate.call(thisArg, value, i, list)) {
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
};
|
|
}
|
|
|
|
if (!Array.prototype.includes) {
|
|
Array.prototype.includes = function(searchElement /*, fromIndex*/) {
|
|
'use strict';
|
|
if (this == null) {
|
|
throw new TypeError('Array.prototype.includes called on null or undefined');
|
|
}
|
|
|
|
var O = Object(this);
|
|
var len = parseInt(O.length, 10) || 0;
|
|
if (len === 0) {
|
|
return false;
|
|
}
|
|
var n = parseInt(arguments[1], 10) || 0;
|
|
var k;
|
|
if (n >= 0) {
|
|
k = n;
|
|
} else {
|
|
k = len + n;
|
|
if (k < 0) {k = 0;}
|
|
}
|
|
var currentElement;
|
|
while (k < len) {
|
|
currentElement = O[k];
|
|
if (searchElement === currentElement ||
|
|
(searchElement !== searchElement && currentElement !== currentElement)) { // NaN !== NaN
|
|
return true;
|
|
}
|
|
k++;
|
|
}
|
|
return false;
|
|
};
|
|
}
|
|
|
|
if (!String.prototype.startsWith) {
|
|
String.prototype.startsWith = function(searchString, position){
|
|
position = position || 0;
|
|
return this.substr(position, searchString.length) === searchString;
|
|
};
|
|
}
|
|
|
|
if (!String.prototype.endsWith) {
|
|
String.prototype.endsWith = function(searchString, position) {
|
|
var subjectString = this.toString();
|
|
if (typeof position !== 'number' || !isFinite(position) || Math.floor(position) !== position || position > subjectString.length) {
|
|
position = subjectString.length;
|
|
}
|
|
position -= searchString.length;
|
|
var lastIndex = subjectString.lastIndexOf(searchString, position);
|
|
return lastIndex !== -1 && lastIndex === position;
|
|
};
|
|
}
|