From a22e24fd99f10fea2e846bb7b32b19da4e678641 Mon Sep 17 00:00:00 2001 From: lucastetreault Date: Fri, 11 Sep 2015 01:20:21 -0600 Subject: [PATCH 1/2] refactor: use isDefined and isUndefined consistently Fix any place that x == undefined or x != undefined to use isUndefined and isDefined instead Closes #4365 --- src/Angular.js | 2 +- src/jqLite.js | 6 +++--- src/ng/cacheFactory.js | 4 ++-- src/ng/compile.js | 4 ++-- src/ng/cookieReader.js | 2 +- src/ng/directive/ngBind.js | 4 ++-- src/ng/directive/ngModel.js | 6 +++--- src/ng/httpBackend.js | 2 +- src/ng/location.js | 6 +++--- src/ng/sce.js | 4 ++-- src/ngAnimate/shared.js | 4 ++-- src/ngCookies/cookieWriter.js | 2 +- src/ngMock/angular-mocks.js | 6 +++--- src/ngResource/resource.js | 2 +- src/ngScenario/Scenario.js | 2 +- test/ng/httpSpec.js | 6 +++--- test/ng/intervalSpec.js | 2 +- test/ng/qSpec.js | 2 +- test/ng/sceSpecs.js | 4 ++-- test/ngCookies/cookieWriterSpec.js | 4 ++-- test/ngScenario/matchersSpec.js | 2 +- 21 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src/Angular.js b/src/Angular.js index 50038de77f75..a15bfec7172d 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -977,7 +977,7 @@ function equals(o1, o2) { for (key in o2) { if (!(key in keySet) && key.charAt(0) !== '$' && - o2[key] !== undefined && + isDefined(o2[key]) && !isFunction(o2[key])) return false; } return true; diff --git a/src/jqLite.js b/src/jqLite.js index 2a36b315a8ce..0629619e5e35 100644 --- a/src/jqLite.js +++ b/src/jqLite.js @@ -450,7 +450,7 @@ function jqLiteInheritedData(element, name, value) { while (element) { for (var i = 0, ii = names.length; i < ii; i++) { - if ((value = jqLite.data(element, names[i])) !== undefined) return value; + if (isDefined(value = jqLite.data(element, names[i]))) return value; } // If dealing with a document fragment node with a host element, and no parent, use the host @@ -694,7 +694,7 @@ forEach({ // in a way that survives minification. // jqLiteEmpty takes no arguments but is a setter. if (fn !== jqLiteEmpty && - (((fn.length == 2 && (fn !== jqLiteHasClass && fn !== jqLiteController)) ? arg1 : arg2) === undefined)) { + (isUndefined((fn.length == 2 && (fn !== jqLiteHasClass && fn !== jqLiteController)) ? arg1 : arg2))) { if (isObject(arg1)) { // we are a write, but the object properties are the key/values @@ -715,7 +715,7 @@ forEach({ // TODO: do we still need this? var value = fn.$dv; // Only if we have $dv do we iterate over all, otherwise it is just the first element. - var jj = (value === undefined) ? Math.min(nodeCount, 1) : nodeCount; + var jj = (isUndefined(value)) ? Math.min(nodeCount, 1) : nodeCount; for (var j = 0; j < jj; j++) { var nodeValue = fn(this[j], arg1, arg2); value = value ? value + nodeValue : nodeValue; diff --git a/src/ng/cacheFactory.js b/src/ng/cacheFactory.js index 377c0c820d54..ad3939879af7 100644 --- a/src/ng/cacheFactory.js +++ b/src/ng/cacheFactory.js @@ -67,10 +67,10 @@ $scope.keys = []; $scope.cache = $cacheFactory('cacheId'); $scope.put = function(key, value) { - if ($scope.cache.get(key) === undefined) { + if (isUndefined($scope.cache.get(key))) { $scope.keys.push(key); } - $scope.cache.put(key, value === undefined ? null : value); + $scope.cache.put(key, isUndefined(value) ? null : value); }; }]); diff --git a/src/ng/compile.js b/src/ng/compile.js index d60c670ee80f..20eb7593d776 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -1165,7 +1165,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { } if (writeAttr !== false) { - if (value === null || value === undefined) { + if (value === null || isUndefined(value)) { this.$$element.removeAttr(attrName); } else { this.$$element.attr(attrName, value); @@ -2131,7 +2131,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { i = 0, ii = directives.length; i < ii; i++) { try { directive = directives[i]; - if ((maxPriority === undefined || maxPriority > directive.priority) && + if ((isUndefined(maxPriority) || maxPriority > directive.priority) && directive.restrict.indexOf(location) != -1) { if (startAttrName) { directive = inherit(directive, {$$start: startAttrName, $$end: endAttrName}); diff --git a/src/ng/cookieReader.js b/src/ng/cookieReader.js index b22d96096893..1a7e52071246 100644 --- a/src/ng/cookieReader.js +++ b/src/ng/cookieReader.js @@ -39,7 +39,7 @@ function $$CookieReader($document) { // the first value that is seen for a cookie is the most // specific one. values for the same cookie name that // follow are for less specific paths. - if (lastCookies[name] === undefined) { + if (isUndefined(lastCookies[name])) { lastCookies[name] = safeDecodeURIComponent(cookie.substring(index + 1)); } } diff --git a/src/ng/directive/ngBind.js b/src/ng/directive/ngBind.js index 37660a75ba52..3c7c65d4cb56 100644 --- a/src/ng/directive/ngBind.js +++ b/src/ng/directive/ngBind.js @@ -60,7 +60,7 @@ var ngBindDirective = ['$compile', function($compile) { $compile.$$addBindingInfo(element, attr.ngBind); element = element[0]; scope.$watch(attr.ngBind, function ngBindWatchAction(value) { - element.textContent = value === undefined ? '' : value; + element.textContent = isUndefined(value) ? '' : value; }); }; } @@ -128,7 +128,7 @@ var ngBindTemplateDirective = ['$interpolate', '$compile', function($interpolate $compile.$$addBindingInfo(element, interpolateFn.expressions); element = element[0]; attr.$observe('ngBindTemplate', function(value) { - element.textContent = value === undefined ? '' : value; + element.textContent = isUndefined(value) ? '' : value; }); }; } diff --git a/src/ng/directive/ngModel.js b/src/ng/directive/ngModel.js index aa3a6f532141..034c386a5527 100644 --- a/src/ng/directive/ngModel.js +++ b/src/ng/directive/ngModel.js @@ -556,7 +556,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$ function processParseErrors() { var errorKey = ctrl.$$parserName || 'parse'; - if (parserValid === undefined) { + if (isUndefined(parserValid)) { setValidity(errorKey, null); } else { if (!parserValid) { @@ -1242,7 +1242,7 @@ var ngModelOptionsDirective = function() { var that = this; this.$options = copy($scope.$eval($attrs.ngModelOptions)); // Allow adding/overriding bound events - if (this.$options.updateOn !== undefined) { + if (isDefined(this.$options.updateOn)) { this.$options.updateOnDefault = false; // extract "default" pseudo-event from list of events that can trigger a model update this.$options.updateOn = trim(this.$options.updateOn.replace(DEFAULT_REGEXP, function() { @@ -1272,7 +1272,7 @@ function addSetValidityMethod(context) { ctrl.$setValidity = setValidity; function setValidity(validationErrorKey, state, controller) { - if (state === undefined) { + if (isUndefined(state)) { createAndSet('$pending', validationErrorKey, controller); } else { unsetAndCleanup('$pending', validationErrorKey, controller); diff --git a/src/ng/httpBackend.js b/src/ng/httpBackend.js index 6f89b284ae6f..b6aa9689796d 100644 --- a/src/ng/httpBackend.js +++ b/src/ng/httpBackend.js @@ -126,7 +126,7 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc function completeRequest(callback, status, response, headersString, statusText) { // cancel timeout and subsequent timeout promise resolution - if (timeoutId !== undefined) { + if (isDefined(timeoutId)) { $browserDefer.cancel(timeoutId); } jsonpDone = xhr = null; diff --git a/src/ng/location.js b/src/ng/location.js index 0412f3d9183f..a904b100b7c6 100644 --- a/src/ng/location.js +++ b/src/ng/location.js @@ -141,14 +141,14 @@ function LocationHtml5Url(appBase, appBaseNoFile, basePrefix) { var appUrl, prevAppUrl; var rewrittenUrl; - if ((appUrl = beginsWith(appBase, url)) !== undefined) { + if (isDefined(appUrl = beginsWith(appBase, url))) { prevAppUrl = appUrl; - if ((appUrl = beginsWith(basePrefix, appUrl)) !== undefined) { + if (isDefined(appUrl = beginsWith(basePrefix, appUrl))) { rewrittenUrl = appBaseNoFile + (beginsWith('/', appUrl) || appUrl); } else { rewrittenUrl = appBase + prevAppUrl; } - } else if ((appUrl = beginsWith(appBaseNoFile, url)) !== undefined) { + } else if (isDefined(appUrl = beginsWith(appBaseNoFile, url))) { rewrittenUrl = appBaseNoFile + appUrl; } else if (appBaseNoFile == url + '/') { rewrittenUrl = appBaseNoFile; diff --git a/src/ng/sce.js b/src/ng/sce.js index 8733f14fc61d..d11419ff0f25 100644 --- a/src/ng/sce.js +++ b/src/ng/sce.js @@ -294,7 +294,7 @@ function $SceDelegateProvider() { 'Attempted to trust a value in invalid context. Context: {0}; Value: {1}', type, trustedValue); } - if (trustedValue === null || trustedValue === undefined || trustedValue === '') { + if (trustedValue === null || isUndefined(trustedValue) || trustedValue === '') { return trustedValue; } // All the current contexts in SCE_CONTEXTS happen to be strings. In order to avoid trusting @@ -349,7 +349,7 @@ function $SceDelegateProvider() { * `$sceDelegate.trustAs`} if valid in this context. Otherwise, throws an exception. */ function getTrusted(type, maybeTrusted) { - if (maybeTrusted === null || maybeTrusted === undefined || maybeTrusted === '') { + if (maybeTrusted === null || isUndefined(maybeTrusted) || maybeTrusted === '') { return maybeTrusted; } var constructor = (byType.hasOwnProperty(type) ? byType[type] : null); diff --git a/src/ngAnimate/shared.js b/src/ngAnimate/shared.js index 51a7f33d6e74..ad58a1506cab 100644 --- a/src/ngAnimate/shared.js +++ b/src/ngAnimate/shared.js @@ -36,7 +36,7 @@ var CSS_PREFIX = '', TRANSITION_PROP, TRANSITIONEND_EVENT, ANIMATION_PROP, ANIMA // Also, the only modern browser that uses vendor prefixes for transitions/keyframes is webkit // therefore there is no reason to test anymore for other vendor prefixes: // http://caniuse.com/#search=transition -if (window.ontransitionend === undefined && window.onwebkittransitionend !== undefined) { +if (isUndefined(window.ontransitionend) && isDefined(window.onwebkittransitionend)) { CSS_PREFIX = '-webkit-'; TRANSITION_PROP = 'WebkitTransition'; TRANSITIONEND_EVENT = 'webkitTransitionEnd transitionend'; @@ -45,7 +45,7 @@ if (window.ontransitionend === undefined && window.onwebkittransitionend !== und TRANSITIONEND_EVENT = 'transitionend'; } -if (window.onanimationend === undefined && window.onwebkitanimationend !== undefined) { +if (isUndefined(window.onanimationend) && isDefined(window.onwebkitanimationend)) { CSS_PREFIX = '-webkit-'; ANIMATION_PROP = 'WebkitAnimation'; ANIMATIONEND_EVENT = 'webkitAnimationEnd animationend'; diff --git a/src/ngCookies/cookieWriter.js b/src/ngCookies/cookieWriter.js index b4f496f825bb..038551a47421 100644 --- a/src/ngCookies/cookieWriter.js +++ b/src/ngCookies/cookieWriter.js @@ -20,7 +20,7 @@ function $$CookieWriter($document, $log, $browser) { options = options || {}; expires = options.expires; path = angular.isDefined(options.path) ? options.path : cookiePath; - if (value === undefined) { + if (isUndefined(value)) { expires = 'Thu, 01 Jan 1970 00:00:00 GMT'; value = ''; } diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js index 579831314de4..9601fd6bd908 100644 --- a/src/ngMock/angular-mocks.js +++ b/src/ngMock/angular-mocks.js @@ -87,7 +87,7 @@ angular.mock.$Browser = function() { if (fn.id === deferId) fnIndex = index; }); - if (fnIndex !== undefined) { + if (isDefined(fnIndex)) { self.deferredFns.splice(fnIndex, 1); return true; } @@ -462,7 +462,7 @@ angular.mock.$IntervalProvider = function() { if (fn.id === promise.$$intervalId) fnIndex = index; }); - if (fnIndex !== undefined) { + if (isDefined(fnIndex)) { repeatFns.splice(fnIndex, 1); } } @@ -504,7 +504,7 @@ angular.mock.$IntervalProvider = function() { if (fn.id === promise.$$intervalId) fnIndex = index; }); - if (fnIndex !== undefined) { + if (isDefined(fnIndex)) { repeatFns[fnIndex].deferred.reject('canceled'); repeatFns.splice(fnIndex, 1); return true; diff --git a/src/ngResource/resource.js b/src/ngResource/resource.js index 4c37ad48ce62..ebdaaa143da8 100644 --- a/src/ngResource/resource.js +++ b/src/ngResource/resource.js @@ -17,7 +17,7 @@ function lookupDottedPath(obj, path) { throw $resourceMinErr('badmember', 'Dotted member path "@{0}" is invalid.', path); } var keys = path.split('.'); - for (var i = 0, ii = keys.length; i < ii && obj !== undefined; i++) { + for (var i = 0, ii = keys.length; i < ii && isDefined(obj); i++) { var key = keys[i]; obj = (obj !== null) ? obj[key] : undefined; } diff --git a/src/ngScenario/Scenario.js b/src/ngScenario/Scenario.js index 9d1e7ca26c17..126a50e94925 100644 --- a/src/ngScenario/Scenario.js +++ b/src/ngScenario/Scenario.js @@ -293,7 +293,7 @@ _jQuery.fn.bindings = function(windowJquery, bindExp) { } function push(value) { - if (value === undefined) { + if (isUndefined(value)) { value = ''; } else if (typeof value !== 'string') { value = angular.toJson(value); diff --git a/test/ng/httpSpec.js b/test/ng/httpSpec.js index 0b872766206f..168305eab34f 100644 --- a/test/ng/httpSpec.js +++ b/test/ng/httpSpec.js @@ -764,8 +764,8 @@ describe('$http', function() { $httpBackend.expect('POST', '/url', 'messageBody', function(headers) { return headers['accept'] == 'Rewritten' && headers['content-type'] == 'Content-Type Rewritten' && - headers['Accept'] === undefined && - headers['Content-Type'] === undefined; + isUndefined(headers['Accept']) && + isUndefined(headers['Content-Type']); }).respond(''); $http({url: '/url', method: 'POST', data: 'messageBody', headers: { @@ -779,7 +779,7 @@ describe('$http', function() { mockedCookies['XSRF-TOKEN'] = 'secret'; $browser.url('http://host.com/base'); $httpBackend.expect('GET', 'http://www.test.com/url', undefined, function(headers) { - return headers['X-XSRF-TOKEN'] === undefined; + return isUndefined(headers['X-XSRF-TOKEN']); }).respond(''); $http({url: 'http://www.test.com/url', method: 'GET', headers: {}}); diff --git a/test/ng/intervalSpec.js b/test/ng/intervalSpec.js index 2bc2de321a18..bbcf3d2f0d88 100644 --- a/test/ng/intervalSpec.js +++ b/test/ng/intervalSpec.js @@ -29,7 +29,7 @@ describe('$interval', function() { if (fn.id === id) fnIndex = index; }); - if (fnIndex !== undefined) { + if (isDefined(fnIndex)) { repeatFns.splice(fnIndex, 1); return true; } diff --git a/test/ng/qSpec.js b/test/ng/qSpec.js index dbafc18fcfca..824728528823 100644 --- a/test/ng/qSpec.js +++ b/test/ng/qSpec.js @@ -51,7 +51,7 @@ describe('q', function() { log.push(logPrefix + '->throw(' + _argToString(returnVal) + ')'); throw returnVal; } else { - if (returnVal === undefined) { + if (isUndefined(returnVal)) { log.push(logPrefix); } else { log.push(logPrefix + '->' + _argToString(returnVal)); diff --git a/test/ng/sceSpecs.js b/test/ng/sceSpecs.js index a35f6f3d83b7..3e1d55e9ae7e 100644 --- a/test/ng/sceSpecs.js +++ b/test/ng/sceSpecs.js @@ -282,10 +282,10 @@ describe('SCE', function() { function runTest(cfg, testFn) { return function() { module(function($sceDelegateProvider) { - if (cfg.whiteList !== undefined) { + if (isDefined(cfg.whiteList)) { $sceDelegateProvider.resourceUrlWhitelist(cfg.whiteList); } - if (cfg.blackList !== undefined) { + if (isDefined(cfg.blackList)) { $sceDelegateProvider.resourceUrlBlacklist(cfg.blackList); } }); diff --git a/test/ngCookies/cookieWriterSpec.js b/test/ngCookies/cookieWriterSpec.js index e94f2b16e7c7..e5986aea785e 100644 --- a/test/ngCookies/cookieWriterSpec.js +++ b/test/ngCookies/cookieWriterSpec.js @@ -138,8 +138,8 @@ describe('cookie options', function() { .reduce(function(prev, value) { var pair = value.split('=', 2); if (pair[0] === key) { - if (prev === undefined) { - return pair[1] === undefined ? true : pair[1]; + if (isUndefined(prev)) { + return isUndefined(pair[1]) ? true : pair[1]; } else { throw 'duplicate key in cookie string'; } diff --git a/test/ngScenario/matchersSpec.js b/test/ngScenario/matchersSpec.js index cecba292690b..d8a62a02d4bd 100644 --- a/test/ngScenario/matchersSpec.js +++ b/test/ngScenario/matchersSpec.js @@ -6,7 +6,7 @@ describe('angular.scenario.matchers', function() { function expectMatcher(value, test) { delete matchers.error; delete matchers.future.value; - if (value !== undefined) { + if (isDefined(value)) { matchers.future.value = value; } test(); From a5f84bfae3966fb705b5d4bbebff655aeadc5811 Mon Sep 17 00:00:00 2001 From: lucastetreault Date: Fri, 11 Sep 2015 22:11:57 -0600 Subject: [PATCH 2/2] refactor: use isDefined and isUndefined consistently Fix any place that x == undefined or x != undefined to use isUndefined and isDefined instead Closes #4365 --- src/ngCookies/cookieWriter.js | 2 +- src/ngMock/angular-mocks.js | 6 +++--- src/ngResource/resource.js | 2 +- src/ngScenario/Scenario.js | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/ngCookies/cookieWriter.js b/src/ngCookies/cookieWriter.js index 038551a47421..1285a346bac7 100644 --- a/src/ngCookies/cookieWriter.js +++ b/src/ngCookies/cookieWriter.js @@ -20,7 +20,7 @@ function $$CookieWriter($document, $log, $browser) { options = options || {}; expires = options.expires; path = angular.isDefined(options.path) ? options.path : cookiePath; - if (isUndefined(value)) { + if (angular.isUndefined(value)) { expires = 'Thu, 01 Jan 1970 00:00:00 GMT'; value = ''; } diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js index 9601fd6bd908..ba023c79475b 100644 --- a/src/ngMock/angular-mocks.js +++ b/src/ngMock/angular-mocks.js @@ -87,7 +87,7 @@ angular.mock.$Browser = function() { if (fn.id === deferId) fnIndex = index; }); - if (isDefined(fnIndex)) { + if (angular.isDefined(fnIndex)) { self.deferredFns.splice(fnIndex, 1); return true; } @@ -462,7 +462,7 @@ angular.mock.$IntervalProvider = function() { if (fn.id === promise.$$intervalId) fnIndex = index; }); - if (isDefined(fnIndex)) { + if (angular.isDefined(fnIndex)) { repeatFns.splice(fnIndex, 1); } } @@ -504,7 +504,7 @@ angular.mock.$IntervalProvider = function() { if (fn.id === promise.$$intervalId) fnIndex = index; }); - if (isDefined(fnIndex)) { + if (angular.isDefined(fnIndex)) { repeatFns[fnIndex].deferred.reject('canceled'); repeatFns.splice(fnIndex, 1); return true; diff --git a/src/ngResource/resource.js b/src/ngResource/resource.js index ebdaaa143da8..34ba768716b9 100644 --- a/src/ngResource/resource.js +++ b/src/ngResource/resource.js @@ -17,7 +17,7 @@ function lookupDottedPath(obj, path) { throw $resourceMinErr('badmember', 'Dotted member path "@{0}" is invalid.', path); } var keys = path.split('.'); - for (var i = 0, ii = keys.length; i < ii && isDefined(obj); i++) { + for (var i = 0, ii = keys.length; i < ii && angular.isDefined(obj); i++) { var key = keys[i]; obj = (obj !== null) ? obj[key] : undefined; } diff --git a/src/ngScenario/Scenario.js b/src/ngScenario/Scenario.js index 126a50e94925..70436e479751 100644 --- a/src/ngScenario/Scenario.js +++ b/src/ngScenario/Scenario.js @@ -293,7 +293,7 @@ _jQuery.fn.bindings = function(windowJquery, bindExp) { } function push(value) { - if (isUndefined(value)) { + if (angular.isUndefined(value)) { value = ''; } else if (typeof value !== 'string') { value = angular.toJson(value);