Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@wordpress/wordcount

Package Overview
Dependencies
Maintainers
16
Versions
159
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@wordpress/wordcount - npm Package Compare versions

Comparing version 2.12.0 to 2.13.0

build-types/defaultSettings.d.ts

38

build-module/defaultSettings.js

@@ -0,1 +1,39 @@

/** @typedef {import('./index').WPWordCountStrategy} WPWordCountStrategy */
/** @typedef {Partial<{type: WPWordCountStrategy, shortcodes: string[]}>} WPWordCountL10n */
/**
* @typedef WPWordCountSettingsFields
* @property {RegExp} HTMLRegExp Regular expression that matches HTML tags
* @property {RegExp} HTMLcommentRegExp Regular expression that matches HTML comments
* @property {RegExp} spaceRegExp Regular expression that matches spaces in HTML
* @property {RegExp} HTMLEntityRegExp Regular expression that matches HTML entities
* @property {RegExp} connectorRegExp Regular expression that matches word connectors, like em-dash
* @property {RegExp} removeRegExp Regular expression that matches various characters to be removed when counting
* @property {RegExp} astralRegExp Regular expression that matches astral UTF-16 code points
* @property {RegExp} wordsRegExp Regular expression that matches words
* @property {RegExp} characters_excluding_spacesRegExp Regular expression that matches characters excluding spaces
* @property {RegExp} characters_including_spacesRegExp Regular expression that matches characters including spaces
* @property {RegExp} shortcodesRegExp Regular expression that matches WordPress shortcodes
* @property {string[]} shortcodes List of all shortcodes
* @property {WPWordCountStrategy} type Describes what and how are we counting
* @property {WPWordCountL10n} l10n Object with human translations
*/
/**
* Lower-level settings for word counting that can be overridden.
*
* @typedef {Partial<WPWordCountSettingsFields>} WPWordCountUserSettings
*/
// Disable reason: JSDoc linter doesn't seem to parse the union (`&`) correctly: https://github.com/jsdoc/jsdoc/issues/1285
/* eslint-disable jsdoc/valid-types */
/**
* Word counting settings that include non-optional values we set if missing
*
* @typedef {WPWordCountUserSettings & typeof defaultSettings} WPWordCountDefaultSettings
*/
/* eslint-enable jsdoc/valid-types */
export var defaultSettings = {

@@ -2,0 +40,0 @@ HTMLRegExp: /<\/?[a-z][^>]*?>/gi,

92

build-module/index.js

@@ -20,14 +20,27 @@ /**

/**
* @typedef {import('./defaultSettings').WPWordCountDefaultSettings} WPWordCountSettings
* @typedef {import('./defaultSettings').WPWordCountUserSettings} WPWordCountUserSettings
*/
/**
* Possible ways of counting.
*
* @typedef {'words'|'characters_excluding_spaces'|'characters_including_spaces'} WPWordCountStrategy
*/
/**
* Private function to manage the settings.
*
* @param {string} type The type of count to be done.
* @param {Object} userSettings Custom settings for the count.
* @param {WPWordCountStrategy} type The type of count to be done.
* @param {WPWordCountUserSettings} userSettings Custom settings for the count.
*
* @return {void|Object|*} The combined settings object to be used.
* @return {WPWordCountSettings} The combined settings object to be used.
*/
function loadSettings(type, userSettings) {
var settings = extend(defaultSettings, userSettings);
settings.shortcodes = settings.l10n.shortcodes || {};
var _settings$l10n$shortc, _settings$l10n;
var settings = extend({}, defaultSettings, userSettings);
settings.shortcodes = (_settings$l10n$shortc = (_settings$l10n = settings.l10n) === null || _settings$l10n === void 0 ? void 0 : _settings$l10n.shortcodes) !== null && _settings$l10n$shortc !== void 0 ? _settings$l10n$shortc : [];
if (settings.shortcodes && settings.shortcodes.length) {

@@ -37,3 +50,3 @@ settings.shortcodesRegExp = new RegExp('\\[\\/?(?:' + settings.shortcodes.join('|') + ')[^\\]]*?\\]', 'g');

settings.type = type || settings.l10n.type;
settings.type = type;

@@ -47,32 +60,36 @@ if (settings.type !== 'characters_excluding_spaces' && settings.type !== 'characters_including_spaces') {

/**
* Match the regex for the type 'words'
* Count the words in text
*
* @param {string} text The text being processed
* @param {string} regex The regular expression pattern being matched
* @param {Object} settings Settings object containing regular expressions for each strip function
* @param {string} text The text being processed
* @param {RegExp} regex The regular expression pattern being matched
* @param {WPWordCountSettings} settings Settings object containing regular expressions for each strip function
*
* @return {Array|{index: number, input: string}} The matched string.
* @return {number} Count of words.
*/
function matchWords(text, regex, settings) {
text = flow(stripTags.bind(this, settings), stripHTMLComments.bind(this, settings), stripShortcodes.bind(this, settings), stripSpaces.bind(this, settings), stripHTMLEntities.bind(this, settings), stripConnectors.bind(this, settings), stripRemovables.bind(this, settings))(text);
function countWords(text, regex, settings) {
var _text$match$length, _text$match;
text = flow(stripTags.bind(null, settings), stripHTMLComments.bind(null, settings), stripShortcodes.bind(null, settings), stripSpaces.bind(null, settings), stripHTMLEntities.bind(null, settings), stripConnectors.bind(null, settings), stripRemovables.bind(null, settings))(text);
text = text + '\n';
return text.match(regex);
return (_text$match$length = (_text$match = text.match(regex)) === null || _text$match === void 0 ? void 0 : _text$match.length) !== null && _text$match$length !== void 0 ? _text$match$length : 0;
}
/**
* Match the regex for either 'characters_excluding_spaces' or 'characters_including_spaces'
* Count the characters in text
*
* @param {string} text The text being processed
* @param {string} regex The regular expression pattern being matched
* @param {Object} settings Settings object containing regular expressions for each strip function
* @param {string} text The text being processed
* @param {RegExp} regex The regular expression pattern being matched
* @param {WPWordCountSettings} settings Settings object containing regular expressions for each strip function
*
* @return {Array|{index: number, input: string}} The matched string.
* @return {number} Count of characters.
*/
function matchCharacters(text, regex, settings) {
text = flow(stripTags.bind(this, settings), stripHTMLComments.bind(this, settings), stripShortcodes.bind(this, settings), stripSpaces.bind(this, settings), transposeAstralsToCountableChar.bind(this, settings), transposeHTMLEntitiesToCountableChars.bind(this, settings))(text);
function countCharacters(text, regex, settings) {
var _text$match$length2, _text$match2;
text = flow(stripTags.bind(null, settings), stripHTMLComments.bind(null, settings), stripShortcodes.bind(null, settings), transposeAstralsToCountableChar.bind(null, settings), stripSpaces.bind(null, settings), transposeHTMLEntitiesToCountableChars.bind(null, settings))(text);
text = text + '\n';
return text.match(regex);
return (_text$match$length2 = (_text$match2 = text.match(regex)) === null || _text$match2 === void 0 ? void 0 : _text$match2.length) !== null && _text$match$length2 !== void 0 ? _text$match$length2 : 0;
}

@@ -82,5 +99,5 @@ /**

*
* @param {string} text The text being processed
* @param {string} type The type of count. Accepts ;words', 'characters_excluding_spaces', or 'characters_including_spaces'.
* @param {Object} userSettings Custom settings object.
* @param {string} text The text being processed
* @param {WPWordCountStrategy} type The type of count. Accepts 'words', 'characters_excluding_spaces', or 'characters_including_spaces'.
* @param {WPWordCountUserSettings} userSettings Custom settings object.
*

@@ -98,13 +115,22 @@ * @example

export function count(text, type, userSettings) {
if ('' === text) {
return 0;
}
var settings = loadSettings(type, userSettings);
var matchRegExp;
if (text) {
var settings = loadSettings(type, userSettings);
var matchRegExp = settings[type + 'RegExp'];
var results = 'words' === settings.type ? matchWords(text, matchRegExp, settings) : matchCharacters(text, matchRegExp, settings);
return results ? results.length : 0;
switch (settings.type) {
case 'words':
matchRegExp = settings.wordsRegExp;
return countWords(text, matchRegExp, settings);
case 'characters_including_spaces':
matchRegExp = settings.characters_including_spacesRegExp;
return countCharacters(text, matchRegExp, settings);
case 'characters_excluding_spaces':
matchRegExp = settings.characters_excluding_spacesRegExp;
return countCharacters(text, matchRegExp, settings);
default:
return 0;
}
}
//# sourceMappingURL=index.js.map
/**
* Replaces items matched in the regex with spaces.
*
* @param {Object} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
* @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
*

@@ -10,8 +10,4 @@ * @return {string} The manipulated text.

export default function stripConnectors(settings, text) {
if (settings.connectorRegExp) {
return text.replace(settings.connectorRegExp, ' ');
}
return text;
return text.replace(settings.connectorRegExp, ' ');
}
//# sourceMappingURL=stripConnectors.js.map
/**
* Removes items matched in the regex.
*
* @param {Object} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
* @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
*

@@ -10,8 +10,4 @@ * @return {string} The manipulated text.

export default function stripHTMLComments(settings, text) {
if (settings.HTMLcommentRegExp) {
return text.replace(settings.HTMLcommentRegExp, '');
}
return text;
return text.replace(settings.HTMLcommentRegExp, '');
}
//# sourceMappingURL=stripHTMLComments.js.map
/**
* Removes items matched in the regex.
*
* @param {Object} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
* @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
*

@@ -10,8 +10,4 @@ * @return {string} The manipulated text.

export default function stripHTMLEntities(settings, text) {
if (settings.HTMLEntityRegExp) {
return text.replace(settings.HTMLEntityRegExp, '');
}
return text;
return text.replace(settings.HTMLEntityRegExp, '');
}
//# sourceMappingURL=stripHTMLEntities.js.map
/**
* Removes items matched in the regex.
*
* @param {Object} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
* @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
*

@@ -10,8 +10,4 @@ * @return {string} The manipulated text.

export default function stripRemovables(settings, text) {
if (settings.removeRegExp) {
return text.replace(settings.removeRegExp, '');
}
return text;
return text.replace(settings.removeRegExp, '');
}
//# sourceMappingURL=stripRemovables.js.map
/**
* Replaces items matched in the regex with a new line.
*
* @param {Object} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
* @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
*

@@ -7,0 +7,0 @@ * @return {string} The manipulated text.

/**
* Replaces items matched in the regex with spaces.
*
* @param {Object} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
* @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
*

@@ -10,6 +10,4 @@ * @return {string} The manipulated text.

export default function stripSpaces(settings, text) {
if (settings.spaceRegExp) {
return text.replace(settings.spaceRegExp, ' ');
}
return text.replace(settings.spaceRegExp, ' ');
}
//# sourceMappingURL=stripSpaces.js.map
/**
* Replaces items matched in the regex with new line
*
* @param {Object} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
* @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
*

@@ -10,6 +10,4 @@ * @return {string} The manipulated text.

export default function stripTags(settings, text) {
if (settings.HTMLRegExp) {
return text.replace(settings.HTMLRegExp, '\n');
}
return text.replace(settings.HTMLRegExp, '\n');
}
//# sourceMappingURL=stripTags.js.map
/**
* Replaces items matched in the regex with character.
*
* @param {Object} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
* @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
*

@@ -10,8 +10,4 @@ * @return {string} The manipulated text.

export default function transposeAstralsToCountableChar(settings, text) {
if (settings.astralRegExp) {
return text.replace(settings.astralRegExp, 'a');
}
return text;
return text.replace(settings.astralRegExp, 'a');
}
//# sourceMappingURL=transposeAstralsToCountableChar.js.map
/**
* Replaces items matched in the regex with a single character.
*
* @param {Object} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
* @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
*

@@ -10,8 +10,4 @@ * @return {string} The manipulated text.

export default function transposeHTMLEntitiesToCountableChars(settings, text) {
if (settings.HTMLEntityRegExp) {
return text.replace(settings.HTMLEntityRegExp, 'a');
}
return text;
return text.replace(settings.HTMLEntityRegExp, 'a');
}
//# sourceMappingURL=transposeHTMLEntitiesToCountableChars.js.map

@@ -7,2 +7,41 @@ "use strict";

exports.defaultSettings = void 0;
/** @typedef {import('./index').WPWordCountStrategy} WPWordCountStrategy */
/** @typedef {Partial<{type: WPWordCountStrategy, shortcodes: string[]}>} WPWordCountL10n */
/**
* @typedef WPWordCountSettingsFields
* @property {RegExp} HTMLRegExp Regular expression that matches HTML tags
* @property {RegExp} HTMLcommentRegExp Regular expression that matches HTML comments
* @property {RegExp} spaceRegExp Regular expression that matches spaces in HTML
* @property {RegExp} HTMLEntityRegExp Regular expression that matches HTML entities
* @property {RegExp} connectorRegExp Regular expression that matches word connectors, like em-dash
* @property {RegExp} removeRegExp Regular expression that matches various characters to be removed when counting
* @property {RegExp} astralRegExp Regular expression that matches astral UTF-16 code points
* @property {RegExp} wordsRegExp Regular expression that matches words
* @property {RegExp} characters_excluding_spacesRegExp Regular expression that matches characters excluding spaces
* @property {RegExp} characters_including_spacesRegExp Regular expression that matches characters including spaces
* @property {RegExp} shortcodesRegExp Regular expression that matches WordPress shortcodes
* @property {string[]} shortcodes List of all shortcodes
* @property {WPWordCountStrategy} type Describes what and how are we counting
* @property {WPWordCountL10n} l10n Object with human translations
*/
/**
* Lower-level settings for word counting that can be overridden.
*
* @typedef {Partial<WPWordCountSettingsFields>} WPWordCountUserSettings
*/
// Disable reason: JSDoc linter doesn't seem to parse the union (`&`) correctly: https://github.com/jsdoc/jsdoc/issues/1285
/* eslint-disable jsdoc/valid-types */
/**
* Word counting settings that include non-optional values we set if missing
*
* @typedef {WPWordCountUserSettings & typeof defaultSettings} WPWordCountDefaultSettings
*/
/* eslint-enable jsdoc/valid-types */
var defaultSettings = {

@@ -9,0 +48,0 @@ HTMLRegExp: /<\/?[a-z][^>]*?>/gi,

@@ -41,13 +41,26 @@ "use strict";

/**
* @typedef {import('./defaultSettings').WPWordCountDefaultSettings} WPWordCountSettings
* @typedef {import('./defaultSettings').WPWordCountUserSettings} WPWordCountUserSettings
*/
/**
* Possible ways of counting.
*
* @typedef {'words'|'characters_excluding_spaces'|'characters_including_spaces'} WPWordCountStrategy
*/
/**
* Private function to manage the settings.
*
* @param {string} type The type of count to be done.
* @param {Object} userSettings Custom settings for the count.
* @param {WPWordCountStrategy} type The type of count to be done.
* @param {WPWordCountUserSettings} userSettings Custom settings for the count.
*
* @return {void|Object|*} The combined settings object to be used.
* @return {WPWordCountSettings} The combined settings object to be used.
*/
function loadSettings(type, userSettings) {
var settings = (0, _lodash.extend)(_defaultSettings.defaultSettings, userSettings);
settings.shortcodes = settings.l10n.shortcodes || {};
var _settings$l10n$shortc, _settings$l10n;
var settings = (0, _lodash.extend)({}, _defaultSettings.defaultSettings, userSettings);
settings.shortcodes = (_settings$l10n$shortc = (_settings$l10n = settings.l10n) === null || _settings$l10n === void 0 ? void 0 : _settings$l10n.shortcodes) !== null && _settings$l10n$shortc !== void 0 ? _settings$l10n$shortc : [];
if (settings.shortcodes && settings.shortcodes.length) {

@@ -57,3 +70,3 @@ settings.shortcodesRegExp = new RegExp('\\[\\/?(?:' + settings.shortcodes.join('|') + ')[^\\]]*?\\]', 'g');

settings.type = type || settings.l10n.type;
settings.type = type;

@@ -67,32 +80,36 @@ if (settings.type !== 'characters_excluding_spaces' && settings.type !== 'characters_including_spaces') {

/**
* Match the regex for the type 'words'
* Count the words in text
*
* @param {string} text The text being processed
* @param {string} regex The regular expression pattern being matched
* @param {Object} settings Settings object containing regular expressions for each strip function
* @param {string} text The text being processed
* @param {RegExp} regex The regular expression pattern being matched
* @param {WPWordCountSettings} settings Settings object containing regular expressions for each strip function
*
* @return {Array|{index: number, input: string}} The matched string.
* @return {number} Count of words.
*/
function matchWords(text, regex, settings) {
text = (0, _lodash.flow)(_stripTags.default.bind(this, settings), _stripHTMLComments.default.bind(this, settings), _stripShortcodes.default.bind(this, settings), _stripSpaces.default.bind(this, settings), _stripHTMLEntities.default.bind(this, settings), _stripConnectors.default.bind(this, settings), _stripRemovables.default.bind(this, settings))(text);
function countWords(text, regex, settings) {
var _text$match$length, _text$match;
text = (0, _lodash.flow)(_stripTags.default.bind(null, settings), _stripHTMLComments.default.bind(null, settings), _stripShortcodes.default.bind(null, settings), _stripSpaces.default.bind(null, settings), _stripHTMLEntities.default.bind(null, settings), _stripConnectors.default.bind(null, settings), _stripRemovables.default.bind(null, settings))(text);
text = text + '\n';
return text.match(regex);
return (_text$match$length = (_text$match = text.match(regex)) === null || _text$match === void 0 ? void 0 : _text$match.length) !== null && _text$match$length !== void 0 ? _text$match$length : 0;
}
/**
* Match the regex for either 'characters_excluding_spaces' or 'characters_including_spaces'
* Count the characters in text
*
* @param {string} text The text being processed
* @param {string} regex The regular expression pattern being matched
* @param {Object} settings Settings object containing regular expressions for each strip function
* @param {string} text The text being processed
* @param {RegExp} regex The regular expression pattern being matched
* @param {WPWordCountSettings} settings Settings object containing regular expressions for each strip function
*
* @return {Array|{index: number, input: string}} The matched string.
* @return {number} Count of characters.
*/
function matchCharacters(text, regex, settings) {
text = (0, _lodash.flow)(_stripTags.default.bind(this, settings), _stripHTMLComments.default.bind(this, settings), _stripShortcodes.default.bind(this, settings), _stripSpaces.default.bind(this, settings), _transposeAstralsToCountableChar.default.bind(this, settings), _transposeHTMLEntitiesToCountableChars.default.bind(this, settings))(text);
function countCharacters(text, regex, settings) {
var _text$match$length2, _text$match2;
text = (0, _lodash.flow)(_stripTags.default.bind(null, settings), _stripHTMLComments.default.bind(null, settings), _stripShortcodes.default.bind(null, settings), _transposeAstralsToCountableChar.default.bind(null, settings), _stripSpaces.default.bind(null, settings), _transposeHTMLEntitiesToCountableChars.default.bind(null, settings))(text);
text = text + '\n';
return text.match(regex);
return (_text$match$length2 = (_text$match2 = text.match(regex)) === null || _text$match2 === void 0 ? void 0 : _text$match2.length) !== null && _text$match$length2 !== void 0 ? _text$match$length2 : 0;
}

@@ -102,5 +119,5 @@ /**

*
* @param {string} text The text being processed
* @param {string} type The type of count. Accepts ;words', 'characters_excluding_spaces', or 'characters_including_spaces'.
* @param {Object} userSettings Custom settings object.
* @param {string} text The text being processed
* @param {WPWordCountStrategy} type The type of count. Accepts 'words', 'characters_excluding_spaces', or 'characters_including_spaces'.
* @param {WPWordCountUserSettings} userSettings Custom settings object.
*

@@ -118,13 +135,22 @@ * @example

function count(text, type, userSettings) {
if ('' === text) {
return 0;
}
var settings = loadSettings(type, userSettings);
var matchRegExp;
if (text) {
var settings = loadSettings(type, userSettings);
var matchRegExp = settings[type + 'RegExp'];
var results = 'words' === settings.type ? matchWords(text, matchRegExp, settings) : matchCharacters(text, matchRegExp, settings);
return results ? results.length : 0;
switch (settings.type) {
case 'words':
matchRegExp = settings.wordsRegExp;
return countWords(text, matchRegExp, settings);
case 'characters_including_spaces':
matchRegExp = settings.characters_including_spacesRegExp;
return countCharacters(text, matchRegExp, settings);
case 'characters_excluding_spaces':
matchRegExp = settings.characters_excluding_spacesRegExp;
return countCharacters(text, matchRegExp, settings);
default:
return 0;
}
}
//# sourceMappingURL=index.js.map

@@ -11,4 +11,4 @@ "use strict";

*
* @param {Object} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
* @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
*

@@ -18,8 +18,4 @@ * @return {string} The manipulated text.

function stripConnectors(settings, text) {
if (settings.connectorRegExp) {
return text.replace(settings.connectorRegExp, ' ');
}
return text;
return text.replace(settings.connectorRegExp, ' ');
}
//# sourceMappingURL=stripConnectors.js.map

@@ -11,4 +11,4 @@ "use strict";

*
* @param {Object} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
* @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
*

@@ -18,8 +18,4 @@ * @return {string} The manipulated text.

function stripHTMLComments(settings, text) {
if (settings.HTMLcommentRegExp) {
return text.replace(settings.HTMLcommentRegExp, '');
}
return text;
return text.replace(settings.HTMLcommentRegExp, '');
}
//# sourceMappingURL=stripHTMLComments.js.map

@@ -11,4 +11,4 @@ "use strict";

*
* @param {Object} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
* @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
*

@@ -18,8 +18,4 @@ * @return {string} The manipulated text.

function stripHTMLEntities(settings, text) {
if (settings.HTMLEntityRegExp) {
return text.replace(settings.HTMLEntityRegExp, '');
}
return text;
return text.replace(settings.HTMLEntityRegExp, '');
}
//# sourceMappingURL=stripHTMLEntities.js.map

@@ -11,4 +11,4 @@ "use strict";

*
* @param {Object} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
* @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
*

@@ -18,8 +18,4 @@ * @return {string} The manipulated text.

function stripRemovables(settings, text) {
if (settings.removeRegExp) {
return text.replace(settings.removeRegExp, '');
}
return text;
return text.replace(settings.removeRegExp, '');
}
//# sourceMappingURL=stripRemovables.js.map

@@ -11,4 +11,4 @@ "use strict";

*
* @param {Object} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
* @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
*

@@ -15,0 +15,0 @@ * @return {string} The manipulated text.

@@ -11,4 +11,4 @@ "use strict";

*
* @param {Object} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
* @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
*

@@ -18,6 +18,4 @@ * @return {string} The manipulated text.

function stripSpaces(settings, text) {
if (settings.spaceRegExp) {
return text.replace(settings.spaceRegExp, ' ');
}
return text.replace(settings.spaceRegExp, ' ');
}
//# sourceMappingURL=stripSpaces.js.map

@@ -11,4 +11,4 @@ "use strict";

*
* @param {Object} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
* @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
*

@@ -18,6 +18,4 @@ * @return {string} The manipulated text.

function stripTags(settings, text) {
if (settings.HTMLRegExp) {
return text.replace(settings.HTMLRegExp, '\n');
}
return text.replace(settings.HTMLRegExp, '\n');
}
//# sourceMappingURL=stripTags.js.map

@@ -11,4 +11,4 @@ "use strict";

*
* @param {Object} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
* @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
*

@@ -18,8 +18,4 @@ * @return {string} The manipulated text.

function transposeAstralsToCountableChar(settings, text) {
if (settings.astralRegExp) {
return text.replace(settings.astralRegExp, 'a');
}
return text;
return text.replace(settings.astralRegExp, 'a');
}
//# sourceMappingURL=transposeAstralsToCountableChar.js.map

@@ -11,4 +11,4 @@ "use strict";

*
* @param {Object} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
* @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
*

@@ -18,8 +18,4 @@ * @return {string} The manipulated text.

function transposeHTMLEntitiesToCountableChars(settings, text) {
if (settings.HTMLEntityRegExp) {
return text.replace(settings.HTMLEntityRegExp, 'a');
}
return text;
return text.replace(settings.HTMLEntityRegExp, 'a');
}
//# sourceMappingURL=transposeHTMLEntitiesToCountableChars.js.map
{
"name": "@wordpress/wordcount",
"version": "2.12.0",
"version": "2.13.0",
"description": "WordPress word count utility.",

@@ -32,3 +32,3 @@ "author": "The WordPress Contributors",

},
"gitHead": "cbcd167ffb9b67f87a79df71d95aa48dc4db1a64"
"gitHead": "68a2e6dc7d154f9d33dce06152a8a5bdd28b6e90"
}

@@ -33,4 +33,4 @@ # Word Count

- _text_ `string`: The text being processed
- _type_ `string`: The type of count. Accepts ;words', 'characters_excluding_spaces', or 'characters_including_spaces'.
- _userSettings_ `Object`: Custom settings object.
- _type_ `WPWordCountStrategy`: The type of count. Accepts 'words', 'characters_excluding_spaces', or 'characters_including_spaces'.
- _userSettings_ `WPWordCountUserSettings`: Custom settings object.

@@ -37,0 +37,0 @@ _Returns_

@@ -0,1 +1,38 @@

/** @typedef {import('./index').WPWordCountStrategy} WPWordCountStrategy */
/** @typedef {Partial<{type: WPWordCountStrategy, shortcodes: string[]}>} WPWordCountL10n */
/**
* @typedef WPWordCountSettingsFields
* @property {RegExp} HTMLRegExp Regular expression that matches HTML tags
* @property {RegExp} HTMLcommentRegExp Regular expression that matches HTML comments
* @property {RegExp} spaceRegExp Regular expression that matches spaces in HTML
* @property {RegExp} HTMLEntityRegExp Regular expression that matches HTML entities
* @property {RegExp} connectorRegExp Regular expression that matches word connectors, like em-dash
* @property {RegExp} removeRegExp Regular expression that matches various characters to be removed when counting
* @property {RegExp} astralRegExp Regular expression that matches astral UTF-16 code points
* @property {RegExp} wordsRegExp Regular expression that matches words
* @property {RegExp} characters_excluding_spacesRegExp Regular expression that matches characters excluding spaces
* @property {RegExp} characters_including_spacesRegExp Regular expression that matches characters including spaces
* @property {RegExp} shortcodesRegExp Regular expression that matches WordPress shortcodes
* @property {string[]} shortcodes List of all shortcodes
* @property {WPWordCountStrategy} type Describes what and how are we counting
* @property {WPWordCountL10n} l10n Object with human translations
*/
/**
* Lower-level settings for word counting that can be overridden.
*
* @typedef {Partial<WPWordCountSettingsFields>} WPWordCountUserSettings
*/
// Disable reason: JSDoc linter doesn't seem to parse the union (`&`) correctly: https://github.com/jsdoc/jsdoc/issues/1285
/* eslint-disable jsdoc/valid-types */
/**
* Word counting settings that include non-optional values we set if missing
*
* @typedef {WPWordCountUserSettings & typeof defaultSettings} WPWordCountDefaultSettings
*/
/* eslint-enable jsdoc/valid-types */
export const defaultSettings = {

@@ -2,0 +39,0 @@ HTMLRegExp: /<\/?[a-z][^>]*?>/gi,

@@ -21,13 +21,24 @@ /**

/**
* @typedef {import('./defaultSettings').WPWordCountDefaultSettings} WPWordCountSettings
* @typedef {import('./defaultSettings').WPWordCountUserSettings} WPWordCountUserSettings
*/
/**
* Possible ways of counting.
*
* @typedef {'words'|'characters_excluding_spaces'|'characters_including_spaces'} WPWordCountStrategy
*/
/**
* Private function to manage the settings.
*
* @param {string} type The type of count to be done.
* @param {Object} userSettings Custom settings for the count.
* @param {WPWordCountStrategy} type The type of count to be done.
* @param {WPWordCountUserSettings} userSettings Custom settings for the count.
*
* @return {void|Object|*} The combined settings object to be used.
* @return {WPWordCountSettings} The combined settings object to be used.
*/
function loadSettings( type, userSettings ) {
const settings = extend( defaultSettings, userSettings );
const settings = extend( {}, defaultSettings, userSettings );
settings.shortcodes = settings.l10n.shortcodes || {};
settings.shortcodes = settings.l10n?.shortcodes ?? [];

@@ -41,3 +52,3 @@ if ( settings.shortcodes && settings.shortcodes.length ) {

settings.type = type || settings.l10n.type;
settings.type = type;

@@ -55,44 +66,44 @@ if (

/**
* Match the regex for the type 'words'
* Count the words in text
*
* @param {string} text The text being processed
* @param {string} regex The regular expression pattern being matched
* @param {Object} settings Settings object containing regular expressions for each strip function
* @param {string} text The text being processed
* @param {RegExp} regex The regular expression pattern being matched
* @param {WPWordCountSettings} settings Settings object containing regular expressions for each strip function
*
* @return {Array|{index: number, input: string}} The matched string.
* @return {number} Count of words.
*/
function matchWords( text, regex, settings ) {
function countWords( text, regex, settings ) {
text = flow(
stripTags.bind( this, settings ),
stripHTMLComments.bind( this, settings ),
stripShortcodes.bind( this, settings ),
stripSpaces.bind( this, settings ),
stripHTMLEntities.bind( this, settings ),
stripConnectors.bind( this, settings ),
stripRemovables.bind( this, settings )
stripTags.bind( null, settings ),
stripHTMLComments.bind( null, settings ),
stripShortcodes.bind( null, settings ),
stripSpaces.bind( null, settings ),
stripHTMLEntities.bind( null, settings ),
stripConnectors.bind( null, settings ),
stripRemovables.bind( null, settings )
)( text );
text = text + '\n';
return text.match( regex );
return text.match( regex )?.length ?? 0;
}
/**
* Match the regex for either 'characters_excluding_spaces' or 'characters_including_spaces'
* Count the characters in text
*
* @param {string} text The text being processed
* @param {string} regex The regular expression pattern being matched
* @param {Object} settings Settings object containing regular expressions for each strip function
* @param {string} text The text being processed
* @param {RegExp} regex The regular expression pattern being matched
* @param {WPWordCountSettings} settings Settings object containing regular expressions for each strip function
*
* @return {Array|{index: number, input: string}} The matched string.
* @return {number} Count of characters.
*/
function matchCharacters( text, regex, settings ) {
function countCharacters( text, regex, settings ) {
text = flow(
stripTags.bind( this, settings ),
stripHTMLComments.bind( this, settings ),
stripShortcodes.bind( this, settings ),
stripSpaces.bind( this, settings ),
transposeAstralsToCountableChar.bind( this, settings ),
transposeHTMLEntitiesToCountableChars.bind( this, settings )
stripTags.bind( null, settings ),
stripHTMLComments.bind( null, settings ),
stripShortcodes.bind( null, settings ),
transposeAstralsToCountableChar.bind( null, settings ),
stripSpaces.bind( null, settings ),
transposeHTMLEntitiesToCountableChars.bind( null, settings )
)( text );
text = text + '\n';
return text.match( regex );
return text.match( regex )?.length ?? 0;
}

@@ -103,5 +114,5 @@

*
* @param {string} text The text being processed
* @param {string} type The type of count. Accepts ;words', 'characters_excluding_spaces', or 'characters_including_spaces'.
* @param {Object} userSettings Custom settings object.
* @param {string} text The text being processed
* @param {WPWordCountStrategy} type The type of count. Accepts 'words', 'characters_excluding_spaces', or 'characters_including_spaces'.
* @param {WPWordCountUserSettings} userSettings Custom settings object.
*

@@ -116,18 +127,18 @@ * @example

*/
export function count( text, type, userSettings ) {
if ( '' === text ) {
return 0;
const settings = loadSettings( type, userSettings );
let matchRegExp;
switch ( settings.type ) {
case 'words':
matchRegExp = settings.wordsRegExp;
return countWords( text, matchRegExp, settings );
case 'characters_including_spaces':
matchRegExp = settings.characters_including_spacesRegExp;
return countCharacters( text, matchRegExp, settings );
case 'characters_excluding_spaces':
matchRegExp = settings.characters_excluding_spacesRegExp;
return countCharacters( text, matchRegExp, settings );
default:
return 0;
}
if ( text ) {
const settings = loadSettings( type, userSettings );
const matchRegExp = settings[ type + 'RegExp' ];
const results =
'words' === settings.type
? matchWords( text, matchRegExp, settings )
: matchCharacters( text, matchRegExp, settings );
return results ? results.length : 0;
}
}
/**
* Replaces items matched in the regex with spaces.
*
* @param {Object} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
* @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
*

@@ -10,6 +10,3 @@ * @return {string} The manipulated text.

export default function stripConnectors( settings, text ) {
if ( settings.connectorRegExp ) {
return text.replace( settings.connectorRegExp, ' ' );
}
return text;
return text.replace( settings.connectorRegExp, ' ' );
}
/**
* Removes items matched in the regex.
*
* @param {Object} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
* @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
*

@@ -10,6 +10,3 @@ * @return {string} The manipulated text.

export default function stripHTMLComments( settings, text ) {
if ( settings.HTMLcommentRegExp ) {
return text.replace( settings.HTMLcommentRegExp, '' );
}
return text;
return text.replace( settings.HTMLcommentRegExp, '' );
}
/**
* Removes items matched in the regex.
*
* @param {Object} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
* @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
*

@@ -10,6 +10,3 @@ * @return {string} The manipulated text.

export default function stripHTMLEntities( settings, text ) {
if ( settings.HTMLEntityRegExp ) {
return text.replace( settings.HTMLEntityRegExp, '' );
}
return text;
return text.replace( settings.HTMLEntityRegExp, '' );
}
/**
* Removes items matched in the regex.
*
* @param {Object} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
* @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
*

@@ -10,6 +10,3 @@ * @return {string} The manipulated text.

export default function stripRemovables( settings, text ) {
if ( settings.removeRegExp ) {
return text.replace( settings.removeRegExp, '' );
}
return text;
return text.replace( settings.removeRegExp, '' );
}
/**
* Replaces items matched in the regex with a new line.
*
* @param {Object} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
* @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
*

@@ -7,0 +7,0 @@ * @return {string} The manipulated text.

/**
* Replaces items matched in the regex with spaces.
*
* @param {Object} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
* @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
*

@@ -10,5 +10,3 @@ * @return {string} The manipulated text.

export default function stripSpaces( settings, text ) {
if ( settings.spaceRegExp ) {
return text.replace( settings.spaceRegExp, ' ' );
}
return text.replace( settings.spaceRegExp, ' ' );
}
/**
* Replaces items matched in the regex with new line
*
* @param {Object} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
* @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
*

@@ -10,5 +10,3 @@ * @return {string} The manipulated text.

export default function stripTags( settings, text ) {
if ( settings.HTMLRegExp ) {
return text.replace( settings.HTMLRegExp, '\n' );
}
return text.replace( settings.HTMLRegExp, '\n' );
}
/**
* Replaces items matched in the regex with character.
*
* @param {Object} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
* @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
*

@@ -10,6 +10,3 @@ * @return {string} The manipulated text.

export default function transposeAstralsToCountableChar( settings, text ) {
if ( settings.astralRegExp ) {
return text.replace( settings.astralRegExp, 'a' );
}
return text;
return text.replace( settings.astralRegExp, 'a' );
}
/**
* Replaces items matched in the regex with a single character.
*
* @param {Object} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
* @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
* @param {string} text The string being counted.
*

@@ -13,6 +13,3 @@ * @return {string} The manipulated text.

) {
if ( settings.HTMLEntityRegExp ) {
return text.replace( settings.HTMLEntityRegExp, 'a' );
}
return text;
return text.replace( settings.HTMLEntityRegExp, 'a' );
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc