From 2bd05bbfd3c6fe95b558e7a95f198f446890ef26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Mor=C3=B3n=20Zirfas?= Date: Sun, 27 Oct 2019 19:48:49 +0100 Subject: [PATCH] Replace old shim with current from mozilla --- Array/every.js | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/Array/every.js b/Array/every.js index a75c8ac..aa98813 100644 --- a/Array/every.js +++ b/Array/every.js @@ -1,12 +1,10 @@ -/* -https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every -*/ if (!Array.prototype.every) { - Array.prototype.every = function(callback, thisArg) { + Array.prototype.every = function(callbackfn, thisArg) { + 'use strict'; var T, k; - if (this === void 0 || this === null) { - throw new TypeError('Array.prototype.every called on null or undefined'); + if (this == null) { + throw new TypeError('this is null or not defined'); } // 1. Let O be the result of calling ToObject passing the this @@ -18,13 +16,15 @@ if (!Array.prototype.every) { // 3. Let len be ToUint32(lenValue). var len = O.length >>> 0; - // 4. If IsCallable(callback) is false, throw a TypeError exception. - if (callback.__class__ !== 'Function') { - throw new TypeError(callback + ' is not a function'); + // 4. If IsCallable(callbackfn) is false, throw a TypeError exception. + if (typeof callbackfn !== 'function' && Object.prototype.toString.call(callbackfn) !== '[object Function]') { + throw new TypeError(); } // 5. If thisArg was supplied, let T be thisArg; else let T be undefined. - T = (arguments.length > 1) ? thisArg : void 0; + if (arguments.length > 1) { + T = thisArg; + } // 6. Let k be 0. k = 0; @@ -41,15 +41,17 @@ if (!Array.prototype.every) { // This step can be combined with c // c. If kPresent is true, then if (k in O) { - + var testResult; // i. Let kValue be the result of calling the Get internal method // of O with argument Pk. kValue = O[k]; - // ii. Let testResult be the result of calling the Call internal method - // of callback with T as the this value and argument list - // containing kValue, k, and O. - var testResult = callback.call(T, kValue, k, O); + // ii. Let testResult be the result of calling the Call internal method + // of callbackfn with T as the this value if T is not undefined + // else is the result of calling callbackfn + // and argument list containing kValue, k, and O. + if(T) testResult = callbackfn.call(T, kValue, k, O); + else testResult = callbackfn(kValue,k,O) // iii. If ToBoolean(testResult) is false, return false. if (!testResult) { @@ -60,4 +62,4 @@ if (!Array.prototype.every) { } return true; }; -} \ No newline at end of file +}