Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Improve lang/isNaN behavior. closes #39
the native `isNaN` is very confusing since it coerces value ToNumber:

    // native isNaN behavior:
    isNaN([])   // false
    isNaN('')   // false
    isNaN(null) // false
    isNaN('12') // false

this implementation solves edge cases and provides a more intuitive behavior:

    // our implementation
    isNaN([])   // true
    isNaN('')   // true
    isNaN(null) // true
    isNaN('12') // true
  • Loading branch information
millermedeiros committed Feb 21, 2013
1 parent a8c82a7 commit b33a7b3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,7 @@ Next
- improve `number/sign` behavior when value is zero (returns zero).
- add `lang/is` and `lang/isnt`.
- add `lang/isInteger`.
- improve `lang/isNaN` to actually check if value *is not a number* (#39).


v0.3.0
Expand Down
2 changes: 1 addition & 1 deletion src/lang/isNaN.js
Expand Up @@ -7,7 +7,7 @@ define(['./isNumber'], function (isNumber) {
// based on the fact that NaN !== NaN
// need to check if it's a number to avoid conflicts with host objects
// also need to coerce ToNumber to avoid edge case `new Number(NaN)`
return isNumber(val) && val != +val;
return !isNumber(val) || val != +val;
}

return isNaN;
Expand Down
20 changes: 10 additions & 10 deletions tests/spec/lang/spec-isNaN.js
Expand Up @@ -3,17 +3,17 @@ define(['mout/lang/isNaN'], function(isNaN){
describe('lang/isNaN', function(){

it('should check if value is NaN for realz', function(){
expect( isNaN(true) ).toBe( false );
expect( isNaN(false) ).toBe( false );
expect( isNaN(true) ).toBe( true );
expect( isNaN(false) ).toBe( true );
expect( isNaN(123) ).toBe( false );
expect( isNaN('000123') ).toBe( false );
expect( isNaN('dolor123bar') ).toBe( false );
expect( isNaN( {} ) ).toBe( false );
expect( isNaN( [] ) ).toBe( false );
expect( isNaN( [1,2] ) ).toBe( false );
expect( isNaN( '' ) ).toBe( false );
expect( isNaN( null ) ).toBe( false );
expect( isNaN( undefined ) ).toBe( false );
expect( isNaN('000123') ).toBe( true );
expect( isNaN('dolor123bar') ).toBe( true );
expect( isNaN( {} ) ).toBe( true );
expect( isNaN( [] ) ).toBe( true );
expect( isNaN( [1,2] ) ).toBe( true );
expect( isNaN( '' ) ).toBe( true );
expect( isNaN( null ) ).toBe( true );
expect( isNaN( undefined ) ).toBe( true );
expect( isNaN( new Number(123) ) ).toBe( false );
expect( isNaN( new Number('123') ) ).toBe( false );
expect( isNaN( NaN ) ).toBe( true );
Expand Down

0 comments on commit b33a7b3

Please sign in to comment.