story-generator/src/features/syllables/utils/syllableSplitter.ts
2025-01-23 16:49:12 -03:00

22 lines
687 B
TypeScript

const VOWELS = new Set(['a', 'e', 'i', 'o', 'u', 'á', 'é', 'í', 'ó', 'ú', 'â', 'ê', 'ô', 'ã', 'õ']);
export function splitIntoSyllables(word: string): string[] {
const syllables: string[] = [];
let currentSyllable = '';
for (let i = 0; i < word.length; i++) {
const char = word[i].toLowerCase();
currentSyllable += word[i];
if (VOWELS.has(char)) {
if (i < word.length - 1 && !VOWELS.has(word[i+1].toLowerCase())) {
syllables.push(currentSyllable);
currentSyllable = '';
} else if (i === word.length - 1) {
syllables.push(currentSyllable);
}
}
}
return syllables.length > 0 ? syllables : [word];
}