Skip to content

Instantly share code, notes, and snippets.

@juanpujol
Created June 22, 2013 01:01
Show Gist options
  • Save juanpujol/5835379 to your computer and use it in GitHub Desktop.
Save juanpujol/5835379 to your computer and use it in GitHub Desktop.
AngularJS Filter to transform a constant like strings to human text. i.e. "SUPPORT_SYSTEM" => "Support system"
angular.module('app').filter('humanizeConstant', function(){
return function(text) {
if(text) {
var string = text.split("_").join(" ").toLowerCase();
var string = string.charAt(0).toUpperCase() + string.slice(1);
return string
};
};
});
@lasergoat
Copy link

I needed to capitalize the first letter in each word so I did this:

angular.module('app').filter('humanize', function(){
    return function(text) {
        if(text) { 
            text = text.split("_");

            // go through each word in the text and capitalize the first letter
            for (var i in text) {
                var word = text[i];
                word = word.toLowerCase();
                word = word.charAt(0).toUpperCase() + word.slice(1);
                text[i] = word;
            }

            return text.join(" "); 
        }
    };
});

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