blob: bb1d27966c42149d92f06eefc71432ead9d86125 [file] [log] [blame]
Matteo Scandolo7bc39c42016-04-20 11:38:42 -07001(function() {
2 'use strict';
3
4 /**
5 * @ngdoc service
Matteo Scandolo974c0e42016-05-25 16:02:16 -07006 * @name xos.uiComponents.LabelFormatter
Matteo Scandolo7bc39c42016-04-20 11:38:42 -07007 * @description This factory define a set of helper function to format label started from an object property
8 **/
9
10 angular
Matteo Scandolo199ec002016-04-22 10:53:49 -070011 .module('xos.uiComponents')
Matteo Scandolo7bc39c42016-04-20 11:38:42 -070012 .factory('LabelFormatter', labelFormatter);
13
14 function labelFormatter() {
15
16 const _formatByUnderscore = string => string.split('_').join(' ').trim();
17
18 const _formatByUppercase = string => string.split(/(?=[A-Z])/).map(w => w.toLowerCase()).join(' ');
19
20 const _capitalize = string => string.slice(0, 1).toUpperCase() + string.slice(1);
21
22 const format = (string) => {
23 string = _formatByUnderscore(string);
24 string = _formatByUppercase(string);
25
Matteo Scandoloe2ee2d92016-04-27 15:58:16 -070026 string = _capitalize(string).replace(/\s\s+/g, ' ') + ':';
27 return string.replace('::', ':');
Matteo Scandolo7bc39c42016-04-20 11:38:42 -070028 };
29
30 return {
31 // test export
32 _formatByUnderscore: _formatByUnderscore,
33 _formatByUppercase: _formatByUppercase,
34 _capitalize: _capitalize,
35 // export to use
36 format: format
37 };
38 }
39})();