OpenLayers OpenLayers

Ticket #1441: numeric.patch

File numeric.patch, 2.6 kB (added by tschaub, 8 months ago)

test if a string represents a number

  • tests/test_BaseTypes.html

    old new  
    141141        ); 
    142142 
    143143    } 
     144 
     145    function test_String_isNumeric(t) { 
     146        var cases = [ 
     147            {value: "3", expect: true}, 
     148            {value: "+3", expect: true}, 
     149            {value: "-3", expect: true}, 
     150            {value: "3.0", expect: true}, 
     151            {value: "+3.0", expect: true}, 
     152            {value: "-3.0", expect: true}, 
     153            {value: "6.02e23", expect: true}, 
     154            {value: "+1.0e-100", expect: true}, 
     155            {value: "-1.0e+100", expect: true}, 
     156            {value: "1E100", expect: true}, 
     157            {value: null, expect: false}, 
     158            {value: true, expect: false}, 
     159            {value: false, expect: false}, 
     160            {value: undefined, expect: false}, 
     161            {value: "", expect: false}, 
     162            {value: "3 ", expect: false}, 
     163            {value: " 3", expect: false}, 
     164            {value: "1e", expect: false}, 
     165            {value: "1+e", expect: false}, 
     166            {value: "1-e", expect: false} 
     167        ]; 
     168        t.plan(cases.length); 
     169         
     170        var func = OpenLayers.String.isNumeric; 
     171        var obj, val, got, exp; 
     172        for(var i=0; i<cases.length; ++i) { 
     173            obj = cases[i]; 
     174            val = obj.value; 
     175            exp = obj.expect; 
     176            got = func(val); 
     177            t.eq(got, exp, "'" + val + "' returns " + exp); 
     178        } 
     179         
     180    } 
    144181    
    145182    
    146183    function test_06_Number_limitSigDigs(t) { 
  • lib/OpenLayers/BaseTypes.js

    old new  
    136136            } 
    137137        } 
    138138        return tokens.join(""); 
     139    }, 
     140     
     141    /** 
     142     * Property: OpenLayers.String.numberRegEx 
     143     * Used to test strings as numbers. 
     144     */ 
     145    numberRegEx: /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/, 
     146     
     147    /** 
     148     * APIFunction: OpenLayers.String.isNumeric 
     149     * Determine whether a string contains only a numeric value. 
     150     * 
     151     * Examples: 
     152     * (code) 
     153     * OpenLayers.String.isNumeric("6.02e23") // true 
     154     * OpenLayers.String.isNumeric("12 dozen") // false 
     155     * OpenLayers.String.isNumeric("4") // true 
     156     * OpenLayers.String.isNumeric(" 4 ") // false 
     157     * (end) 
     158     * 
     159     * Returns: 
     160     * {Boolean} String contains only a number. 
     161     */ 
     162    isNumeric: function(value) { 
     163        return OpenLayers.String.numberRegEx.test(value); 
    139164    } 
    140165 
    141166};