-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcapitalize.js
44 lines (44 loc) · 1.58 KB
/
capitalize.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
'use strict';
/**
* Capitalize Filter
* Capitalizes all the words of a given sentence.
* If the format parameter is set to 'team', uppercase the team abbreviation.
* i.e. CLUB DEPORTIVO LOGROÑÉS => Club Deportivo Logroñés
* i.e. sd logroñés => SD Logroñés
* @author Pablo Villoslada Puigcerber <[email protected]>
*
* @param {string} input The string to be formatted.
* @param {string} [format] The format to be applied being the options 'all', 'first' or 'team'.
* If not specified, 'all' is used.
* @param {string} [separator] The character(s) to be used for separating the string.
* If not specified, space is used.
* @returns {string} Formatted string.
*/
angular.module('puigcerber.capitalize',[])
.filter('capitalize', function () {
return function (input, format, separator) {
if (!input) {
return input;
}
format = format || 'all';
separator = separator || ' ';
if (format === 'first') {
// Capitalize the first letter of a sentence
var output = input.charAt(0).toUpperCase() + input.slice(1).toLowerCase();
if (separator === ' ') {
return output;
} else {
return output.split(separator).join(' ');
}
} else {
return input.split(separator).map(function(word) {
if (word.length === 2 && format === 'team') {
// Uppercase team abbreviations like FC, CD, SD
return word.toUpperCase();
} else {
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
}
}).join(' ');
}
};
});