1. #1
    Sencha - Community Support Team mystix's Avatar
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    6,236
    Vote Rating
    5
    mystix will become famous soon enough

      0  

    Default [FIXED][2.x] Ext.num(NaN, 0) returns NaN

    [FIXED][2.x] Ext.num(NaN, 0) returns NaN


    the following code
    Code:
    Ext.num(NaN, 0);
    returns NaN instead of zero. (yes, NaN is a number unfortunately).

    i propose the following simple fix:
    Code:
    Ext.apply(
        num : function(v, defaultValue) {
            return Number(String(v))? Number(v) : defaultValue;
        }
    });
    which performs type string-to-number coercion where applicable (which imho should take place. i refer to one such help request: http://extjs.com/forum/showthread.php?t=46438).

  2. #2
    Sencha User jack.slocum's Avatar
    Join Date
    Mar 2007
    Location
    Tampa, FL
    Posts
    6,955
    Vote Rating
    17
    jack.slocum will become famous soon enough jack.slocum will become famous soon enough

      0  

    Default


    What about:

    Code:
    num : function(v, defaultValue){
                if(typeof v != 'number' || isNaN(v)){
                    return defaultValue;
                }
                return v;
            }
    Jack Slocum
    Ext JS Founder
    Original author of Ext JS 1, 2 & 3.
    Twitter: @jackslocum
    jack@extjs.com

  3. #3
    Sencha - Community Support Team mystix's Avatar
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    6,236
    Vote Rating
    5
    mystix will become famous soon enough

      0  

    Default


    that definitely works, but doesn't take care of the following valid numeric values, simply because they're quoted and hence of type String:
    Code:
    '1.9'
    '-.9'
    '+8.'
    p.s. wasn't the intent of Ext.num() to address the shortcomings of js's isNaN() method
    i.e. to check if a number could successfully be coerced into a numeric value, minus the quirks surrounding isNaN()?

  4. #4
    Ext User
    Join Date
    Oct 2007
    Posts
    83
    Vote Rating
    0
    robw is on a distinguished road

      0  

    Default


    I just came across this issue trying:

    PHP Code:
    Ext.num('1',0//returns 0 
    Expected it to return 1 (as a number). Would be great if it did the conversion - i guess it would have to decide on parseInt or parseFloat if isNaN returns false?

    Thanks
    Rob

  5. #5
    Sencha - Community Support Team mystix's Avatar
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    6,236
    Vote Rating
    5
    mystix will become famous soon enough

      0  

    Default


    Quote Originally Posted by robw View Post
    I just came across this issue trying:

    PHP Code:
    Ext.num('1',0//returns 0 
    Expected it to return 1 (as a number). Would be great if it did the conversion - i guess it would have to decide on parseInt or parseFloat if isNaN returns false?

    Thanks
    Rob
    if it helps, here's what i'm currently using:
    Code:
    Ext.apply(Ext, {
        num : function(v, defaultValue) {
            v = Number(v); // cast value to type Number
    
            return !isNaN(v)? v : defaultValue;
        }
    });
    which takes care of everything i know of (including the 3 examples i posted above) -- no parseFloat / parseInt required.

  6. #6
    Ext User
    Join Date
    Oct 2007
    Posts
    83
    Vote Rating
    0
    robw is on a distinguished road

      0  

    Default


    great thanks - I was overcomplicating it

  7. #7
    Sencha - Ext JS Dev Team evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    16,906
    Vote Rating
    626
    evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute

      0  

    Default


    Modified in SVN.
    Evan Trimboli
    Sencha Developer
    Twitter - @evantrimboli
    Don't be afraid of the source code!

  8. #8
    Sencha - Community Support Team mystix's Avatar
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    6,236
    Vote Rating
    5
    mystix will become famous soon enough

      0  

    Default


    found one more failing test case:
    Code:
    Ext.num(null); // returns 0
    a tiny change is needed with the code in SVN to prevent this:
    Code:
    Ext.apply(Ext, {
        num : function(v, defaultValue) {
            v = Number(v == null? NaN : v);
            return !isNaN(v)? v : defaultValue;
        }
    });
    Last edited by mystix; 14 Mar 2009 at 9:55 AM. Reason: edit

  9. #9
    Sencha - Ext JS Dev Team evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    16,906
    Vote Rating
    626
    evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute

      0  

    Default


    Committed.
    Evan Trimboli
    Sencha Developer
    Twitter - @evantrimboli
    Don't be afraid of the source code!