wp/wp-includes/js/dist/wordcount.js
changeset 18 be944660c56a
parent 16 a86126ab1dd4
child 19 3d72ae0968f4
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
    80 /******/ 	// __webpack_public_path__
    80 /******/ 	// __webpack_public_path__
    81 /******/ 	__webpack_require__.p = "";
    81 /******/ 	__webpack_require__.p = "";
    82 /******/
    82 /******/
    83 /******/
    83 /******/
    84 /******/ 	// Load entry module and return exports
    84 /******/ 	// Load entry module and return exports
    85 /******/ 	return __webpack_require__(__webpack_require__.s = 450);
    85 /******/ 	return __webpack_require__(__webpack_require__.s = "pC98");
    86 /******/ })
    86 /******/ })
    87 /************************************************************************/
    87 /************************************************************************/
    88 /******/ ({
    88 /******/ ({
    89 
    89 
    90 /***/ 2:
    90 /***/ "YLtl":
    91 /***/ (function(module, exports) {
    91 /***/ (function(module, exports) {
    92 
    92 
    93 (function() { module.exports = this["lodash"]; }());
    93 (function() { module.exports = window["lodash"]; }());
    94 
    94 
    95 /***/ }),
    95 /***/ }),
    96 
    96 
    97 /***/ 450:
    97 /***/ "pC98":
    98 /***/ (function(module, __webpack_exports__, __webpack_require__) {
    98 /***/ (function(module, __webpack_exports__, __webpack_require__) {
    99 
    99 
   100 "use strict";
   100 "use strict";
   101 // ESM COMPAT FLAG
   101 // ESM COMPAT FLAG
   102 __webpack_require__.r(__webpack_exports__);
   102 __webpack_require__.r(__webpack_exports__);
   103 
   103 
   104 // EXPORTS
   104 // EXPORTS
   105 __webpack_require__.d(__webpack_exports__, "count", function() { return /* binding */ count; });
   105 __webpack_require__.d(__webpack_exports__, "count", function() { return /* binding */ count; });
   106 
   106 
   107 // EXTERNAL MODULE: external {"this":"lodash"}
   107 // EXTERNAL MODULE: external "lodash"
   108 var external_this_lodash_ = __webpack_require__(2);
   108 var external_lodash_ = __webpack_require__("YLtl");
   109 
   109 
   110 // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/defaultSettings.js
   110 // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/defaultSettings.js
   111 var defaultSettings = {
   111 /** @typedef {import('./index').WPWordCountStrategy} WPWordCountStrategy */
       
   112 
       
   113 /** @typedef {Partial<{type: WPWordCountStrategy, shortcodes: string[]}>} WPWordCountL10n */
       
   114 
       
   115 /**
       
   116  * @typedef WPWordCountSettingsFields
       
   117  * @property {RegExp}              HTMLRegExp                        Regular expression that matches HTML tags
       
   118  * @property {RegExp}              HTMLcommentRegExp                 Regular expression that matches HTML comments
       
   119  * @property {RegExp}              spaceRegExp                       Regular expression that matches spaces in HTML
       
   120  * @property {RegExp}              HTMLEntityRegExp                  Regular expression that matches HTML entities
       
   121  * @property {RegExp}              connectorRegExp                   Regular expression that matches word connectors, like em-dash
       
   122  * @property {RegExp}              removeRegExp                      Regular expression that matches various characters to be removed when counting
       
   123  * @property {RegExp}              astralRegExp                      Regular expression that matches astral UTF-16 code points
       
   124  * @property {RegExp}              wordsRegExp                       Regular expression that matches words
       
   125  * @property {RegExp}              characters_excluding_spacesRegExp Regular expression that matches characters excluding spaces
       
   126  * @property {RegExp}              characters_including_spacesRegExp Regular expression that matches characters including spaces
       
   127  * @property {RegExp}              shortcodesRegExp                  Regular expression that matches WordPress shortcodes
       
   128  * @property {string[]}            shortcodes                        List of all shortcodes
       
   129  * @property {WPWordCountStrategy} type                              Describes what and how are we counting
       
   130  * @property {WPWordCountL10n}     l10n                              Object with human translations
       
   131  */
       
   132 
       
   133 /**
       
   134  * Lower-level settings for word counting that can be overridden.
       
   135  *
       
   136  * @typedef {Partial<WPWordCountSettingsFields>} WPWordCountUserSettings
       
   137  */
       
   138 // Disable reason: JSDoc linter doesn't seem to parse the union (`&`) correctly: https://github.com/jsdoc/jsdoc/issues/1285
       
   139 
       
   140 /* eslint-disable jsdoc/valid-types */
       
   141 
       
   142 /**
       
   143  * Word counting settings that include non-optional values we set if missing
       
   144  *
       
   145  * @typedef {WPWordCountUserSettings & typeof defaultSettings} WPWordCountDefaultSettings
       
   146  */
       
   147 
       
   148 /* eslint-enable jsdoc/valid-types */
       
   149 const defaultSettings = {
   112   HTMLRegExp: /<\/?[a-z][^>]*?>/gi,
   150   HTMLRegExp: /<\/?[a-z][^>]*?>/gi,
   113   HTMLcommentRegExp: /<!--[\s\S]*?-->/g,
   151   HTMLcommentRegExp: /<!--[\s\S]*?-->/g,
   114   spaceRegExp: /&nbsp;|&#160;/gi,
   152   spaceRegExp: /&nbsp;|&#160;/gi,
   115   HTMLEntityRegExp: /&\S+?;/g,
   153   HTMLEntityRegExp: /&\S+?;/g,
   116   // \u2014 = em-dash
   154   // \u2014 = em-dash
   117   connectorRegExp: /--|\u2014/g,
   155   connectorRegExp: /--|\u2014/g,
   118   // Characters to be removed from input text.
   156   // Characters to be removed from input text.
   119   removeRegExp: new RegExp(['[', // Basic Latin (extract)
   157   removeRegExp: new RegExp(['[', // Basic Latin (extract)
   120   "!-@[-`{-~", // Latin-1 Supplement (extract)
   158   '\u0021-\u002F\u003A-\u0040\u005B-\u0060\u007B-\u007E', // Latin-1 Supplement (extract)
   121   "\x80-\xBF\xD7\xF7",
   159   '\u0080-\u00BF\u00D7\u00F7',
   122   /*
   160   /*
   123    * The following range consists of:
   161    * The following range consists of:
   124    * General Punctuation
   162    * General Punctuation
   125    * Superscripts and Subscripts
   163    * Superscripts and Subscripts
   126    * Currency Symbols
   164    * Currency Symbols
   144    * Supplemental Arrows-B
   182    * Supplemental Arrows-B
   145    * Miscellaneous Mathematical Symbols-B
   183    * Miscellaneous Mathematical Symbols-B
   146    * Supplemental Mathematical Operators
   184    * Supplemental Mathematical Operators
   147    * Miscellaneous Symbols and Arrows
   185    * Miscellaneous Symbols and Arrows
   148    */
   186    */
   149   "\u2000-\u2BFF", // Supplemental Punctuation
   187   '\u2000-\u2BFF', // Supplemental Punctuation
   150   "\u2E00-\u2E7F", ']'].join(''), 'g'),
   188   '\u2E00-\u2E7F', ']'].join(''), 'g'),
   151   // Remove UTF-16 surrogate points, see https://en.wikipedia.org/wiki/UTF-16#U.2BD800_to_U.2BDFFF
   189   // Remove UTF-16 surrogate points, see https://en.wikipedia.org/wiki/UTF-16#U.2BD800_to_U.2BDFFF
   152   astralRegExp: /[\uD800-\uDBFF][\uDC00-\uDFFF]/g,
   190   astralRegExp: /[\uD800-\uDBFF][\uDC00-\uDFFF]/g,
   153   wordsRegExp: /\S\s+/g,
   191   wordsRegExp: /\S\s+/g,
   154   characters_excluding_spacesRegExp: /\S/g,
   192   characters_excluding_spacesRegExp: /\S/g,
   155 
   193 
   172 
   210 
   173 // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripTags.js
   211 // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripTags.js
   174 /**
   212 /**
   175  * Replaces items matched in the regex with new line
   213  * Replaces items matched in the regex with new line
   176  *
   214  *
   177  * @param {Object} settings The main settings object containing regular expressions
   215  * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
   178  * @param {string} text     The string being counted.
   216  * @param {string}                                text     The string being counted.
   179  *
   217  *
   180  * @return {string} The manipulated text.
   218  * @return {string} The manipulated text.
   181  */
   219  */
   182 function stripTags(settings, text) {
   220 function stripTags(settings, text) {
   183   if (settings.HTMLRegExp) {
   221   return text.replace(settings.HTMLRegExp, '\n');
   184     return text.replace(settings.HTMLRegExp, '\n');
       
   185   }
       
   186 }
   222 }
   187 
   223 
   188 // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/transposeAstralsToCountableChar.js
   224 // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/transposeAstralsToCountableChar.js
   189 /**
   225 /**
   190  * Replaces items matched in the regex with character.
   226  * Replaces items matched in the regex with character.
   191  *
   227  *
   192  * @param {Object} settings The main settings object containing regular expressions
   228  * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
   193  * @param {string} text     The string being counted.
   229  * @param {string}                                text     The string being counted.
   194  *
   230  *
   195  * @return {string} The manipulated text.
   231  * @return {string} The manipulated text.
   196  */
   232  */
   197 function transposeAstralsToCountableChar(settings, text) {
   233 function transposeAstralsToCountableChar(settings, text) {
   198   if (settings.astralRegExp) {
   234   return text.replace(settings.astralRegExp, 'a');
   199     return text.replace(settings.astralRegExp, 'a');
       
   200   }
       
   201 
       
   202   return text;
       
   203 }
   235 }
   204 
   236 
   205 // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripHTMLEntities.js
   237 // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripHTMLEntities.js
   206 /**
   238 /**
   207  * Removes items matched in the regex.
   239  * Removes items matched in the regex.
   208  *
   240  *
   209  * @param {Object} settings The main settings object containing regular expressions
   241  * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
   210  * @param {string} text     The string being counted.
   242  * @param {string}                                text     The string being counted.
   211  *
   243  *
   212  * @return {string} The manipulated text.
   244  * @return {string} The manipulated text.
   213  */
   245  */
   214 function stripHTMLEntities(settings, text) {
   246 function stripHTMLEntities(settings, text) {
   215   if (settings.HTMLEntityRegExp) {
   247   return text.replace(settings.HTMLEntityRegExp, '');
   216     return text.replace(settings.HTMLEntityRegExp, '');
       
   217   }
       
   218 
       
   219   return text;
       
   220 }
   248 }
   221 
   249 
   222 // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripConnectors.js
   250 // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripConnectors.js
   223 /**
   251 /**
   224  * Replaces items matched in the regex with spaces.
   252  * Replaces items matched in the regex with spaces.
   225  *
   253  *
   226  * @param {Object} settings The main settings object containing regular expressions
   254  * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
   227  * @param {string} text     The string being counted.
   255  * @param {string}                                text     The string being counted.
   228  *
   256  *
   229  * @return {string} The manipulated text.
   257  * @return {string} The manipulated text.
   230  */
   258  */
   231 function stripConnectors(settings, text) {
   259 function stripConnectors(settings, text) {
   232   if (settings.connectorRegExp) {
   260   return text.replace(settings.connectorRegExp, ' ');
   233     return text.replace(settings.connectorRegExp, ' ');
       
   234   }
       
   235 
       
   236   return text;
       
   237 }
   261 }
   238 
   262 
   239 // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripRemovables.js
   263 // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripRemovables.js
   240 /**
   264 /**
   241  * Removes items matched in the regex.
   265  * Removes items matched in the regex.
   242  *
   266  *
   243  * @param {Object} settings The main settings object containing regular expressions
   267  * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
   244  * @param {string} text     The string being counted.
   268  * @param {string}                                text     The string being counted.
   245  *
   269  *
   246  * @return {string} The manipulated text.
   270  * @return {string} The manipulated text.
   247  */
   271  */
   248 function stripRemovables(settings, text) {
   272 function stripRemovables(settings, text) {
   249   if (settings.removeRegExp) {
   273   return text.replace(settings.removeRegExp, '');
   250     return text.replace(settings.removeRegExp, '');
       
   251   }
       
   252 
       
   253   return text;
       
   254 }
   274 }
   255 
   275 
   256 // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripHTMLComments.js
   276 // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripHTMLComments.js
   257 /**
   277 /**
   258  * Removes items matched in the regex.
   278  * Removes items matched in the regex.
   259  *
   279  *
   260  * @param {Object} settings The main settings object containing regular expressions
   280  * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
   261  * @param {string} text     The string being counted.
   281  * @param {string}                                text     The string being counted.
   262  *
   282  *
   263  * @return {string} The manipulated text.
   283  * @return {string} The manipulated text.
   264  */
   284  */
   265 function stripHTMLComments(settings, text) {
   285 function stripHTMLComments(settings, text) {
   266   if (settings.HTMLcommentRegExp) {
   286   return text.replace(settings.HTMLcommentRegExp, '');
   267     return text.replace(settings.HTMLcommentRegExp, '');
       
   268   }
       
   269 
       
   270   return text;
       
   271 }
   287 }
   272 
   288 
   273 // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripShortcodes.js
   289 // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripShortcodes.js
   274 /**
   290 /**
   275  * Replaces items matched in the regex with a new line.
   291  * Replaces items matched in the regex with a new line.
   276  *
   292  *
   277  * @param {Object} settings The main settings object containing regular expressions
   293  * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
   278  * @param {string} text     The string being counted.
   294  * @param {string}                                text     The string being counted.
   279  *
   295  *
   280  * @return {string} The manipulated text.
   296  * @return {string} The manipulated text.
   281  */
   297  */
   282 function stripShortcodes(settings, text) {
   298 function stripShortcodes(settings, text) {
   283   if (settings.shortcodesRegExp) {
   299   if (settings.shortcodesRegExp) {
   289 
   305 
   290 // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripSpaces.js
   306 // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/stripSpaces.js
   291 /**
   307 /**
   292  * Replaces items matched in the regex with spaces.
   308  * Replaces items matched in the regex with spaces.
   293  *
   309  *
   294  * @param {Object} settings The main settings object containing regular expressions
   310  * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
   295  * @param {string} text     The string being counted.
   311  * @param {string}                                text     The string being counted.
   296  *
   312  *
   297  * @return {string} The manipulated text.
   313  * @return {string} The manipulated text.
   298  */
   314  */
   299 function stripSpaces(settings, text) {
   315 function stripSpaces(settings, text) {
   300   if (settings.spaceRegExp) {
   316   return text.replace(settings.spaceRegExp, ' ');
   301     return text.replace(settings.spaceRegExp, ' ');
       
   302   }
       
   303 }
   317 }
   304 
   318 
   305 // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/transposeHTMLEntitiesToCountableChars.js
   319 // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/transposeHTMLEntitiesToCountableChars.js
   306 /**
   320 /**
   307  * Replaces items matched in the regex with a single character.
   321  * Replaces items matched in the regex with a single character.
   308  *
   322  *
   309  * @param {Object} settings The main settings object containing regular expressions
   323  * @param {import('./index').WPWordCountSettings} settings The main settings object containing regular expressions
   310  * @param {string} text     The string being counted.
   324  * @param {string}                                text     The string being counted.
   311  *
   325  *
   312  * @return {string} The manipulated text.
   326  * @return {string} The manipulated text.
   313  */
   327  */
   314 function transposeHTMLEntitiesToCountableChars(settings, text) {
   328 function transposeHTMLEntitiesToCountableChars(settings, text) {
   315   if (settings.HTMLEntityRegExp) {
   329   return text.replace(settings.HTMLEntityRegExp, 'a');
   316     return text.replace(settings.HTMLEntityRegExp, 'a');
       
   317   }
       
   318 
       
   319   return text;
       
   320 }
   330 }
   321 
   331 
   322 // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/index.js
   332 // CONCATENATED MODULE: ./node_modules/@wordpress/wordcount/build-module/index.js
   323 /**
   333 /**
   324  * External dependencies
   334  * External dependencies
   337 
   347 
   338 
   348 
   339 
   349 
   340 
   350 
   341 /**
   351 /**
       
   352  * @typedef {import('./defaultSettings').WPWordCountDefaultSettings}  WPWordCountSettings
       
   353  * @typedef {import('./defaultSettings').WPWordCountUserSettings}     WPWordCountUserSettings
       
   354  */
       
   355 
       
   356 /**
       
   357  * Possible ways of counting.
       
   358  *
       
   359  * @typedef {'words'|'characters_excluding_spaces'|'characters_including_spaces'} WPWordCountStrategy
       
   360  */
       
   361 
       
   362 /**
   342  * Private function to manage the settings.
   363  * Private function to manage the settings.
   343  *
   364  *
   344  * @param {string} type         The type of count to be done.
   365  * @param {WPWordCountStrategy}     type         The type of count to be done.
   345  * @param {Object} userSettings Custom settings for the count.
   366  * @param {WPWordCountUserSettings} userSettings Custom settings for the count.
   346  *
   367  *
   347  * @return {void|Object|*} The combined settings object to be used.
   368  * @return {WPWordCountSettings} The combined settings object to be used.
   348  */
   369  */
   349 
   370 
   350 function loadSettings(type, userSettings) {
   371 function loadSettings(type, userSettings) {
   351   var settings = Object(external_this_lodash_["extend"])(defaultSettings, userSettings);
   372   var _settings$l10n$shortc, _settings$l10n;
   352   settings.shortcodes = settings.l10n.shortcodes || {};
   373 
       
   374   const settings = Object(external_lodash_["extend"])({}, defaultSettings, userSettings);
       
   375   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 : [];
   353 
   376 
   354   if (settings.shortcodes && settings.shortcodes.length) {
   377   if (settings.shortcodes && settings.shortcodes.length) {
   355     settings.shortcodesRegExp = new RegExp('\\[\\/?(?:' + settings.shortcodes.join('|') + ')[^\\]]*?\\]', 'g');
   378     settings.shortcodesRegExp = new RegExp('\\[\\/?(?:' + settings.shortcodes.join('|') + ')[^\\]]*?\\]', 'g');
   356   }
   379   }
   357 
   380 
   358   settings.type = type || settings.l10n.type;
   381   settings.type = type;
   359 
   382 
   360   if (settings.type !== 'characters_excluding_spaces' && settings.type !== 'characters_including_spaces') {
   383   if (settings.type !== 'characters_excluding_spaces' && settings.type !== 'characters_including_spaces') {
   361     settings.type = 'words';
   384     settings.type = 'words';
   362   }
   385   }
   363 
   386 
   364   return settings;
   387   return settings;
   365 }
   388 }
   366 /**
   389 /**
   367  * Match the regex for the type 'words'
   390  * Count the words in text
   368  *
   391  *
   369  * @param {string} text     The text being processed
   392  * @param {string}              text     The text being processed
   370  * @param {string} regex    The regular expression pattern being matched
   393  * @param {RegExp}              regex    The regular expression pattern being matched
   371  * @param {Object} settings Settings object containing regular expressions for each strip function
   394  * @param {WPWordCountSettings} settings Settings object containing regular expressions for each strip function
   372  *
   395  *
   373  * @return {Array|{index: number, input: string}} The matched string.
   396  * @return {number} Count of words.
   374  */
   397  */
   375 
   398 
   376 
   399 
   377 function matchWords(text, regex, settings) {
   400 function countWords(text, regex, settings) {
   378   text = Object(external_this_lodash_["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);
   401   var _text$match$length, _text$match;
       
   402 
       
   403   text = Object(external_lodash_["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);
   379   text = text + '\n';
   404   text = text + '\n';
   380   return text.match(regex);
   405   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;
   381 }
   406 }
   382 /**
   407 /**
   383  * Match the regex for either 'characters_excluding_spaces' or 'characters_including_spaces'
   408  * Count the characters in text
   384  *
   409  *
   385  * @param {string} text     The text being processed
   410  * @param {string}              text     The text being processed
   386  * @param {string} regex    The regular expression pattern being matched
   411  * @param {RegExp}              regex    The regular expression pattern being matched
   387  * @param {Object} settings Settings object containing regular expressions for each strip function
   412  * @param {WPWordCountSettings} settings Settings object containing regular expressions for each strip function
   388  *
   413  *
   389  * @return {Array|{index: number, input: string}} The matched string.
   414  * @return {number} Count of characters.
   390  */
   415  */
   391 
   416 
   392 
   417 
   393 function matchCharacters(text, regex, settings) {
   418 function countCharacters(text, regex, settings) {
   394   text = Object(external_this_lodash_["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);
   419   var _text$match$length2, _text$match2;
       
   420 
       
   421   text = Object(external_lodash_["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);
   395   text = text + '\n';
   422   text = text + '\n';
   396   return text.match(regex);
   423   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;
   397 }
   424 }
   398 /**
   425 /**
   399  * Count some words.
   426  * Count some words.
   400  *
   427  *
   401  * @param {string} text         The text being processed
   428  * @param {string}                  text         The text being processed
   402  * @param {string} type         The type of count. Accepts ;words', 'characters_excluding_spaces', or 'characters_including_spaces'.
   429  * @param {WPWordCountStrategy}     type         The type of count. Accepts 'words', 'characters_excluding_spaces', or 'characters_including_spaces'.
   403  * @param {Object} userSettings Custom settings object.
   430  * @param {WPWordCountUserSettings} userSettings Custom settings object.
   404  *
   431  *
   405  * @example
   432  * @example
   406  * ```js
   433  * ```js
   407  * import { count } from '@wordpress/wordcount';
   434  * import { count } from '@wordpress/wordcount';
   408  * const numberOfWords = count( 'Words to count', 'words', {} )
   435  * const numberOfWords = count( 'Words to count', 'words', {} )
   411  * @return {number} The word or character count.
   438  * @return {number} The word or character count.
   412  */
   439  */
   413 
   440 
   414 
   441 
   415 function count(text, type, userSettings) {
   442 function count(text, type, userSettings) {
   416   if ('' === text) {
   443   const settings = loadSettings(type, userSettings);
   417     return 0;
   444   let matchRegExp;
       
   445 
       
   446   switch (settings.type) {
       
   447     case 'words':
       
   448       matchRegExp = settings.wordsRegExp;
       
   449       return countWords(text, matchRegExp, settings);
       
   450 
       
   451     case 'characters_including_spaces':
       
   452       matchRegExp = settings.characters_including_spacesRegExp;
       
   453       return countCharacters(text, matchRegExp, settings);
       
   454 
       
   455     case 'characters_excluding_spaces':
       
   456       matchRegExp = settings.characters_excluding_spacesRegExp;
       
   457       return countCharacters(text, matchRegExp, settings);
       
   458 
       
   459     default:
       
   460       return 0;
   418   }
   461   }
   419 
       
   420   if (text) {
       
   421     var settings = loadSettings(type, userSettings);
       
   422     var matchRegExp = settings[type + 'RegExp'];
       
   423     var results = 'words' === settings.type ? matchWords(text, matchRegExp, settings) : matchCharacters(text, matchRegExp, settings);
       
   424     return results ? results.length : 0;
       
   425   }
       
   426 }
   462 }
   427 
   463 
   428 
   464 
   429 /***/ })
   465 /***/ })
   430 
   466