Files
lodash/words.js
2017-03-14 23:29:32 -07:00

29 lines
821 B
JavaScript

import asciiWords from './.internal/asciiWords.js'
import hasUnicodeWord from './.internal/hasUnicodeWord.js'
import unicodeWords from './.internal/unicodeWords.js'
/**
* 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) {
return hasUnicodeWord(string) ? unicodeWords(string) : asciiWords(string)
}
return string.match(pattern) || []
}
export default words