javascripts/js_extensions.js
// Some Ruby-ish String extensions
// from http://code.google.com/p/inflection-js/
if (!String.prototype.camelize) String.prototype.camelize = function(lowFirstLetter)
{
var str=this; //.toLowerCase();
var str_path=str.split('/');
for(var i=0;i<str_path.length;i++)
{
var str_arr=str_path[i].split('_');
var initX=((lowFirstLetter&&i+1==str_path.length)?(1):(0));
for(var x=initX;x<str_arr.length;x++)
str_arr[x]=str_arr[x].charAt(0).toUpperCase()+str_arr[x].substring(1);
str_path[i]=str_arr.join('');
}
str=str_path.join('::');
return str;
};
if (!String.prototype.capitalize) String.prototype.capitalize = function()
{
var str=this.toLowerCase();
str=str.substring(0,1).toUpperCase()+str.substring(1);
return str;
};
if (!String.prototype.humanize) String.prototype.humanize = function(lowFirstLetter)
{
var str=this.toLowerCase();
str=str.replace(new RegExp('_id','g'),'');
str=str.replace(new RegExp('_','g'),' ');
if(!lowFirstLetter)str=str.capitalize();
return str;
};
// This one is borrowed from prototype.js
if (!String.prototype.underscore) String.prototype.underscore = function() {
return this.replace(/::/g, '/')
.replace(/([A-Z]+)([A-Z][a-z])/g, '$1_$2')
.replace(/([a-z\d])([A-Z])/g, '$1_$2')
.replace(/-/g, '_')
.toLowerCase();
};