Skip to content

Instantly share code, notes, and snippets.

@taketin
Created November 20, 2012 07:42
Show Gist options
  • Save taketin/4116615 to your computer and use it in GitHub Desktop.
Save taketin/4116615 to your computer and use it in GitHub Desktop.
capitalize method
String.prototype.capitalize = function() {
return this.toString().toLowerCase().replace(/\b[a-z]/g, function(letter) {
return letter.toUpperCase();
})
}
@taketin
Copy link
Author

taketin commented Nov 20, 2012

CoffeeScript

String::capitalize = ->
  @toString().toLowerCase().replace /\b[a-z]/g, (letter) ->
    letter.toUpperCase()

@taketin
Copy link
Author

taketin commented Nov 20, 2012

  • usage
str = 'hoge'
str.capitalize()  // 'Hoge'

str = 'hoge fuga piyo'
str.capitalize()  // 'Hoge Fuga Piyo'

str = 'hoge-fuga-piyo'
str.capitalize()  // 'Hoge-Fuga-Piyo'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment