Skip to content

Instantly share code, notes, and snippets.

@cyberfox
Created October 20, 2011 18:43
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cyberfox/1301931 to your computer and use it in GitHub Desktop.
Save cyberfox/1301931 to your computer and use it in GitHub Desktop.
JavaScript humanize method.
com = { cyberfox: {} };
/**
* Convert a property name into a human readable string by replacing _ with
* spaces, and upcasing the first letter of each word.
*
* @param {string} property The property name to convert into a readable name.
* @return {string} The property name converted to a friendly readable format.
* @private
*/
com.cyberfox.humanize_ = function(property) {
return property.replace(/_/g, ' ')
.replace(/(\w+)/g, function(match) {
return match.charAt(0).toUpperCase() + match.slice(1);
});
};
@macedd
Copy link

macedd commented Sep 24, 2015

This do not match special characters (as they are not word)

Eg. furgão will become FurgãO

I ended using this version:

        return text.toString().toLowerCase()
                .replace(/[_-]/g, ' ')
                .replace(/(?:^|\s)\S/g, function(a) {
                  return a.toUpperCase();
                });

@jensneeoosh
Copy link

Just helped fix tons of IE8 errors. Thanks!

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