forked from Puigcerber/angular-capitalize-filter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapitalize.js
38 lines (38 loc) · 1.37 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
'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.
* @returns {string} Formatted string.
*/
angular.module('customFilters',[])
.filter('capitalize', function () {
return function (input, format) {
format = format || 'all';
if (typeof input !== 'undefined') {
if (format === 'first') {
// Capitalize the first letter of a sentence
return input.charAt(0).toUpperCase() + input.slice(1).toLowerCase();
} else {
var words = input.split(' ');
var result = [];
words.forEach(function(word) {
if (word.length === 2 && format === 'team') {
// Uppercase team abbreviations like FC, CD, SD
result.push(word.toUpperCase());
} else {
result.push(word.charAt(0).toUpperCase() + word.slice(1).toLowerCase());
}
});
return result.join(' ');
}
}
};
});