blob: b2c7a9445c978b434edfa9d650d045a27f3c64a3 [file] [log] [blame]
Matteo Scandoloc49f53c2016-04-20 11:38:42 -07001(function() {
2 'use strict';
3
4 /**
5 * @ngdoc service
6 * @name xos.helpers.LabelFormatter
7 * @description This factory define a set of helper function to format label started from an object property
8 **/
9
10 angular
Matteo Scandolod702aff2016-04-22 10:53:49 -070011 .module('xos.uiComponents')
Matteo Scandoloc49f53c2016-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
26 return _capitalize(string).replace(/\s\s+/g, ' ') + ':';
27 };
28
29 return {
30 // test export
31 _formatByUnderscore: _formatByUnderscore,
32 _formatByUppercase: _formatByUppercase,
33 _capitalize: _capitalize,
34 // export to use
35 format: format
36 };
37 }
38})();