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
};
};
});Written by Juan Pujol
Related protips
3 Responses
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.
Thanks man, you are right about the var declaration. I already corrected it.
There's also Inflector from UI-Utils: http://angular-ui.github.io/ui-utils/#/inflector
specifically: https://github.com/angular-ui/ui-utils/blob/master/modules/inflector/inflector.js
Have a fresh tip? Share with Coderwall community!
JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.
Security insights from Cside