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

Commit f8466b9

Browse files
committed
feat(isNaN): isNumberNaN
window.isNaN(‘lol’); //=> true Number.isNaN(‘lol’); //=> false isNaN converts it’s arguments into a Number before checking if it’s NaN
1 parent 4374f89 commit f8466b9

File tree

7 files changed

+16
-12
lines changed

7 files changed

+16
-12
lines changed

src/Angular.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,11 @@ function toInt(str) {
399399
return parseInt(str, 10);
400400
}
401401

402+
var _isNaN = window.isNaN;
403+
var isNumberNaN = Number.isNaN || function isNumberNaN(num) {
404+
return isNumber(num) ? _isNaN(num) : false;
405+
};
406+
402407

403408
function inherit(parent, extra) {
404409
return extend(Object.create(parent), extra);
@@ -1113,7 +1118,7 @@ function fromJson(json) {
11131118

11141119
function timezoneToOffset(timezone, fallback) {
11151120
var requestedTimezoneOffset = Date.parse('Jan 01, 1970 00:00:00 ' + timezone) / 60000;
1116-
return isNaN(requestedTimezoneOffset) ? fallback : requestedTimezoneOffset;
1121+
return isNumberNaN(requestedTimezoneOffset) ? fallback : requestedTimezoneOffset;
11171122
}
11181123

11191124

src/ng/directive/input.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1259,7 +1259,7 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
12591259
if (isDefined(val) && !isNumber(val)) {
12601260
val = parseFloat(val, 10);
12611261
}
1262-
minVal = isNumber(val) && !isNaN(val) ? val : undefined;
1262+
minVal = !isNumberNaN(val) ? val : undefined;
12631263
// TODO(matsko): implement validateLater to reduce number of validations
12641264
ctrl.$validate();
12651265
});
@@ -1275,7 +1275,7 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
12751275
if (isDefined(val) && !isNumber(val)) {
12761276
val = parseFloat(val, 10);
12771277
}
1278-
maxVal = isNumber(val) && !isNaN(val) ? val : undefined;
1278+
maxVal = !isNumberNaN(val) ? val : undefined;
12791279
// TODO(matsko): implement validateLater to reduce number of validations
12801280
ctrl.$validate();
12811281
});

src/ng/directive/ngModel.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
506506
*/
507507
this.$validate = function() {
508508
// ignore $validate before model is initialized
509-
if (isNumber(ctrl.$modelValue) && isNaN(ctrl.$modelValue)) {
509+
if (isNumberNaN(ctrl.$modelValue)) {
510510
return;
511511
}
512512

@@ -676,7 +676,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
676676
}
677677
}
678678
}
679-
if (isNumber(ctrl.$modelValue) && isNaN(ctrl.$modelValue)) {
679+
if (isNumberNaN(ctrl.$modelValue)) {
680680
// ctrl.$modelValue has not been touched yet...
681681
ctrl.$modelValue = ngModelGet($scope);
682682
}

src/ng/directive/ngPluralize.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ var ngPluralizeDirective = ['$locale', '$interpolate', '$log', function($locale,
207207

208208
scope.$watch(numberExp, function ngPluralizeWatchAction(newVal) {
209209
var count = parseFloat(newVal);
210-
var countIsNaN = isNaN(count);
210+
var countIsNaN = isNumberNaN(count);
211211

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

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

src/ng/directive/validators.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ var maxlengthDirective = function() {
6161
var maxlength = -1;
6262
attr.$observe('maxlength', function(value) {
6363
var intVal = toInt(value);
64-
maxlength = isNaN(intVal) ? -1 : intVal;
64+
maxlength = isNumberNaN(intVal) ? -1 : intVal;
6565
ctrl.$validate();
6666
});
6767
ctrl.$validators.maxlength = function(modelValue, viewValue) {

src/ng/filter/limitTo.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ function limitToFilter() {
9696
} else {
9797
limit = toInt(limit);
9898
}
99-
if (isNaN(limit)) return input;
99+
if (isNumberNaN(limit)) return input;
100100

101101
if (isNumber(input)) input = input.toString();
102102
if (!isArray(input) && !isString(input)) return input;
103103

104-
begin = (!begin || isNaN(begin)) ? 0 : toInt(begin);
104+
begin = (!begin || isNumberNaN(begin)) ? 0 : toInt(begin);
105105
begin = (begin < 0 && begin >= -input.length) ? input.length + begin : begin;
106106

107107
if (limit >= 0) {

src/ng/rootScope.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -768,8 +768,7 @@ function $RootScopeProvider() {
768768
if ((value = watch.get(current)) !== (last = watch.last) &&
769769
!(watch.eq
770770
? equals(value, last)
771-
: (typeof value === 'number' && typeof last === 'number'
772-
&& isNaN(value) && isNaN(last)))) {
771+
: (isNumberNaN(value) && isNumberNaN(last)))) {
773772
dirty = true;
774773
lastDirtyWatch = watch;
775774
watch.last = watch.eq ? copy(value, null) : value;

0 commit comments

Comments
 (0)