Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80742 views
1
var baseToString = require('../internal/baseToString'),
2
isIterateeCall = require('../internal/isIterateeCall');
3
4
/** Used to match words to create compound words. */
5
var reWords = (function() {
6
var upper = '[A-Z\\xc0-\\xd6\\xd8-\\xde]',
7
lower = '[a-z\\xdf-\\xf6\\xf8-\\xff]+';
8
9
return RegExp(upper + '+(?=' + upper + lower + ')|' + upper + '?' + lower + '|' + upper + '+|[0-9]+', 'g');
10
}());
11
12
/**
13
* Splits `string` into an array of its words.
14
*
15
* @static
16
* @memberOf _
17
* @category String
18
* @param {string} [string=''] The string to inspect.
19
* @param {RegExp|string} [pattern] The pattern to match words.
20
* @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
21
* @returns {Array} Returns the words of `string`.
22
* @example
23
*
24
* _.words('fred, barney, & pebbles');
25
* // => ['fred', 'barney', 'pebbles']
26
*
27
* _.words('fred, barney, & pebbles', /[^, ]+/g);
28
* // => ['fred', 'barney', '&', 'pebbles']
29
*/
30
function words(string, pattern, guard) {
31
if (guard && isIterateeCall(string, pattern, guard)) {
32
pattern = null;
33
}
34
string = baseToString(string);
35
return string.match(pattern || reWords) || [];
36
}
37
38
module.exports = words;
39
40