voca
Version:
The ultimate JavaScript string library
40 lines (36 loc) • 1.13 kB
JavaScript
import './internal/is_nil.js';
import './is_string.js';
import './internal/coerce_to_string.js';
import './internal/const.js';
import './internal/const_extended.js';
import './internal/nil_default.js';
import './internal/to_string.js';
import words from './words.js';
/**
* Counts the number of words in `subject`.
*
* @function countWords
* @static
* @since 1.0.0
* @memberOf Count
* @param {string} [subject=''] The string to split into words.
* @param {string|RegExp} [pattern] The pattern to watch words. If `pattern` is not RegExp, it is transformed to `new RegExp(pattern, flags)`.
* @param {string} [flags=''] The regular expression flags. Applies when `pattern` is string type.
* @return {number} Returns the number of words.
* @example
* v.countWords('gravity can cross dimensions');
* // => 4
*
* v.countWords('GravityCanCrossDimensions');
* // => 4
*
* v.countWords('Gravity - can cross dimensions!');
* // => 4
*
* v.words('Earth gravity', /[^\s]+/g);
* // => 2
*/
function countWords(subject, pattern, flags) {
return words(subject, pattern, flags).length;
}
export default countWords;