Skip to content

Instantly share code, notes, and snippets.

@cyberfox
Created October 20, 2011 18:43
Show Gist options
  • Select an option

  • Save cyberfox/1301931 to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown

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
Copy Markdown

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