words.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. var asciiWords = require('./_asciiWords'),
  2. hasUnicodeWord = require('./_hasUnicodeWord'),
  3. toString = require('./toString'),
  4. unicodeWords = require('./_unicodeWords');
  5. /**
  6. * Splits `string` into an array of its words.
  7. *
  8. * @static
  9. * @memberOf _
  10. * @since 3.0.0
  11. * @category String
  12. * @param {string} [string=''] The string to inspect.
  13. * @param {RegExp|string} [pattern] The pattern to match words.
  14. * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.
  15. * @returns {Array} Returns the words of `string`.
  16. * @example
  17. *
  18. * _.words('fred, barney, & pebbles');
  19. * // => ['fred', 'barney', 'pebbles']
  20. *
  21. * _.words('fred, barney, & pebbles', /[^, ]+/g);
  22. * // => ['fred', 'barney', '&', 'pebbles']
  23. */
  24. function words(string, pattern, guard) {
  25. string = toString(string);
  26. pattern = guard ? undefined : pattern;
  27. if (pattern === undefined) {
  28. return hasUnicodeWord(string) ? unicodeWords(string) : asciiWords(string);
  29. }
  30. return string.match(pattern) || [];
  31. }
  32. module.exports = words;