Skip to content

Instantly share code, notes, and snippets.

@romeoh
Created September 4, 2012 05:23
Show Gist options
  • Save romeoh/3616948 to your computer and use it in GitHub Desktop.
Save romeoh/3616948 to your computer and use it in GitHub Desktop.
string utils
/* trim utils */
trim: function(_value){
return _value.replace(/^\s+|\s+$/g,"");
},
leftTrim: function(_value){
return _value.replace(/^\s+/,"");
},
rightTrim: function(_value){
return _value.replace(/\s+$/,"");
},
isNumber: function(n){
return !isNaN(parseFloat(n)) && isFinite(n);
},
camelize: function(_value){
return _value.replace (/(?:^|[-_])(\w)/g, function (_, c) {
return c ? c.toUpperCase () : '';
})
},
decamelize: function(_value){
return _value.replace(/([a-z])([A-Z])/g,'$1-$2').toLowerCase();
},
Mui.toCurrency = function(_value){
if(isNaN(_value)) return "NaN";
_value = Math.round(_value*100) / 100;
var amountStr = String(_value);
var amountArray = amountStr.split(".");
if(amountArray[1] == undefined) amountArray[1] = "";
if(amountArray[1].length == 1) amountArray[1] += "";
var currencyArray = new Array();
var start, end = amountArray[0].length;
while(end > 0) {
start = Math.max(end - 3, 0);
currencyArray.unshift(amountArray[0].slice(start, end));
end = start;
}
amountArray[0] = currencyArray.join(",");
return (amountArray.join(""));
},
Mui.toNumber = function(_value){
var amountArray = new Array();
var currencyArray = new Array();
for(var i=0; i<_value.toString().length; i++){
amountArray[i] = _value.toString().substr(i, 1);
if(amountArray[i] == ",") amountArray.splice(i);
if(amountArray[i] == ".") amountArray.splice(i);
if(amountArray[i] != undefined) currencyArray.push(amountArray[i]);
}
return currencyArray.join("")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment