You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. var strictCheck = function(str) {
  10. if (str[0] === '!') {
  11. return true;
  12. }
  13. var index = 0;
  14. var pipeIndex = -2;
  15. var closeSquareIndex = -2;
  16. var closeCurlyIndex = -2;
  17. var closeParenIndex = -2;
  18. var backSlashIndex = -2;
  19. while (index < str.length) {
  20. if (str[index] === '*') {
  21. return true;
  22. }
  23. if (str[index + 1] === '?' && /[\].+)]/.test(str[index])) {
  24. return true;
  25. }
  26. if (closeSquareIndex !== -1 && str[index] === '[' && str[index + 1] !== ']') {
  27. if (closeSquareIndex < index) {
  28. closeSquareIndex = str.indexOf(']', index);
  29. }
  30. if (closeSquareIndex > index) {
  31. if (backSlashIndex === -1 || backSlashIndex > closeSquareIndex) {
  32. return true;
  33. }
  34. backSlashIndex = str.indexOf('\\', index);
  35. if (backSlashIndex === -1 || backSlashIndex > closeSquareIndex) {
  36. return true;
  37. }
  38. }
  39. }
  40. if (closeCurlyIndex !== -1 && str[index] === '{' && str[index + 1] !== '}') {
  41. closeCurlyIndex = str.indexOf('}', index);
  42. if (closeCurlyIndex > index) {
  43. backSlashIndex = str.indexOf('\\', index);
  44. if (backSlashIndex === -1 || backSlashIndex > closeCurlyIndex) {
  45. return true;
  46. }
  47. }
  48. }
  49. if (closeParenIndex !== -1 && str[index] === '(' && str[index + 1] === '?' && /[:!=]/.test(str[index + 2]) && str[index + 3] !== ')') {
  50. closeParenIndex = str.indexOf(')', index);
  51. if (closeParenIndex > index) {
  52. backSlashIndex = str.indexOf('\\', index);
  53. if (backSlashIndex === -1 || backSlashIndex > closeParenIndex) {
  54. return true;
  55. }
  56. }
  57. }
  58. if (pipeIndex !== -1 && str[index] === '(' && str[index + 1] !== '|') {
  59. if (pipeIndex < index) {
  60. pipeIndex = str.indexOf('|', index);
  61. }
  62. if (pipeIndex !== -1 && str[pipeIndex + 1] !== ')') {
  63. closeParenIndex = str.indexOf(')', pipeIndex);
  64. if (closeParenIndex > pipeIndex) {
  65. backSlashIndex = str.indexOf('\\', pipeIndex);
  66. if (backSlashIndex === -1 || backSlashIndex > closeParenIndex) {
  67. return true;
  68. }
  69. }
  70. }
  71. }
  72. if (str[index] === '\\') {
  73. var open = str[index + 1];
  74. index += 2;
  75. var close = chars[open];
  76. if (close) {
  77. var n = str.indexOf(close, index);
  78. if (n !== -1) {
  79. index = n + 1;
  80. }
  81. }
  82. if (str[index] === '!') {
  83. return true;
  84. }
  85. } else {
  86. index++;
  87. }
  88. }
  89. return false;
  90. };
  91. var relaxedCheck = function(str) {
  92. if (str[0] === '!') {
  93. return true;
  94. }
  95. var index = 0;
  96. while (index < str.length) {
  97. if (/[*?{}()[\]]/.test(str[index])) {
  98. return true;
  99. }
  100. if (str[index] === '\\') {
  101. var open = str[index + 1];
  102. index += 2;
  103. var close = chars[open];
  104. if (close) {
  105. var n = str.indexOf(close, index);
  106. if (n !== -1) {
  107. index = n + 1;
  108. }
  109. }
  110. if (str[index] === '!') {
  111. return true;
  112. }
  113. } else {
  114. index++;
  115. }
  116. }
  117. return false;
  118. };
  119. module.exports = function isGlob(str, options) {
  120. if (typeof str !== 'string' || str === '') {
  121. return false;
  122. }
  123. if (isExtglob(str)) {
  124. return true;
  125. }
  126. var check = strictCheck;
  127. // optionally relax check
  128. if (options && options.strict === false) {
  129. check = relaxedCheck;
  130. }
  131. return check(str);
  132. };