Last Updated: January 20, 2020
·
2.997K
· juanpujol

Humanize constant filter for AngularJs

I had the need to humanize constants coming from an API, so I made this very simple filter.

i.e: "MY_CONSTANT" => "My constant"

angular.module('app').filter('humanizeConstant', function(){
  return function(text) {
    if(text) { 
      var string = text.split("_").join(" ").toLowerCase();
      string = string.charAt(0).toUpperCase() + string.slice(1); 
      return string
    };
  };
});

3 Responses
Add your response

Thanks for posting this. I also threw in string = text.split('-').join(' ').toLowerCase(); for my use. By the way, you shouldn't redeclare your var string the second time.

over 1 year ago ·

Thanks man, you are right about the var declaration. I already corrected it.

over 1 year ago ·

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

AngularJS pages are long-lived browser apps with the same script risk as a newer single-page app. Third-party code in the template can read forms and values rendered on the page.

Security insights from Cside