mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-07 10:07:48 +00:00
Simplify words.
This commit is contained in:
13
words.js
13
words.js
@@ -1,7 +1,13 @@
|
||||
import asciiWords from './.internal/asciiWords.js'
|
||||
import hasUnicodeWord from './.internal/hasUnicodeWord.js'
|
||||
import unicodeWords from './.internal/unicodeWords.js'
|
||||
|
||||
const asciiWords = RegExp.prototype.exec.bind(
|
||||
/[^\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f]+/g
|
||||
)
|
||||
|
||||
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 ]/
|
||||
)
|
||||
|
||||
/**
|
||||
* Splits `string` into an array of its words.
|
||||
*
|
||||
@@ -20,7 +26,8 @@ import unicodeWords from './.internal/unicodeWords.js'
|
||||
*/
|
||||
function words(string, pattern) {
|
||||
if (pattern === undefined) {
|
||||
return hasUnicodeWord(string) ? unicodeWords(string) : asciiWords(string)
|
||||
const result = hasUnicodeWord(string) ? unicodeWords(string) : asciiWords(string)
|
||||
return result || []
|
||||
}
|
||||
return string.match(pattern) || []
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user