Files
freeCodeCamp/utils/block-nameify.ts
T
Naomi CarriganandOliver Eyton-Williams 2076e41b2d chore: improve block namify (#49950)
Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>

---------

Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
2023-04-06 18:31:55 +02:00

25 lines
763 B
TypeScript

import preFormattedBlockNames from './preformatted-block-names.json';
import preFormattedWords from './preformatted-words.json';
const noFormatting = ['and', 'for', 'of', 'the', 'up', 'with', 'to', 'by', 'a'];
export function blockNameify(phrase: string): string {
const preFormatted =
(preFormattedBlockNames as Record<string, string>)[phrase] || '';
if (preFormatted) {
return preFormatted;
}
return phrase
.split('-')
.map(word => {
if (noFormatting.indexOf(word) !== -1) {
return word;
}
if ((preFormattedWords as Record<string, string>)[word]) {
return (preFormattedWords as Record<string, string>)[word];
}
return word.charAt(0).toUpperCase() + word.slice(1);
})
.join(' ');
}