Simplify words.

This commit is contained in:
John-David Dalton
2017-03-27 14:08:22 -07:00
parent 8be26ac755
commit 6d19563a9f
4 changed files with 19 additions and 47 deletions

View File

@@ -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) || []
}