UNPKG

yoastseo-dep

Version:

Yoast clientside page analysis

66 lines (58 loc) 1.85 kB
import { isUndefined } from "lodash-es"; import arrayToRegex from "../regex/createRegexFromArray.js"; /** * Constructs a language syllable regex that contains a regex for matching syllable exclusion. * * @param {object} syllableRegex The object containing the syllable exclusions. * @constructor */ const SyllableCountStep = function( syllableRegex ) { this._hasRegex = false; this._regex = ""; this._multiplier = ""; this.createRegex( syllableRegex ); }; /** * Returns if a valid regex has been set. * * @returns {boolean} True if a regex has been set, false if not. */ SyllableCountStep.prototype.hasRegex = function() { return this._hasRegex; }; /** * Creates a regex based on the given syllable exclusions, and sets the multiplier to use. * * @param {object} syllableRegex The object containing the syllable exclusions and multiplier. * @returns {void} */ SyllableCountStep.prototype.createRegex = function( syllableRegex ) { if ( ! isUndefined( syllableRegex ) && ! isUndefined( syllableRegex.fragments ) ) { this._hasRegex = true; this._regex = arrayToRegex( syllableRegex.fragments, true ); this._multiplier = syllableRegex.countModifier; } }; /** * Returns the stored regular expression. * * @returns {RegExp} The stored regular expression. */ SyllableCountStep.prototype.getRegex = function() { return this._regex; }; /** * Matches syllable exclusions in a given word and the returns the number found multiplied with the * given multiplier. * * @param {String} word The word to match for syllable exclusions. * @returns {number} The amount of syllables found. */ SyllableCountStep.prototype.countSyllables = function( word ) { if ( this._hasRegex ) { const match = word.match( this._regex ) || []; return match.length * this._multiplier; } return 0; }; export default SyllableCountStep;