Skip to content

Commit

Permalink
Compatibility with IE11 (#592)
Browse files Browse the repository at this point in the history
I have some Problems with the old implementation under IE11, it throw Exceptions and/or produce unwanted behavior in other JS (eg JQuery).
  • Loading branch information
fglueck authored Mar 2, 2021
1 parent 72a9700 commit 5d63790
Showing 1 changed file with 42 additions and 19 deletions.
61 changes: 42 additions & 19 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,47 @@
if (!Array.prototype.find) {
Array.prototype.find = 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
}
Object.defineProperty(Array.prototype, 'find', {
value: function(predicate) {
// 1. Let O be ? ToObject(this value).
if (this == null) {
throw TypeError('"this" is null or not defined');
}

var o = Object(this);

// 2. Let len be ? ToLength(? Get(O, "length")).
var len = o.length >>> 0;

// 3. If IsCallable(predicate) is false, throw a TypeError exception.
if (typeof predicate !== 'function') {
throw TypeError('predicate must be a function');
}

// 4. If thisArg was supplied, let T be thisArg; else let T be undefined.
var thisArg = arguments[1];

// 5. Let k be 0.
var k = 0;

// 6. Repeat, while k < len
while (k < len) {
// a. Let Pk be ! ToString(k).
// b. Let kValue be ? Get(O, Pk).
// c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
// d. If testResult is true, return kValue.
var kValue = o[k];
if (predicate.call(thisArg, kValue, k, o)) {
return kValue;
}
return undefined
}
// e. Increase k by 1.
k++;
}

// 7. Return undefined.
return undefined;
},
configurable: true,
writable: true
});
}

if (typeof window !== 'undefined' && typeof window.CustomEvent !== "function") {
Expand Down

0 comments on commit 5d63790

Please sign in to comment.