Skip to content

Commit 24cd700

Browse files
lucastetreaultpetebacondarwin
authored andcommitted
refactor(*): use isDefined and isUndefined consistently
Fix any place that compares with `undefined` to use `isUndefined` and `isDefined` instead. Closes angular#4365 Closes angular#12831
1 parent e46ab43 commit 24cd700

22 files changed

+39
-39
lines changed

src/Angular.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,7 @@ function equals(o1, o2) {
977977
for (key in o2) {
978978
if (!(key in keySet) &&
979979
key.charAt(0) !== '$' &&
980-
o2[key] !== undefined &&
980+
isDefined(o2[key]) &&
981981
!isFunction(o2[key])) return false;
982982
}
983983
return true;

src/jqLite.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ function jqLiteInheritedData(element, name, value) {
450450

451451
while (element) {
452452
for (var i = 0, ii = names.length; i < ii; i++) {
453-
if ((value = jqLite.data(element, names[i])) !== undefined) return value;
453+
if (isDefined(value = jqLite.data(element, names[i]))) return value;
454454
}
455455

456456
// If dealing with a document fragment node with a host element, and no parent, use the host
@@ -694,7 +694,7 @@ forEach({
694694
// in a way that survives minification.
695695
// jqLiteEmpty takes no arguments but is a setter.
696696
if (fn !== jqLiteEmpty &&
697-
(((fn.length == 2 && (fn !== jqLiteHasClass && fn !== jqLiteController)) ? arg1 : arg2) === undefined)) {
697+
(isUndefined((fn.length == 2 && (fn !== jqLiteHasClass && fn !== jqLiteController)) ? arg1 : arg2))) {
698698
if (isObject(arg1)) {
699699

700700
// we are a write, but the object properties are the key/values
@@ -715,7 +715,7 @@ forEach({
715715
// TODO: do we still need this?
716716
var value = fn.$dv;
717717
// Only if we have $dv do we iterate over all, otherwise it is just the first element.
718-
var jj = (value === undefined) ? Math.min(nodeCount, 1) : nodeCount;
718+
var jj = (isUndefined(value)) ? Math.min(nodeCount, 1) : nodeCount;
719719
for (var j = 0; j < jj; j++) {
720720
var nodeValue = fn(this[j], arg1, arg2);
721721
value = value ? value + nodeValue : nodeValue;

src/ng/cacheFactory.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@
6767
$scope.keys = [];
6868
$scope.cache = $cacheFactory('cacheId');
6969
$scope.put = function(key, value) {
70-
if ($scope.cache.get(key) === undefined) {
70+
if (isUndefined($scope.cache.get(key))) {
7171
$scope.keys.push(key);
7272
}
73-
$scope.cache.put(key, value === undefined ? null : value);
73+
$scope.cache.put(key, isUndefined(value) ? null : value);
7474
};
7575
}]);
7676
</file>

src/ng/compile.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1165,7 +1165,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
11651165
}
11661166

11671167
if (writeAttr !== false) {
1168-
if (value === null || value === undefined) {
1168+
if (value === null || isUndefined(value)) {
11691169
this.$$element.removeAttr(attrName);
11701170
} else {
11711171
this.$$element.attr(attrName, value);
@@ -2131,7 +2131,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
21312131
i = 0, ii = directives.length; i < ii; i++) {
21322132
try {
21332133
directive = directives[i];
2134-
if ((maxPriority === undefined || maxPriority > directive.priority) &&
2134+
if ((isUndefined(maxPriority) || maxPriority > directive.priority) &&
21352135
directive.restrict.indexOf(location) != -1) {
21362136
if (startAttrName) {
21372137
directive = inherit(directive, {$$start: startAttrName, $$end: endAttrName});

src/ng/cookieReader.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function $$CookieReader($document) {
3939
// the first value that is seen for a cookie is the most
4040
// specific one. values for the same cookie name that
4141
// follow are for less specific paths.
42-
if (lastCookies[name] === undefined) {
42+
if (isUndefined(lastCookies[name])) {
4343
lastCookies[name] = safeDecodeURIComponent(cookie.substring(index + 1));
4444
}
4545
}

src/ng/directive/ngBind.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ var ngBindDirective = ['$compile', function($compile) {
6060
$compile.$$addBindingInfo(element, attr.ngBind);
6161
element = element[0];
6262
scope.$watch(attr.ngBind, function ngBindWatchAction(value) {
63-
element.textContent = value === undefined ? '' : value;
63+
element.textContent = isUndefined(value) ? '' : value;
6464
});
6565
};
6666
}
@@ -128,7 +128,7 @@ var ngBindTemplateDirective = ['$interpolate', '$compile', function($interpolate
128128
$compile.$$addBindingInfo(element, interpolateFn.expressions);
129129
element = element[0];
130130
attr.$observe('ngBindTemplate', function(value) {
131-
element.textContent = value === undefined ? '' : value;
131+
element.textContent = isUndefined(value) ? '' : value;
132132
});
133133
};
134134
}

src/ng/directive/ngModel.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
556556

557557
function processParseErrors() {
558558
var errorKey = ctrl.$$parserName || 'parse';
559-
if (parserValid === undefined) {
559+
if (isUndefined(parserValid)) {
560560
setValidity(errorKey, null);
561561
} else {
562562
if (!parserValid) {
@@ -1242,7 +1242,7 @@ var ngModelOptionsDirective = function() {
12421242
var that = this;
12431243
this.$options = copy($scope.$eval($attrs.ngModelOptions));
12441244
// Allow adding/overriding bound events
1245-
if (this.$options.updateOn !== undefined) {
1245+
if (isDefined(this.$options.updateOn)) {
12461246
this.$options.updateOnDefault = false;
12471247
// extract "default" pseudo-event from list of events that can trigger a model update
12481248
this.$options.updateOn = trim(this.$options.updateOn.replace(DEFAULT_REGEXP, function() {
@@ -1272,7 +1272,7 @@ function addSetValidityMethod(context) {
12721272
ctrl.$setValidity = setValidity;
12731273

12741274
function setValidity(validationErrorKey, state, controller) {
1275-
if (state === undefined) {
1275+
if (isUndefined(state)) {
12761276
createAndSet('$pending', validationErrorKey, controller);
12771277
} else {
12781278
unsetAndCleanup('$pending', validationErrorKey, controller);

src/ng/httpBackend.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
126126

127127
function completeRequest(callback, status, response, headersString, statusText) {
128128
// cancel timeout and subsequent timeout promise resolution
129-
if (timeoutId !== undefined) {
129+
if (isDefined(timeoutId)) {
130130
$browserDefer.cancel(timeoutId);
131131
}
132132
jsonpDone = xhr = null;

src/ng/location.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,14 @@ function LocationHtml5Url(appBase, appBaseNoFile, basePrefix) {
141141
var appUrl, prevAppUrl;
142142
var rewrittenUrl;
143143

144-
if ((appUrl = beginsWith(appBase, url)) !== undefined) {
144+
if (isDefined(appUrl = beginsWith(appBase, url))) {
145145
prevAppUrl = appUrl;
146-
if ((appUrl = beginsWith(basePrefix, appUrl)) !== undefined) {
146+
if (isDefined(appUrl = beginsWith(basePrefix, appUrl))) {
147147
rewrittenUrl = appBaseNoFile + (beginsWith('/', appUrl) || appUrl);
148148
} else {
149149
rewrittenUrl = appBase + prevAppUrl;
150150
}
151-
} else if ((appUrl = beginsWith(appBaseNoFile, url)) !== undefined) {
151+
} else if (isDefined(appUrl = beginsWith(appBaseNoFile, url))) {
152152
rewrittenUrl = appBaseNoFile + appUrl;
153153
} else if (appBaseNoFile == url + '/') {
154154
rewrittenUrl = appBaseNoFile;

src/ng/sce.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ function $SceDelegateProvider() {
294294
'Attempted to trust a value in invalid context. Context: {0}; Value: {1}',
295295
type, trustedValue);
296296
}
297-
if (trustedValue === null || trustedValue === undefined || trustedValue === '') {
297+
if (trustedValue === null || isUndefined(trustedValue) || trustedValue === '') {
298298
return trustedValue;
299299
}
300300
// All the current contexts in SCE_CONTEXTS happen to be strings. In order to avoid trusting
@@ -349,7 +349,7 @@ function $SceDelegateProvider() {
349349
* `$sceDelegate.trustAs`} if valid in this context. Otherwise, throws an exception.
350350
*/
351351
function getTrusted(type, maybeTrusted) {
352-
if (maybeTrusted === null || maybeTrusted === undefined || maybeTrusted === '') {
352+
if (maybeTrusted === null || isUndefined(maybeTrusted) || maybeTrusted === '') {
353353
return maybeTrusted;
354354
}
355355
var constructor = (byType.hasOwnProperty(type) ? byType[type] : null);

src/ngAnimate/shared.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var CSS_PREFIX = '', TRANSITION_PROP, TRANSITIONEND_EVENT, ANIMATION_PROP, ANIMA
3636
// Also, the only modern browser that uses vendor prefixes for transitions/keyframes is webkit
3737
// therefore there is no reason to test anymore for other vendor prefixes:
3838
// http://caniuse.com/#search=transition
39-
if (window.ontransitionend === undefined && window.onwebkittransitionend !== undefined) {
39+
if (isUndefined(window.ontransitionend) && isDefined(window.onwebkittransitionend)) {
4040
CSS_PREFIX = '-webkit-';
4141
TRANSITION_PROP = 'WebkitTransition';
4242
TRANSITIONEND_EVENT = 'webkitTransitionEnd transitionend';
@@ -45,7 +45,7 @@ if (window.ontransitionend === undefined && window.onwebkittransitionend !== und
4545
TRANSITIONEND_EVENT = 'transitionend';
4646
}
4747

48-
if (window.onanimationend === undefined && window.onwebkitanimationend !== undefined) {
48+
if (isUndefined(window.onanimationend) && isDefined(window.onwebkitanimationend)) {
4949
CSS_PREFIX = '-webkit-';
5050
ANIMATION_PROP = 'WebkitAnimation';
5151
ANIMATIONEND_EVENT = 'webkitAnimationEnd animationend';

src/ngCookies/cookieWriter.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function $$CookieWriter($document, $log, $browser) {
2020
options = options || {};
2121
expires = options.expires;
2222
path = angular.isDefined(options.path) ? options.path : cookiePath;
23-
if (value === undefined) {
23+
if (angular.isUndefined(value)) {
2424
expires = 'Thu, 01 Jan 1970 00:00:00 GMT';
2525
value = '';
2626
}

src/ngMock/angular-mocks.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ angular.mock.$Browser = function() {
8787
if (fn.id === deferId) fnIndex = index;
8888
});
8989

90-
if (fnIndex !== undefined) {
90+
if (angular.isDefined(fnIndex)) {
9191
self.deferredFns.splice(fnIndex, 1);
9292
return true;
9393
}
@@ -462,7 +462,7 @@ angular.mock.$IntervalProvider = function() {
462462
if (fn.id === promise.$$intervalId) fnIndex = index;
463463
});
464464

465-
if (fnIndex !== undefined) {
465+
if (angular.isDefined(fnIndex)) {
466466
repeatFns.splice(fnIndex, 1);
467467
}
468468
}
@@ -504,7 +504,7 @@ angular.mock.$IntervalProvider = function() {
504504
if (fn.id === promise.$$intervalId) fnIndex = index;
505505
});
506506

507-
if (fnIndex !== undefined) {
507+
if (angular.isDefined(fnIndex)) {
508508
repeatFns[fnIndex].deferred.reject('canceled');
509509
repeatFns.splice(fnIndex, 1);
510510
return true;

src/ngResource/resource.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function lookupDottedPath(obj, path) {
1717
throw $resourceMinErr('badmember', 'Dotted member path "@{0}" is invalid.', path);
1818
}
1919
var keys = path.split('.');
20-
for (var i = 0, ii = keys.length; i < ii && obj !== undefined; i++) {
20+
for (var i = 0, ii = keys.length; i < ii && angular.isDefined(obj); i++) {
2121
var key = keys[i];
2222
obj = (obj !== null) ? obj[key] : undefined;
2323
}

src/ngScenario/Scenario.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ _jQuery.fn.bindings = function(windowJquery, bindExp) {
293293
}
294294

295295
function push(value) {
296-
if (value === undefined) {
296+
if (angular.isUndefined(value)) {
297297
value = '';
298298
} else if (typeof value !== 'string') {
299299
value = angular.toJson(value);

src/stringify.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function serializeObject(obj) {
2020
function toDebugString(obj) {
2121
if (typeof obj === 'function') {
2222
return obj.toString().replace(/ \{[\s\S]*$/, '');
23-
} else if (typeof obj === 'undefined') {
23+
} else if (isUndefined(obj)) {
2424
return 'undefined';
2525
} else if (typeof obj !== 'string') {
2626
return serializeObject(obj);

test/ng/httpSpec.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -764,8 +764,8 @@ describe('$http', function() {
764764
$httpBackend.expect('POST', '/url', 'messageBody', function(headers) {
765765
return headers['accept'] == 'Rewritten' &&
766766
headers['content-type'] == 'Content-Type Rewritten' &&
767-
headers['Accept'] === undefined &&
768-
headers['Content-Type'] === undefined;
767+
isUndefined(headers['Accept']) &&
768+
isUndefined(headers['Content-Type']);
769769
}).respond('');
770770

771771
$http({url: '/url', method: 'POST', data: 'messageBody', headers: {
@@ -779,7 +779,7 @@ describe('$http', function() {
779779
mockedCookies['XSRF-TOKEN'] = 'secret';
780780
$browser.url('http://host.com/base');
781781
$httpBackend.expect('GET', 'http://www.test.com/url', undefined, function(headers) {
782-
return headers['X-XSRF-TOKEN'] === undefined;
782+
return isUndefined(headers['X-XSRF-TOKEN']);
783783
}).respond('');
784784

785785
$http({url: 'http://www.test.com/url', method: 'GET', headers: {}});

test/ng/intervalSpec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('$interval', function() {
2929
if (fn.id === id) fnIndex = index;
3030
});
3131

32-
if (fnIndex !== undefined) {
32+
if (isDefined(fnIndex)) {
3333
repeatFns.splice(fnIndex, 1);
3434
return true;
3535
}

test/ng/qSpec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('q', function() {
5151
log.push(logPrefix + '->throw(' + _argToString(returnVal) + ')');
5252
throw returnVal;
5353
} else {
54-
if (returnVal === undefined) {
54+
if (isUndefined(returnVal)) {
5555
log.push(logPrefix);
5656
} else {
5757
log.push(logPrefix + '->' + _argToString(returnVal));

test/ng/sceSpecs.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,10 @@ describe('SCE', function() {
282282
function runTest(cfg, testFn) {
283283
return function() {
284284
module(function($sceDelegateProvider) {
285-
if (cfg.whiteList !== undefined) {
285+
if (isDefined(cfg.whiteList)) {
286286
$sceDelegateProvider.resourceUrlWhitelist(cfg.whiteList);
287287
}
288-
if (cfg.blackList !== undefined) {
288+
if (isDefined(cfg.blackList)) {
289289
$sceDelegateProvider.resourceUrlBlacklist(cfg.blackList);
290290
}
291291
});

test/ngCookies/cookieWriterSpec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ describe('cookie options', function() {
138138
.reduce(function(prev, value) {
139139
var pair = value.split('=', 2);
140140
if (pair[0] === key) {
141-
if (prev === undefined) {
142-
return pair[1] === undefined ? true : pair[1];
141+
if (isUndefined(prev)) {
142+
return isUndefined(pair[1]) ? true : pair[1];
143143
} else {
144144
throw 'duplicate key in cookie string';
145145
}

test/ngScenario/matchersSpec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ describe('angular.scenario.matchers', function() {
66
function expectMatcher(value, test) {
77
delete matchers.error;
88
delete matchers.future.value;
9-
if (value !== undefined) {
9+
if (isDefined(value)) {
1010
matchers.future.value = value;
1111
}
1212
test();

0 commit comments

Comments
 (0)