From 9b26513f605a9bfd6f8d7d37d53c31059a6a6c32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rouven=20We=C3=9Fling?= Date: Fri, 29 Aug 2014 21:22:43 +0200 Subject: [PATCH] refactor(indexOf) use Array.prototype.indexOf exclusively Replaces helper functions with the native ES5 method --- src/.jshintrc | 1 - src/Angular.js | 16 +++------------- src/ng/directive/form.js | 2 +- src/ng/http.js | 2 +- src/ng/rootScope.js | 2 +- src/ngScenario/.jshintrc | 1 - src/ngScenario/Scenario.js | 2 +- src/ngScenario/browserTrigger.js | 13 +------------ test/.jshintrc | 1 - test/helpers/matchers.js | 2 +- test/helpers/testabilityPatch.js | 2 +- test/ng/httpBackendSpec.js | 4 ++-- 12 files changed, 12 insertions(+), 36 deletions(-) diff --git a/src/.jshintrc b/src/.jshintrc index e26593342df5..99ea5a68de32 100644 --- a/src/.jshintrc +++ b/src/.jshintrc @@ -58,7 +58,6 @@ "map": false, "size": false, "includes": false, - "indexOf": false, "arrayRemove": false, "isLeafNode": false, "copy": false, diff --git a/src/Angular.js b/src/Angular.js index a90d7ee7b35b..5177ece218c2 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -53,7 +53,6 @@ map: true, size: true, includes: true, - indexOf: true, arrayRemove: true, isLeafNode: true, copy: true, @@ -651,20 +650,11 @@ function size(obj, ownPropsOnly) { function includes(array, obj) { - return indexOf(array, obj) != -1; -} - -function indexOf(array, obj) { - if (array.indexOf) return array.indexOf(obj); - - for (var i = 0; i < array.length; i++) { - if (obj === array[i]) return i; - } - return -1; + return Array.prototype.indexOf.call(array, obj) != -1; } function arrayRemove(array, value) { - var index = indexOf(array, value); + var index = array.indexOf(value); if (index >=0) array.splice(index, 1); return value; @@ -769,7 +759,7 @@ function copy(source, destination, stackSource, stackDest) { stackDest = stackDest || []; if (isObject(source)) { - var index = indexOf(stackSource, source); + var index = stackSource.indexOf(source); if (index !== -1) return stackDest[index]; stackSource.push(source); diff --git a/src/ng/directive/form.js b/src/ng/directive/form.js index 335ac606582f..9133f524e07c 100644 --- a/src/ng/directive/form.js +++ b/src/ng/directive/form.js @@ -192,7 +192,7 @@ function FormController(element, attrs, $scope, $animate) { var pendingChange, pending = form.$pending && form.$pending[validationToken]; if (pending) { - pendingChange = indexOf(pending, control) >= 0; + pendingChange = pending.indexOf(control) >= 0; if (pendingChange) { arrayRemove(pending, control); pendingCount--; diff --git a/src/ng/http.js b/src/ng/http.js index 1fdc615f2c21..29cda1850b31 100644 --- a/src/ng/http.js +++ b/src/ng/http.js @@ -1008,7 +1008,7 @@ function $HttpProvider() { function removePendingReq() { - var idx = indexOf($http.pendingRequests, config); + var idx = $http.pendingRequests.indexOf(config); if (idx !== -1) $http.pendingRequests.splice(idx, 1); } } diff --git a/src/ng/rootScope.js b/src/ng/rootScope.js index 8e36208ed3a7..5e566f708646 100644 --- a/src/ng/rootScope.js +++ b/src/ng/rootScope.js @@ -1079,7 +1079,7 @@ function $RootScopeProvider(){ var self = this; return function() { - namedListeners[indexOf(namedListeners, listener)] = null; + namedListeners[namedListeners.indexOf(listener)] = null; decrementListenerCount(self, 1, name); }; }, diff --git a/src/ngScenario/.jshintrc b/src/ngScenario/.jshintrc index 59cdd02476c0..ad2cabdd21ee 100644 --- a/src/ngScenario/.jshintrc +++ b/src/ngScenario/.jshintrc @@ -9,7 +9,6 @@ "browserTrigger": false, "console": false, "alert": false, - "indexOf": false, "_jQuery": false, "angularInit": false, "formatException": false, diff --git a/src/ngScenario/Scenario.js b/src/ngScenario/Scenario.js index 7ee8030d404c..ccd3d2cb5704 100644 --- a/src/ngScenario/Scenario.js +++ b/src/ngScenario/Scenario.js @@ -112,7 +112,7 @@ angular.scenario.setUpAndRun = function(config) { } angular.forEach(angular.scenario.output, function(fn, name) { - if (!output.length || indexOf(output,name) != -1) { + if (!output.length || output.indexOf(name) != -1) { var context = body.append('
').find('div:last'); context.attr('id', name); fn.call({}, context, $runner, objModel); diff --git a/src/ngScenario/browserTrigger.js b/src/ngScenario/browserTrigger.js index e3450a8fcf8f..478853e28512 100644 --- a/src/ngScenario/browserTrigger.js +++ b/src/ngScenario/browserTrigger.js @@ -3,17 +3,6 @@ (function() { var msie = parseInt((/msie (\d+)/.exec(navigator.userAgent.toLowerCase()) || [])[1], 10); - function indexOf(array, obj) { - if (array.indexOf) return array.indexOf(obj); - - for ( var i = 0; i < array.length; i++) { - if (obj === array[i]) return i; - } - return -1; - } - - - /** * Triggers a browser event. Attempts to choose the right event if one is * not specified. @@ -60,7 +49,7 @@ keys = keys || []; function pressed(key) { - return indexOf(keys, key) !== -1; + return keys.indexOf(key) !== -1; } if (msie < 9) { diff --git a/test/.jshintrc b/test/.jshintrc index 48fbe95e02e9..9ce1b57a4a7c 100644 --- a/test/.jshintrc +++ b/test/.jshintrc @@ -56,7 +56,6 @@ "map": false, "size": false, "includes": false, - "indexOf": false, "arrayRemove": false, "isLeafNode": false, "copy": false, diff --git a/test/helpers/matchers.js b/test/helpers/matchers.js index 2cb34c6872ab..95393a91ffb7 100644 --- a/test/helpers/matchers.js +++ b/test/helpers/matchers.js @@ -167,7 +167,7 @@ beforeEach(function() { toBeOneOf: function() { - return indexOf(arguments, this.actual) !== -1; + return Array.prototype.indexOf.call(arguments, this.actual) !== -1; }, toHaveClass: function(clazz) { diff --git a/test/helpers/testabilityPatch.js b/test/helpers/testabilityPatch.js index 8aac59dfaf7d..31a5a63f76b1 100644 --- a/test/helpers/testabilityPatch.js +++ b/test/helpers/testabilityPatch.js @@ -222,7 +222,7 @@ function sortedHtml(element, showNgClass) { var value = node.style[css]; if (isString(value) && isString(css) && css != 'cssText' && value && (1*css != css)) { var text = lowercase(css + ': ' + value); - if (value != 'false' && indexOf(style, text) == -1) { + if (value != 'false' && style.indexOf(text) == -1) { style.push(text); } } diff --git a/test/ng/httpBackendSpec.js b/test/ng/httpBackendSpec.js index 461c394c882c..c1c2ec4f0320 100644 --- a/test/ng/httpBackendSpec.js +++ b/test/ng/httpBackendSpec.js @@ -25,7 +25,7 @@ describe('$httpBackend', function() { while (len--) fakeTimeout.fns.shift()(); }; fakeTimeout.cancel = function(id) { - var i = indexOf(fakeTimeout.ids, id); + var i = fakeTimeout.ids.indexOf(id); if (i >= 0) { fakeTimeout.fns.splice(i, 1); fakeTimeout.delays.splice(i, 1); @@ -50,7 +50,7 @@ describe('$httpBackend', function() { fakeDocument.$$scripts.push(script); }), removeChild: jasmine.createSpy('body.removeChild').andCallFake(function(script) { - var index = indexOf(fakeDocument.$$scripts, script); + var index = fakeDocument.$$scripts.indexOf(script); if (index != -1) { fakeDocument.$$scripts.splice(index, 1); }