blob: 3b63ecd2f32695f13dfd25f99b016318746564bb [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
Matteo Scandolod02ef502016-04-27 15:58:16 -070026 string = _capitalize(string).replace(/\s\s+/g, ' ') + ':';
27 return string.replace('::', ':');
Matteo Scandoloc49f53c2016-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})();