Update indexOf polyfill
Updated from the same source as the original comment. If IE8 isn’t supported then this can be removed.
This commit is contained in:
parent
4efabef8f2
commit
96bf38757d
|
@ -1,40 +1,28 @@
|
||||||
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf
|
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf
|
||||||
if (!Array.prototype.indexOf) {
|
if (!Array.prototype.indexOf) {
|
||||||
Array.prototype.indexOf = function(searchElement /*, fromIndex */) {
|
Array.prototype.indexOf = function (searchElement, fromIndex) {
|
||||||
"use strict";
|
if ( this === undefined || this === null ) {
|
||||||
|
throw new TypeError( '"this" is null or not defined' );
|
||||||
if (this === void 0 || this === null) {
|
|
||||||
throw new TypeError();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var t = Object(this);
|
var length = this.length >>> 0; // Hack to convert object.length to a UInt32
|
||||||
var len = t.length >>> 0;
|
|
||||||
|
|
||||||
if (len === 0) {
|
fromIndex = +fromIndex || 0;
|
||||||
return -1;
|
|
||||||
|
if (Math.abs(fromIndex) === Infinity) {
|
||||||
|
fromIndex = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
var n = 0;
|
if (fromIndex < 0) {
|
||||||
if (arguments.length > 0) {
|
fromIndex += length;
|
||||||
n = Number(arguments[1]);
|
if (fromIndex < 0) {
|
||||||
if (n !== n) { // shortcut for verifying if it's NaN
|
fromIndex = 0;
|
||||||
n = 0;
|
|
||||||
} else if (n !== 0 && n !== (Infinity) && n !== -(Infinity)) {
|
|
||||||
n = (n > 0 || -1) * Math.floor(Math.abs(n));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (n >= len) {
|
for (;fromIndex < length; fromIndex++) {
|
||||||
return -1;
|
if (this[fromIndex] === searchElement) {
|
||||||
}
|
return fromIndex;
|
||||||
|
|
||||||
var k = n >= 0
|
|
||||||
? n
|
|
||||||
: Math.max(len - Math.abs(n), 0);
|
|
||||||
|
|
||||||
for (; k < len; k++) {
|
|
||||||
if (k in t && t[k] === searchElement) {
|
|
||||||
return k;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue