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