mirror of
https://github.com/lucasrcsantana/story-generator.git
synced 2025-12-19 06:47:51 +00:00
22 lines
687 B
TypeScript
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];
|
|
}
|