freeCodeCamp/utils/block-nameify.ts

24 lines
618 B
TypeScript
Raw Normal View History

import preFormattedBlockNames from './preformatted-block-names.json';
2018-04-06 13:51:52 +00:00
const noFormatting = ['and', 'for', 'of', 'the', 'up', 'with'];
2018-04-06 13:51:52 +00:00
export function blockNameify(phrase: string): string {
const preFormatted =
(preFormattedBlockNames as Record<string, string>)[phrase] || '';
2018-04-06 13:51:52 +00:00
if (preFormatted) {
return preFormatted;
}
return phrase
.split('-')
.map(word => {
2018-04-06 13:51:52 +00:00
if (noFormatting.indexOf(word) !== -1) {
return word;
}
if (word === 'javascript') {
return 'JavaScript';
}
return word.charAt(0).toUpperCase() + word.slice(1);
})
.join(' ');
}