@technobuddha/library
Version:
A large library of useful functions
31 lines (30 loc) • 1.08 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.syllables = void 0;
var constants_1 = require("../constants");
var toASCII_1 = __importDefault(require("../toASCII"));
var splitWords_1 = __importDefault(require("../splitWords"));
/**
* Approximate the number of syllables in a string
*
* @param input The string
* @returns the number of syllables
*/
function syllables(input) {
return splitWords_1.default(toASCII_1.default(input.toLowerCase())).reduce(function (count, word) {
if (word.length <= 3) {
count++;
}
else {
word = word.replace(/(?:[^laeiouy]es|ed|[^laeiouy]e)$/u, constants_1.empty).replace(/^y/u, constants_1.empty);
var match = word.match(/[aeiouy]{1,2}/ug);
count += match === null ? 0 : match.length;
}
return count;
}, 0);
}
exports.syllables = syllables;
exports.default = syllables;