Skip to content

Instantly share code, notes, and snippets.

@documentcloud
Created December 1, 2009 21:52
Show Gist options
  • Select an option

  • Save documentcloud/246691 to your computer and use it in GitHub Desktop.

Select an option

Save documentcloud/246691 to your computer and use it in GitHub Desktop.
// Snagged from Prototype
window.Inflector = {
camelize: function(s) {
var parts = s.split('-'), len = parts.length;
if (len == 1) return parts[0];
var camelized = s.charAt(0) == '-'
? parts[0].charAt(0).toUpperCase() + parts[0].substring(1)
: parts[0];
for (var i = 1; i < len; i++)
camelized += parts[i].charAt(0).toUpperCase() + parts[i].substring(1);
return camelized;
},
capitalize: function(s) {
return s.charAt(0).toUpperCase() + s.substring(1).toLowerCase();
},
underscore: function(s) {
return s.replace(/::/g, '/').replace(/([A-Z]+)([A-Z][a-z])/g,'$1_$2').replace(/([a-z\d])([A-Z])/g,'$1_$2').replace(/-/g,'_').toLowerCase();
},
dasherize: function(s) {
return s.replace(/_/g,'-');
},
singularize: function(s) {
return s.replace(/e?s$/, '');
},
// Only works for words that pluralize by adding an 's'.
pluralize: function(s, count) {
return count != 1 ? s + 's' : s;
},
classify: function(s) {
return this.camelize(this.capitalize(this.dasherize(this.singularize(s))));
},
truncate : function(s, length, truncation) {
length = length || 30;
truncation = _.isUndefined(truncation) ? '...' : truncation;
return s.length > length ? s.slice(0, length - truncation.length) + truncation : s;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment