sbenjamin

parseIntToScript

Jul 27th, 2011
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function parseIntToScript(obj){    
  2.  
  3.     //get all the keys in the object and build an aray.    
  4.     var keys = [];
  5.     for(var key in obj){
  6.         keys.push(key);
  7.     };
  8.                
  9.     //condition loop caching               
  10.     var loopMax = keys.length;
  11.    
  12.     //loop over each key.
  13.     for(var i=0; i<loopMax; i++){
  14.        
  15.         //get the object type.
  16.         var thisType = typeof(globals[keys[i]]);
  17.                        
  18.         //if type is string, check to see if it can be converted to a number.
  19.         if(thisType === 'string'){
  20.             //if parseInt returns a number, then convert.
  21.             if(!isNaN(parseInt(globals[keys[i]]))){
  22.                 globals[keys[i]] = parseInt(globals[keys[i]]);
  23.             }
  24.         }
  25.                    
  26.         //convert string arrays to int arrays.
  27.         if(isArray(globals[keys[i]])){
  28.             //loop through the array.
  29.             var loopMax = globals[keys[i]].length;
  30.             for(var j=0; j<loopMax; j++){
  31.                 if(!isNaN(parseInt(globals[keys[i]][j]))){
  32.                     globals[keys[i]][j] = parseInt(globals[keys[i]][j]);
  33.                 };
  34.             };
  35.         };
  36.  
  37.     };//loop
  38.                    
  39.            
  40.                
  41.     function isArray(obj) {
  42.         //returns true is it is an array
  43.         if (obj.constructor.toString().indexOf('Array') == -1)
  44.             return false;
  45.         else
  46.             return true;
  47.     }//isArray
  48.                
  49. };
Advertisement
Add Comment
Please, Sign In to add comment