JSFiddle - React, Tailwind, and code Playground
by gcjnst
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="results">
</div>
CSS
#results {
font: normal 15px monospace;
}
JavaScript
function formatWord(word, rootWord) {
let baseWord = '';
let suffix = '';
if (!word || !rootWord) {
baseWord = word;
} else {
word = word.toUpperCase();
rootWord = rootWord.toUpperCase();
let rootWordLastLetter = rootWord.substring(rootWord.length - 1, rootWord.length);
if (word === rootWord + 'S') {
// PINS/PIN = PIN_S
baseWord = word.substring(0, word.length - 1);
suffix = 'S';
} else if (word === rootWord + 'ES') {
// PASSES/PASS = PASS_ES
baseWord = word.substring(0, word.length - 2);
suffix = 'ES';
} else if (word.endsWith('IES') && rootWordLastLetter === 'Y' && word.length === rootWord.length + 2) {
// PONIES/PONY = PON_IES
baseWord = rootWord.substring(0, rootWord.length - 1);
suffix = 'IES';
} else if (word === rootWord + 'ING') {
// PASSING/PASS = PASS_ING
baseWord = word.substring(0, word.length - 3);
suffix = 'ING';
} else if (word === `${rootWord}${rootWordLastLetter}ING`) {
// PINNING/PIN = PIN_NING
baseWord = rootWord;
suffix = rootWordLastLetter + 'ING';
} else if (rootWordLastLetter === 'E' && word === `${rootWord.substring(0, rootWord.length - 1)}ING`) {
// PILING/PILE = PIL_ING
baseWord = rootWord.substring(0, rootWord.length - 1);
suffix = 'ING';
} else if (word === rootWord + 'D') {
// PILED/PILE = PILE_D
baseWord = word.substring(0, word.length - 1);
suffix = 'D';
} else if (word === rootWord + 'ED') {
// PASSED/PASS = PASS_ED
baseWord = word.substring(0, word.length - 2);
suffix = 'ED';
} else if (word === `${rootWord}${rootWordLastLetter}ED`) {
// PINNED/PIN = PIN_NED
baseWord = rootWord;
suffix = rootWordLastLetter + 'ED';
} else if (word.endsWith('IED') && rootWordLastLetter === 'Y' && word.length === rootWord.length + 2) {
// PONIED/PONY = PON_IED
baseWord = rootWord.substring(0, rootWord.length - 1);
suffix = 'IED';
}
}
return word === `${baseWord}${suffix}` ? `${baseWord}_${suffix}` : word;
}
let tests =...