Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 1 | (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 Scandolo | d702aff | 2016-04-22 10:53:49 -0700 | [diff] [blame] | 11 | .module('xos.uiComponents') |
Matteo Scandolo | c49f53c | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 12 | .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 | })(); |