apphouse
Version:
Component library for React that uses observable state management and theme-able components.
45 lines (37 loc) • 1.09 kB
text/typescript
/**
* Helper function to convert a camel case string to formatted title
* @param camelString string
* @param capitalizeAll boolean if true, all words in the title will be capitalized will be uppercase
* @returns string formatted as title
*/
export function makeTitle(camelString: string, capitalizeAll = false) {
if (capitalizeAll) {
return makeTitleAllUpercase(camelString);
}
let result = '';
for (let i = 0; i < camelString.length; i++) {
const char = camelString.charAt(i);
if (i === 0) {
result += char.toUpperCase();
} else if (char === char.toUpperCase()) {
result += ' ' + char.toLowerCase();
} else {
result += char;
}
}
return result;
}
function makeTitleAllUpercase(camelcaseString: string) {
let result = '';
for (let i = 0; i < camelcaseString.length; i++) {
const char = camelcaseString.charAt(i);
if (i === 0) {
result += char.toUpperCase();
} else if (char === char.toUpperCase()) {
result += ' ' + char.toUpperCase();
} else {
result += char;
}
}
return result;
}