blob: 150e3e332e77061ea67dd7019dcd7c8682760598 [file] [log] [blame]
Matteo Scandolo686547a2017-08-08 13:05:25 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandoloa5d03d52016-07-21 11:35:46 -070019(function() {
20 'use strict';
21
22 /**
23 * @ngdoc service
24 * @name xos.uiComponents.LabelFormatter
25 * @description This factory define a set of helper function to format label started from an object property
26 **/
27
28 angular
29 .module('xos.uiComponents')
30 .factory('LabelFormatter', labelFormatter);
31
32 function labelFormatter() {
33
34 /**
35 * @ngdoc method
36 * @name xos.uiComponents.LabelFormatter#_formatByUnderscore
37 * @methodOf xos.uiComponents.LabelFormatter
38 * @description
39 * Convert a `snake_case` string to readable string.<br/>
40 * Eg: `this_string` will became `this string`
41 * @param {string} string The string to be converted
42 * @returns {string} The converten string
43 **/
44
45 const _formatByUnderscore = string => string.split('_').join(' ').trim();
46
47 /**
48 * @ngdoc method
49 * @name xos.uiComponents.LabelFormatter#_formatByUppercase
50 * @methodOf xos.uiComponents.LabelFormatter
51 * @description
52 * Convert a `camelCase` string to readable string.<br/>
53 * Eg: `thisString` will became `this string`
54 * @param {string} string The string to be converted
55 * @returns {string} The converten string
56 **/
57
58 const _formatByUppercase = string => string.split(/(?=[A-Z])/).map(w => w.toLowerCase()).join(' ');
59
60 /**
61 * @ngdoc method
62 * @name xos.uiComponents.LabelFormatter#_capitalize
63 * @methodOf xos.uiComponents.LabelFormatter
64 * @description
65 * Capitalize the first letter of a string.<br/>
66 * Eg: `this string` will became `This string`
67 * @param {string} string The string to be converted
68 * @returns {string} The converten string
69 **/
70
71 const _capitalize = string => string.slice(0, 1).toUpperCase() + string.slice(1);
72
73 /**
74 * @ngdoc method
75 * @name xos.uiComponents.LabelFormatter#format
76 * @methodOf xos.uiComponents.LabelFormatter
77 * @description
78 * Apply in order:
79 * - _formatByUnderscore
80 * - _formatByUppercase
81 * - _capitalize
82 * - replace multiple space with a single one
Matteo Scandolo1d689852016-09-29 09:42:12 -070083 * - append `:` at the end
Matteo Scandoloa5d03d52016-07-21 11:35:46 -070084 * <br/>
85 * Eg: `this_string` will became `This string:`<br/>
86 * Eg: `thisString` will became `This string:`
87 * @param {string} string The string to be converted
88 * @returns {string} The converten string
89 **/
90
91 const format = (string) => {
92 string = _formatByUnderscore(string);
93 string = _formatByUppercase(string);
94
95 string = _capitalize(string).replace(/\s\s+/g, ' ') + ':';
96 return string.replace('::', ':');
97 };
98
99 return {
100 // test export
101 _formatByUnderscore: _formatByUnderscore,
102 _formatByUppercase: _formatByUppercase,
103 _capitalize: _capitalize,
104 // export to use
105 format: format
106 };
107 }
108})();