aurelia-polyfills
Version:
The minimal set of polyfills that the Aurelia platform needs to run on ES5 browsers.
138 lines (129 loc) • 3.97 kB
JavaScript
if (typeof FEATURE_NO_ES2015 === 'undefined') {
if (!Array.from) {
Array.from = (function () {
var toInteger = function(it) {
return isNaN(it = +it) ? 0 : (it > 0 ? Math.floor : Math.ceil)(it);
};
var toLength = function(it) {
return it > 0 ? Math.min(toInteger(it), 0x1fffffffffffff) : 0; // pow(2, 53) - 1 == 9007199254740991
};
var iterCall = function(iter, fn, val, index) {
try {
return fn(val, index)
}
catch (E) {
if (typeof iter.return == 'function') iter.return();
throw E;
}
};
// The length property of the from method is 1.
return function from(arrayLike/*, mapFn, thisArg */) {
var O = Object(arrayLike)
, C = typeof this == 'function' ? this : Array
, aLen = arguments.length
, mapfn = aLen > 1 ? arguments[1] : undefined
, mapping = mapfn !== undefined
, index = 0
, iterFn = O[Symbol.iterator]
, length, result, step, iterator;
if (mapping) mapfn = mapfn.bind(aLen > 2 ? arguments[2] : undefined);
if (iterFn != undefined && !Array.isArray(arrayLike)) {
for (iterator = iterFn.call(O), result = new C; !(step = iterator.next()).done; index++) {
result[index] = mapping ? iterCall(iterator, mapfn, step.value, index) : step.value;
}
} else {
length = toLength(O.length);
for (result = new C(length); length > index; index++) {
result[index] = mapping ? mapfn(O[index], index) : O[index];
}
}
result.length = index;
return result;
};
}());
}
if (!Array.prototype.find) {
Object.defineProperty(Array.prototype, 'find', {
configurable: true,
writable: true,
enumerable: false,
value: function(predicate) {
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) {
Object.defineProperty(Array.prototype, 'findIndex', {
configurable: true,
writable: true,
enumerable: false,
value: function(predicate) {
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;
}
});
}
} // endif FEATURE_NO_ES2015
if (typeof FEATURE_NO_ES2016 === 'undefined' && !Array.prototype.includes) {
Object.defineProperty(Array.prototype, 'includes', {
configurable: true,
writable: true,
enumerable: false,
value: function(searchElement /*, fromIndex*/ ) {
var O = Object(this);
var len = parseInt(O.length) || 0;
if (len === 0) {
return false;
}
var n = parseInt(arguments[1]) || 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;
}
});
}