Skip to content

Commit caeb74c

Browse files
committed
add polyfill for slice
1 parent 2b62e06 commit caeb74c

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

unbxdSearch.js

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

5+
/**
6+
* Shim for "fixing" IE's lack of support (IE < 9) for applying slice
7+
* on host objects like NamedNodeMap, NodeList, and HTMLCollection
8+
* (technically, since host objects have been implementation-dependent,
9+
* at least before ES6, IE hasn't needed to work this way).
10+
* Also works on strings, fixes IE < 9 to allow an explicit undefined
11+
* for the 2nd argument (as in Firefox), and prevents errors when
12+
* called on other DOM objects.
13+
*/
14+
(function () {
15+
'use strict';
16+
var _slice = Array.prototype.slice;
17+
18+
try {
19+
// Can't be used with DOM elements in IE < 9
20+
_slice.call(document.documentElement);
21+
} catch (e) { // Fails in IE < 9
22+
// This will work for genuine arrays, array-like objects,
23+
// NamedNodeMap (attributes, entities, notations),
24+
// NodeList (e.g., getElementsByTagName), HTMLCollection (e.g., childNodes),
25+
// and will not fail on other DOM objects (as do DOM elements in IE < 9)
26+
Array.prototype.slice = function(begin, end) {
27+
// IE < 9 gets unhappy with an undefined end argument
28+
end = (typeof end !== 'undefined') ? end : this.length;
29+
30+
// For native Array objects, we use the native slice function
31+
if (Object.prototype.toString.call(this) === '[object Array]'){
32+
return _slice.call(this, begin, end);
33+
}
34+
35+
// For array like object we handle it ourselves.
36+
var i, cloned = [],
37+
size, len = this.length;
38+
39+
// Handle negative value for "begin"
40+
var start = begin || 0;
41+
start = (start >= 0) ? start : Math.max(0, len + start);
42+
43+
// Handle negative value for "end"
44+
var upTo = (typeof end == 'number') ? Math.min(end, len) : len;
45+
if (end < 0) {
46+
upTo = len + end;
47+
}
48+
49+
// Actual expected size of the slice
50+
size = upTo - start;
51+
52+
if (size > 0) {
53+
cloned = new Array(size);
54+
if (this.charAt) {
55+
for (i = 0; i < size; i++) {
56+
cloned[i] = this.charAt(start + i);
57+
}
58+
} else {
59+
for (i = 0; i < size; i++) {
60+
cloned[i] = this[start + i];
61+
}
62+
}
63+
}
64+
65+
return cloned;
66+
};
67+
}
68+
}());
69+
570
if (!Function.prototype.bind) {
671
Function.prototype.bind = function (oThis) {
772
if (typeof this !== "function") {

0 commit comments

Comments
 (0)