UNPKG

@technobuddha/library

Version:
24 lines (23 loc) 711 B
import { empty } from '../constants'; import toASCII from '../toASCII'; import splitWords from '../splitWords'; /** * Approximate the number of syllables in a string * * @param input The string * @returns the number of syllables */ export function syllables(input) { return splitWords(toASCII(input.toLowerCase())).reduce((count, word) => { if (word.length <= 3) { count++; } else { word = word.replace(/(?:[^laeiouy]es|ed|[^laeiouy]e)$/u, empty).replace(/^y/u, empty); const match = word.match(/[aeiouy]{1,2}/ug); count += match === null ? 0 : match.length; } return count; }, 0); } export default syllables;