import { PLURAL_SUFFIX, SPELLINGS, SUFFIX_SPELLINGS, WORD_CHARACTERS, } from "#configuration/constants/vocabulary.constants"; const SPACE = " "; const spellingOf = function spellingOf(word: string): string { const mapped = SPELLINGS.get(word); if (mapped !== undefined) { return mapped; } const suffix = SUFFIX_SPELLINGS.find(([british]) => word.endsWith(british)); return suffix === undefined ? word : word.slice(0, -suffix[0].length) + suffix[1]; }; export const normalizeWord = function normalizeWord(word: string): string { const spelled = spellingOf(word.toLowerCase()); return spelled.endsWith(PLURAL_SUFFIX) ? spelled.slice(0, -1) : spelled; }; export const wordsOf = function wordsOf(phrase: string): readonly string[] { const words: string[] = []; let current = ""; for (const char of phrase.toLowerCase() + SPACE) { if (WORD_CHARACTERS.includes(char)) { current += char; continue; } if (current.length > 0) { words.push(normalizeWord(current)); current = ""; } } return words; };