forked from lodash/lodash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwords.js
38 lines (33 loc) · 1.04 KB
/
words.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import unicodeWords from './.internal/unicodeWords.js'
const hasUnicodeWord = RegExp.prototype.test.bind(
/[a-z][A-Z]|[A-Z]{2}[a-z]|[0-9][a-zA-Z]|[a-zA-Z][0-9]|[^a-zA-Z0-9 ]/
)
/** Used to match words composed of alphanumeric characters. */
const reAsciiWord = /[^\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f]+/g
function asciiWords(string) {
return string.match(reAsciiWord)
}
/**
* Splits `string` into an array of its words.
*
* @since 3.0.0
* @category String
* @param {string} [string=''] The string to inspect.
* @param {RegExp|string} [pattern] The pattern to match words.
* @returns {Array} Returns the words of `string`.
* @example
*
* words('fred, barney, & pebbles')
* // => ['fred', 'barney', 'pebbles']
*
* words('fred, barney, & pebbles', /[^, ]+/g)
* // => ['fred', 'barney', '&', 'pebbles']
*/
function words(string, pattern) {
if (pattern === undefined) {
const result = hasUnicodeWord(string) ? unicodeWords(string) : asciiWords(string)
return result || []
}
return string.match(pattern) || []
}
export default words