Matteo Scandolo | 7bc39c4 | 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 | 199ec00 | 2016-04-22 10:53:49 -0700 | [diff] [blame] | 11 | .module('xos.uiComponents') |
Matteo Scandolo | 7bc39c4 | 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 | |
Matteo Scandolo | e2ee2d9 | 2016-04-27 15:58:16 -0700 | [diff] [blame] | 26 | string = _capitalize(string).replace(/\s\s+/g, ' ') + ':'; |
| 27 | return string.replace('::', ':'); |
Matteo Scandolo | 7bc39c4 | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 28 | }; |
| 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 | })(); |