index.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*!
  2. * is-glob <https://github.com/jonschlinkert/is-glob>
  3. *
  4. * Copyright (c) 2014-2017, Jon Schlinkert.
  5. * Released under the MIT License.
  6. */
  7. var isExtglob = require('is-extglob');
  8. var chars = { '{': '}', '(': ')', '[': ']'};
  9. module.exports = function isGlob(str, options) {
  10. if (typeof str !== 'string' || str === '') {
  11. return false;
  12. }
  13. if (isExtglob(str)) {
  14. return true;
  15. }
  16. var regex = /\\(.)|(^!|\*|[\].+)]\?|\[[^\\\]]+\]|\{[^\\}]+\}|\(\?[:!=][^\\)]+\)|\([^|]+\|[^\\)]+\))/;
  17. var match;
  18. // optionally relax regex
  19. if (options && options.strict === false) {
  20. regex = /\\(.)|(^!|[*?{}()[\]]|\(\?)/;
  21. }
  22. while ((match = regex.exec(str))) {
  23. if (match[2]) return true;
  24. var idx = match.index + match[0].length;
  25. // if an open bracket/brace/paren is escaped,
  26. // set the index to the next closing character
  27. var open = match[1];
  28. var close = open ? chars[open] : null;
  29. if (open && close) {
  30. var n = str.indexOf(close, idx);
  31. if (n !== -1) {
  32. idx = n + 1;
  33. }
  34. }
  35. str = str.slice(idx);
  36. }
  37. return false;
  38. };