Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit aa306c1

Browse files
PatrickJSNarretz
authored andcommitted
refactor(*): introduce isNumberNaN
window.isNaN(‘lol’); //=> true Number.isNaN(‘lol’); //=> false isNaN converts it’s arguments into a Number before checking if it’s NaN. In various places in the code base, we are checking if a variable is a Number and NaN (or not), so this can be simplified with this new method (which is not exported on the global Angular object). Closes #11242
1 parent 9fbad3c commit aa306c1

File tree

10 files changed

+22
-15
lines changed

10 files changed

+22
-15
lines changed

src/.eslintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"isObject": false,
4646
"isString": false,
4747
"isNumber": false,
48+
"isNumberNaN": false,
4849
"isDate": false,
4950
"isArray": false,
5051
"isFunction": false,

src/Angular.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
isBlankObject,
4141
isString,
4242
isNumber,
43+
isNumberNaN,
4344
isDate,
4445
isArray,
4546
isFunction,
@@ -412,6 +413,11 @@ function toInt(str) {
412413
return parseInt(str, 10);
413414
}
414415

416+
var isNumberNaN = Number.isNaN || function isNumberNaN(num) {
417+
// eslint-disable-next-line no-self-compare
418+
return num !== num;
419+
};
420+
415421

416422
function inherit(parent, extra) {
417423
return extend(Object.create(parent), extra);
@@ -1276,7 +1282,7 @@ function timezoneToOffset(timezone, fallback) {
12761282
// IE/Edge do not "understand" colon (`:`) in timezone
12771283
timezone = timezone.replace(ALL_COLONS, '');
12781284
var requestedTimezoneOffset = Date.parse('Jan 01, 1970 00:00:00 ' + timezone) / 60000;
1279-
return isNaN(requestedTimezoneOffset) ? fallback : requestedTimezoneOffset;
1285+
return isNumberNaN(requestedTimezoneOffset) ? fallback : requestedTimezoneOffset;
12801286
}
12811287

12821288

src/ng/directive/input.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,7 +1526,7 @@ function parseNumberAttrVal(val) {
15261526
if (isDefined(val) && !isNumber(val)) {
15271527
val = parseFloat(val);
15281528
}
1529-
return isNumber(val) && !isNaN(val) ? val : undefined;
1529+
return !isNumberNaN(val) ? val : undefined;
15301530
}
15311531

15321532
function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
@@ -1650,7 +1650,7 @@ function rangeInputType(scope, element, attr, ctrl, $sniffer, $browser) {
16501650
function minChange(val) {
16511651
minVal = parseNumberAttrVal(val);
16521652
// ignore changes before model is initialized
1653-
if (isNumber(ctrl.$modelValue) && isNaN(ctrl.$modelValue)) {
1653+
if (isNumberNaN(ctrl.$modelValue)) {
16541654
return;
16551655
}
16561656

@@ -1671,7 +1671,7 @@ function rangeInputType(scope, element, attr, ctrl, $sniffer, $browser) {
16711671
function maxChange(val) {
16721672
maxVal = parseNumberAttrVal(val);
16731673
// ignore changes before model is initialized
1674-
if (isNumber(ctrl.$modelValue) && isNaN(ctrl.$modelValue)) {
1674+
if (isNumberNaN(ctrl.$modelValue)) {
16751675
return;
16761676
}
16771677

@@ -1693,7 +1693,7 @@ function rangeInputType(scope, element, attr, ctrl, $sniffer, $browser) {
16931693
function stepChange(val) {
16941694
stepVal = parseNumberAttrVal(val);
16951695
// ignore changes before model is initialized
1696-
if (isNumber(ctrl.$modelValue) && isNaN(ctrl.$modelValue)) {
1696+
if (isNumberNaN(ctrl.$modelValue)) {
16971697
return;
16981698
}
16991699

src/ng/directive/ngModel.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
552552
*/
553553
this.$validate = function() {
554554
// ignore $validate before model is initialized
555-
if (isNumber(ctrl.$modelValue) && isNaN(ctrl.$modelValue)) {
555+
if (isNumberNaN(ctrl.$modelValue)) {
556556
return;
557557
}
558558

@@ -723,7 +723,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
723723
}
724724
}
725725
}
726-
if (isNumber(ctrl.$modelValue) && isNaN(ctrl.$modelValue)) {
726+
if (isNumberNaN(ctrl.$modelValue)) {
727727
// ctrl.$modelValue has not been touched yet...
728728
ctrl.$modelValue = ngModelGet($scope);
729729
}

src/ng/directive/ngPluralize.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ var ngPluralizeDirective = ['$locale', '$interpolate', '$log', function($locale,
206206

207207
scope.$watch(numberExp, function ngPluralizeWatchAction(newVal) {
208208
var count = parseFloat(newVal);
209-
var countIsNaN = isNaN(count);
209+
var countIsNaN = isNumberNaN(count);
210210

211211
if (!countIsNaN && !(count in whens)) {
212212
// If an explicit number rule such as 1, 2, 3... is defined, just use it.
@@ -216,7 +216,7 @@ var ngPluralizeDirective = ['$locale', '$interpolate', '$log', function($locale,
216216

217217
// If both `count` and `lastCount` are NaN, we don't need to re-register a watch.
218218
// In JS `NaN !== NaN`, so we have to explicitly check.
219-
if ((count !== lastCount) && !(countIsNaN && isNumber(lastCount) && isNaN(lastCount))) {
219+
if ((count !== lastCount) && !(countIsNaN && isNumberNaN(lastCount))) {
220220
watchRemover();
221221
var whenExpFn = whensExpFns[count];
222222
if (isUndefined(whenExpFn)) {

src/ng/directive/validators.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ var maxlengthDirective = function() {
259259
var maxlength = -1;
260260
attr.$observe('maxlength', function(value) {
261261
var intVal = toInt(value);
262-
maxlength = isNaN(intVal) ? -1 : intVal;
262+
maxlength = isNumberNaN(intVal) ? -1 : intVal;
263263
ctrl.$validate();
264264
});
265265
ctrl.$validators.maxlength = function(modelValue, viewValue) {

src/ng/filter/limitTo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ function limitToFilter() {
106106
} else {
107107
limit = toInt(limit);
108108
}
109-
if (isNaN(limit)) return input;
109+
if (isNumberNaN(limit)) return input;
110110

111111
if (isNumber(input)) input = input.toString();
112112
if (!isArrayLike(input)) return input;

src/ng/rootScope.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -806,8 +806,7 @@ function $RootScopeProvider() {
806806
if ((value = get(current)) !== (last = watch.last) &&
807807
!(watch.eq
808808
? equals(value, last)
809-
: (typeof value === 'number' && typeof last === 'number'
810-
&& isNaN(value) && isNaN(last)))) {
809+
: (isNumberNaN(value) && isNumberNaN(last)))) {
811810
dirty = true;
812811
lastDirtyWatch = watch;
813812
watch.last = watch.eq ? copy(value, null) : value;

test/.eslintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"isObject": false,
6060
"isString": false,
6161
"isNumber": false,
62+
"isNumberNaN": false,
6263
"isDate": false,
6364
"isArray": false,
6465
"isFunction": false,

test/ng/directive/selectSpec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,7 +1375,7 @@ describe('select', function() {
13751375
expect(element.find('option').eq(0).val()).toBe('? boolean:false ?');
13761376

13771377
browserTrigger(element.find('option').eq(0));
1378-
if (typeof prop === 'number' && isNaN(prop)) {
1378+
if (isNumberNaN(prop)) {
13791379
expect(scope.selected).toBeNaN();
13801380
} else {
13811381
expect(scope.selected).toBe(prop);
@@ -1419,7 +1419,7 @@ describe('select', function() {
14191419
expect(selectController.removeOption.calls.count()).toBe(1);
14201420

14211421
// Updating the option value currently does not update the select model
1422-
if (typeof prop === 'number' && isNaN(prop)) {
1422+
if (isNumberNaN(prop)) {
14231423
expect(selectController.removeOption.calls.argsFor(0)[0]).toBeNaN();
14241424
} else {
14251425
expect(selectController.removeOption.calls.argsFor(0)[0]).toBe(prop);

0 commit comments

Comments
 (0)