Skip to content

Instantly share code, notes, and snippets.

@charlieschwabacher
Created July 21, 2011 09:31
Show Gist options
  • Save charlieschwabacher/1096850 to your computer and use it in GitHub Desktop.
Save charlieschwabacher/1096850 to your computer and use it in GitHub Desktop.
Javascript string presentation methods with underscore.js (upper, lower, capitalize, title)
String.prototype.upper = function() {
return _.reduce(this.split(""), function(memo, chr) { return memo + chr.toUpperCase() }, "")
}
String.prototype.lower = function() {
return _.reduce(this.split(""), function(memo, chr) { return memo + chr.toLowerCase() }, "")
}
String.prototype.capitalize = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
}
String.prototype.title = function() {
return _.reduce(this.lower().split(" "), function(memo, word) { return memo + " " + word.capitalize() }, "").slice(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment