Skip to content

Commit fc66e96

Browse files
committed
refactor(form)
1 parent 59fd5dd commit fc66e96

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

js/filters/titlecase.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,21 @@ module fng.filters {
55
/*@ngInject*/
66
export function titleCase() {
77
return function (str, stripSpaces) {
8-
var value = str
9-
.replace(/(_|\.)/g, ' ') // replace underscores and dots with spaces
10-
.replace(/[A-Z]/g, ' $&').trim() // precede replace caps with a space
11-
.replace(/\w\S*/g, function (txt) { // capitalise first letter of word
12-
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
13-
});
14-
if (stripSpaces) {
15-
value = value.replace(/\s/g, '');
16-
} else {
17-
// lose double spaces
18-
value = value.replace(/\s{2,}/g, ' ');
8+
if (str) {
9+
str = str
10+
.replace(/(_|\.)/g, ' ') // replace underscores and dots with spaces
11+
.replace(/[A-Z]/g, ' $&').trim() // precede replace caps with a space
12+
.replace(/\w\S*/g, function (txt) { // capitalise first letter of word
13+
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
14+
});
15+
if (stripSpaces) {
16+
str = str.replace(/\s/g, '');
17+
} else {
18+
// lose double spaces
19+
str = str.replace(/\s{2,}/g, ' ');
20+
}
1921
}
20-
return value;
21-
};
22+
return str;
23+
}
2224
}
2325
}

0 commit comments

Comments
 (0)