UNPKG

yoastseo

Version:

Yoast client-side content analysis

47 lines (44 loc) 2.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = _default; var _stripNonTextTags = _interopRequireDefault(require("../sanitize/stripNonTextTags.js")); var _stripSpaces = _interopRequireDefault(require("../sanitize/stripSpaces.js")); var _removePunctuation = _interopRequireDefault(require("../sanitize/removePunctuation.js")); var _unifyWhitespace = require("../sanitize/unifyWhitespace.js"); var _matchTextWithTransliteration = _interopRequireDefault(require("./matchTextWithTransliteration.js")); var _quotes = require("../sanitize/quotes.js"); var _lodash = require("lodash"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } /** @module stringProcessing/matchTextWithWord */ /** * Returns the number of matches in a given string * * @param {string} text The text to use for matching the wordToMatch. * @param {string} wordToMatch The word to match in the text. * @param {string} locale The locale used for transliteration. * @param {function} matchWordCustomHelper The helper function to match word in text. * * @returns {Object} An array with all matches of the text, the number of the matches, and the lowest number of positions of the matches. */ function _default(text, wordToMatch, locale, matchWordCustomHelper) { text = (0, _stripNonTextTags.default)(text); text = (0, _unifyWhitespace.unifyAllSpaces)(text); text = (0, _quotes.normalize)(text); wordToMatch = (0, _quotes.normalize)(wordToMatch); let matches = matchWordCustomHelper ? matchWordCustomHelper(text, wordToMatch) : (0, _matchTextWithTransliteration.default)(text, wordToMatch, locale); matches = (0, _lodash.map)(matches, function (keyword) { return (0, _stripSpaces.default)((0, _removePunctuation.default)(keyword)); }); // Create an array of positions of matches to determine where in the text the wordToMatch occurred first. const positions = (0, _lodash.map)(matches, function (keyword) { return text.indexOf(keyword); }); return { count: matches.length, matches: matches, position: positions.length === 0 ? -1 : Math.min(...positions) }; } //# sourceMappingURL=matchTextWithWord.js.map