Skip to content

Commit e2850aa

Browse files
committed
add indexof polyfill
1 parent 24d8272 commit e2850aa

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

unbxdSearch.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,72 @@
22
var unbxdSearchInit = function(jQuery, Handlebars){
33
window.Unbxd = window.Unbxd || {};
44

5+
// Production steps of ECMA-262, Edition 5, 15.4.4.14
6+
// Reference: http://es5.github.io/#x15.4.4.14
7+
if (!Array.prototype.indexOf) {
8+
Array.prototype.indexOf = function(searchElement, fromIndex) {
9+
10+
var k;
11+
12+
// 1. Let O be the result of calling ToObject passing
13+
// the this value as the argument.
14+
if (this == null) {
15+
throw new TypeError('"this" is null or not defined');
16+
}
17+
18+
var O = Object(this);
19+
20+
// 2. Let lenValue be the result of calling the Get
21+
// internal method of O with the argument "length".
22+
// 3. Let len be ToUint32(lenValue).
23+
var len = O.length >>> 0;
24+
25+
// 4. If len is 0, return -1.
26+
if (len === 0) {
27+
return -1;
28+
}
29+
30+
// 5. If argument fromIndex was passed let n be
31+
// ToInteger(fromIndex); else let n be 0.
32+
var n = +fromIndex || 0;
33+
34+
if (Math.abs(n) === Infinity) {
35+
n = 0;
36+
}
37+
38+
// 6. If n >= len, return -1.
39+
if (n >= len) {
40+
return -1;
41+
}
42+
43+
// 7. If n >= 0, then Let k be n.
44+
// 8. Else, n<0, Let k be len - abs(n).
45+
// If k is less than 0, then let k be 0.
46+
k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
47+
48+
// 9. Repeat, while k < len
49+
while (k < len) {
50+
// a. Let Pk be ToString(k).
51+
// This is implicit for LHS operands of the in operator
52+
// b. Let kPresent be the result of calling the
53+
// HasProperty internal method of O with argument Pk.
54+
// This step can be combined with c
55+
// c. If kPresent is true, then
56+
// i. Let elementK be the result of calling the Get
57+
// internal method of O with the argument ToString(k).
58+
// ii. Let same be the result of applying the
59+
// Strict Equality Comparison Algorithm to
60+
// searchElement and elementK.
61+
// iii. If same is true, return k.
62+
if (k in O && O[k] === searchElement) {
63+
return k;
64+
}
65+
k++;
66+
}
67+
return -1;
68+
};
69+
}
70+
571
/**
672
* Shim for "fixing" IE's lack of support (IE < 9) for applying slice
773
* on host objects like NamedNodeMap, NodeList, and HTMLCollection

0 commit comments

Comments
 (0)