tdc-js-modules
Version:
28 lines (26 loc) • 756 B
text/typescript
const capitalize = (s: string) => {
if (!s) {
return '';
}
const char_0 = s.charAt(0);
const char_1 = s.charAt(1);
if (char_0 && char_1) {
return s.charAt(0).toUpperCase() + s.slice(1).toLocaleLowerCase();
} else {
return s;
}
};
const capitalizeAllWords = (str: string) => {
var splitStr = str
.toString()
.toLowerCase()
.split(' ');
for (var i = 0; i < splitStr.length; i++) {
// You do not need to check if i is larger than splitStr length, as your for does that for you
// Assign it back to the array
splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
}
// Directly return the joined string
return splitStr.join(' ');
};
export { capitalize, capitalizeAllWords };