yoastseo
Version:
Yoast client-side content analysis
47 lines (44 loc) • 2.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = _default;
var _sanitizeString = _interopRequireDefault(require("../sanitize/sanitizeString"));
var _lodash = require("lodash");
var _removePunctuation = _interopRequireWildcard(require("../sanitize/removePunctuation.js"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/** @module stringProcessing/countWords */
const punctuationRegex = new RegExp(`([${_removePunctuation.punctuationRegexString}])`, "g");
/**
* Returns an array with words used in the text.
*
* @param {string} text The text to be counted.
* @param {string} [wordBoundaryRegexString=\\s] The regex string for the word boundary that should be used to split the text into words.
* @param {boolean} [shouldRemovePunctuation=true] If punctuation should be removed. Defaults to `true`.
*
* @returns {Array} The array with all words.
*/
function _default(text, wordBoundaryRegexString = "\\s", shouldRemovePunctuation = true) {
// Unify whitespaces and non-breaking spaces, remove table of content and strip the tags and multiple spaces.
text = (0, _sanitizeString.default)(text);
if (text === "") {
return [];
}
const wordBoundaryRegex = new RegExp(wordBoundaryRegexString, "g");
let words = text.split(wordBoundaryRegex);
if (shouldRemovePunctuation) {
words = words.map(_removePunctuation.default);
} else {
// If punctuation is not removed, punctuation marks are tokenized as if they were words.
words = (0, _lodash.flatMap)(words, word => {
const newWord = word.replace(punctuationRegex, " $1 ");
return newWord.split(" ");
});
}
return (0, _lodash.filter)(words, function (word) {
return word.trim() !== "";
});
}
//# sourceMappingURL=getWords.js.map