| 1 |
'use strict'; |
| 2 |
|
| 3 |
const scan = require('./scan'); |
| 4 |
const parse = require('./parse'); |
| 5 |
const utils = require('./utils'); |
| 6 |
const constants = require('./constants'); |
| 7 |
const isObject = val => val && typeof val === 'object' && !Array.isArray(val); |
| 8 |
|
| 9 |
/** |
| 10 |
* Creates a matcher function from one or more glob patterns. The |
| 11 |
* returned function takes a string to match as its first argument, |
| 12 |
* and returns true if the string is a match. The returned matcher |
| 13 |
* function also takes a boolean as the second argument that, when true, |
| 14 |
* returns an object with additional information. |
| 15 |
* |
| 16 |
* ```js |
| 17 |
* const picomatch = require('picomatch'); |
| 18 |
* // picomatch(glob[, options]); |
| 19 |
* |
| 20 |
* const isMatch = picomatch('*.!(*a)'); |
| 21 |
* console.log(isMatch('a.a')); //=> false |
| 22 |
* console.log(isMatch('a.b')); //=> true |
| 23 |
* |
| 24 |
* // For environments without `node.js`, `picomatch/posix` provides you a dependency-free matcher, without automatic OS detection. |
| 25 |
* const picomatch = require('picomatch/posix'); |
| 26 |
* // the same API, defaulting to posix paths |
| 27 |
* const isMatch = picomatch('a/*'); |
| 28 |
* console.log(isMatch('a\\b')); //=> false |
| 29 |
* console.log(isMatch('a/b')); //=> true |
| 30 |
* |
| 31 |
* // you can still configure the matcher function to accept windows paths |
| 32 |
* const isMatch = picomatch('a/*', { options: windows }); |
| 33 |
* console.log(isMatch('a\\b')); //=> true |
| 34 |
* console.log(isMatch('a/b')); //=> true |
| 35 |
* ``` |
| 36 |
* @name picomatch |
| 37 |
* @param {String|Array} `globs` One or more glob patterns. |
| 38 |
* @param {Object=} `options` |
| 39 |
* @return {Function=} Returns a matcher function. |
| 40 |
* @api public |
| 41 |
*/ |
| 42 |
|
| 43 |
const picomatch = (glob, options, returnState = false) => { |
| 44 |
if (Array.isArray(glob)) { |
| 45 |
const fns = glob.map(input => picomatch(input, options, returnState)); |
| 46 |
const arrayMatcher = str => { |
| 47 |
for (const isMatch of fns) { |
| 48 |
const state = isMatch(str); |
| 49 |
if (state) return state; |
| 50 |
} |
| 51 |
return false; |
| 52 |
}; |
| 53 |
return arrayMatcher; |
| 54 |
} |
| 55 |
|
| 56 |
const isState = isObject(glob) && glob.tokens && glob.input; |
| 57 |
|
| 58 |
if (glob === '' || (typeof glob !== 'string' && !isState)) { |
| 59 |
throw new TypeError('Expected pattern to be a non-empty string'); |
| 60 |
} |
| 61 |
|
| 62 |
const opts = options || {}; |
| 63 |
const posix = opts.windows; |
| 64 |
const regex = isState |
| 65 |
? picomatch.compileRe(glob, options) |
| 66 |
: picomatch.makeRe(glob, options, false, true); |
| 67 |
|
| 68 |
const state = regex.state; |
| 69 |
delete regex.state; |
| 70 |
|
| 71 |
let isIgnored = () => false; |
| 72 |
if (opts.ignore) { |
| 73 |
const ignoreOpts = { ...options, ignore: null, onMatch: null, onResult: null }; |
| 74 |
isIgnored = picomatch(opts.ignore, ignoreOpts, returnState); |
| 75 |
} |
| 76 |
|
| 77 |
const matcher = (input, returnObject = false) => { |
| 78 |
const { isMatch, match, output } = picomatch.test(input, regex, options, { glob, posix }); |
| 79 |
const result = { glob, state, regex, posix, input, output, match, isMatch }; |
| 80 |
|
| 81 |
if (typeof opts.onResult === 'function') { |
| 82 |
opts.onResult(result); |
| 83 |
} |
| 84 |
|
| 85 |
if (isMatch === false) { |
| 86 |
result.isMatch = false; |
| 87 |
return returnObject ? result : false; |
| 88 |
} |
| 89 |
|
| 90 |
if (isIgnored(input)) { |
| 91 |
if (typeof opts.onIgnore === 'function') { |
| 92 |
opts.onIgnore(result); |
| 93 |
} |
| 94 |
result.isMatch = false; |
| 95 |
return returnObject ? result : false; |
| 96 |
} |
| 97 |
|
| 98 |
if (typeof opts.onMatch === 'function') { |
| 99 |
opts.onMatch(result); |
| 100 |
} |
| 101 |
return returnObject ? result : true; |
| 102 |
}; |
| 103 |
|
| 104 |
if (returnState) { |
| 105 |
matcher.state = state; |
| 106 |
} |
| 107 |
|
| 108 |
return matcher; |
| 109 |
}; |
| 110 |
|
| 111 |
/** |
| 112 |
* Test `input` with the given `regex`. This is used by the main |
| 113 |
* `picomatch()` function to test the input string. |
| 114 |
* |
| 115 |
* ```js |
| 116 |
* const picomatch = require('picomatch'); |
| 117 |
* // picomatch.test(input, regex[, options]); |
| 118 |
* |
| 119 |
* console.log(picomatch.test('foo/bar', /^(?:([^/]*?)\/([^/]*?))$/)); |
| 120 |
* // { isMatch: true, match: [ 'foo/', 'foo', 'bar' ], output: 'foo/bar' } |
| 121 |
* ``` |
| 122 |
* @param {String} `input` String to test. |
| 123 |
* @param {RegExp} `regex` |
| 124 |
* @return {Object} Returns an object with matching info. |
| 125 |
* @api public |
| 126 |
*/ |
| 127 |
|
| 128 |
picomatch.test = (input, regex, options, { glob, posix } = {}) => { |
| 129 |
if (typeof input !== 'string') { |
| 130 |
throw new TypeError('Expected input to be a string'); |
| 131 |
} |
| 132 |
|
| 133 |
if (input === '') { |
| 134 |
return { isMatch: false, output: '' }; |
| 135 |
} |
| 136 |
|
| 137 |
const opts = options || {}; |
| 138 |
const format = opts.format || (posix ? utils.toPosixSlashes : null); |
| 139 |
let match = input === glob; |
| 140 |
let output = (match && format) ? format(input) : input; |
| 141 |
|
| 142 |
if (match === false) { |
| 143 |
output = format ? format(input) : input; |
| 144 |
match = output === glob; |
| 145 |
} |
| 146 |
|
| 147 |
if (match === false || opts.capture === true) { |
| 148 |
if (opts.matchBase === true || opts.basename === true) { |
| 149 |
match = picomatch.matchBase(input, regex, options, posix); |
| 150 |
} else { |
| 151 |
match = regex.exec(output); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
return { isMatch: Boolean(match), match, output }; |
| 156 |
}; |
| 157 |
|
| 158 |
/** |
| 159 |
* Match the basename of a filepath. |
| 160 |
* |
| 161 |
* ```js |
| 162 |
* const picomatch = require('picomatch'); |
| 163 |
* // picomatch.matchBase(input, glob[, options]); |
| 164 |
* console.log(picomatch.matchBase('foo/bar.js', '*.js'); // true |
| 165 |
* ``` |
| 166 |
* @param {String} `input` String to test. |
| 167 |
* @param {RegExp|String} `glob` Glob pattern or regex created by [.makeRe](#makeRe). |
| 168 |
* @return {Boolean} |
| 169 |
* @api public |
| 170 |
*/ |
| 171 |
|
| 172 |
picomatch.matchBase = (input, glob, options, posix = options && options.windows) => { |
| 173 |
const regex = glob instanceof RegExp ? glob : picomatch.makeRe(glob, options); |
| 174 |
return regex.test(utils.basename(input, { windows: posix })); |
| 175 |
}; |
| 176 |
|
| 177 |
/** |
| 178 |
* Returns true if **any** of the given glob `patterns` match the specified `string`. |
| 179 |
* |
| 180 |
* ```js |
| 181 |
* const picomatch = require('picomatch'); |
| 182 |
* // picomatch.isMatch(string, patterns[, options]); |
| 183 |
* |
| 184 |
* console.log(picomatch.isMatch('a.a', ['b.*', '*.a'])); //=> true |
| 185 |
* console.log(picomatch.isMatch('a.a', 'b.*')); //=> false |
| 186 |
* ``` |
| 187 |
* @param {String|Array} str The string to test. |
| 188 |
* @param {String|Array} patterns One or more glob patterns to use for matching. |
| 189 |
* @param {Object} [options] See available [options](#options). |
| 190 |
* @return {Boolean} Returns true if any patterns match `str` |
| 191 |
* @api public |
| 192 |
*/ |
| 193 |
|
| 194 |
picomatch.isMatch = (str, patterns, options) => picomatch(patterns, options)(str); |
| 195 |
|
| 196 |
/** |
| 197 |
* Parse a glob pattern to create the source string for a regular |
| 198 |
* expression. |
| 199 |
* |
| 200 |
* ```js |
| 201 |
* const picomatch = require('picomatch'); |
| 202 |
* const result = picomatch.parse(pattern[, options]); |
| 203 |
* ``` |
| 204 |
* @param {String} `pattern` |
| 205 |
* @param {Object} `options` |
| 206 |
* @return {Object} Returns an object with useful properties and output to be used as a regex source string. |
| 207 |
* @api public |
| 208 |
*/ |
| 209 |
|
| 210 |
picomatch.parse = (pattern, options) => { |
| 211 |
if (Array.isArray(pattern)) return pattern.map(p => picomatch.parse(p, options)); |
| 212 |
return parse(pattern, { ...options, fastpaths: false }); |
| 213 |
}; |
| 214 |
|
| 215 |
/** |
| 216 |
* Scan a glob pattern to separate the pattern into segments. |
| 217 |
* |
| 218 |
* ```js |
| 219 |
* const picomatch = require('picomatch'); |
| 220 |
* // picomatch.scan(input[, options]); |
| 221 |
* |
| 222 |
* const result = picomatch.scan('!./foo/*.js'); |
| 223 |
* console.log(result); |
| 224 |
* { prefix: '!./', |
| 225 |
* input: '!./foo/*.js', |
| 226 |
* start: 3, |
| 227 |
* base: 'foo', |
| 228 |
* glob: '*.js', |
| 229 |
* isBrace: false, |
| 230 |
* isBracket: false, |
| 231 |
* isGlob: true, |
| 232 |
* isExtglob: false, |
| 233 |
* isGlobstar: false, |
| 234 |
* negated: true } |
| 235 |
* ``` |
| 236 |
* @param {String} `input` Glob pattern to scan. |
| 237 |
* @param {Object} `options` |
| 238 |
* @return {Object} Returns an object with |
| 239 |
* @api public |
| 240 |
*/ |
| 241 |
|
| 242 |
picomatch.scan = (input, options) => scan(input, options); |
| 243 |
|
| 244 |
/** |
| 245 |
* Compile a regular expression from the `state` object returned by the |
| 246 |
* [parse()](#parse) method. |
| 247 |
* |
| 248 |
* ```js |
| 249 |
* const picomatch = require('picomatch'); |
| 250 |
* const state = picomatch.parse('*.js'); |
| 251 |
* // picomatch.compileRe(state[, options]); |
| 252 |
* |
| 253 |
* console.log(picomatch.compileRe(state)); |
| 254 |
* //=> /^(?:(?!\.)(?=.)[^/]*?\.js)$/ |
| 255 |
* ``` |
| 256 |
* @param {Object} `state` |
| 257 |
* @param {Object} `options` |
| 258 |
* @param {Boolean} `returnOutput` Intended for implementors, this argument allows you to return the raw output from the parser. |
| 259 |
* @param {Boolean} `returnState` Adds the state to a `state` property on the returned regex. Useful for implementors and debugging. |
| 260 |
* @return {RegExp} |
| 261 |
* @api public |
| 262 |
*/ |
| 263 |
|
| 264 |
picomatch.compileRe = (state, options, returnOutput = false, returnState = false) => { |
| 265 |
if (returnOutput === true) { |
| 266 |
return state.output; |
| 267 |
} |
| 268 |
|
| 269 |
const opts = options || {}; |
| 270 |
const prepend = opts.contains ? '' : '^'; |
| 271 |
const append = opts.contains ? '' : '$'; |
| 272 |
|
| 273 |
let source = `${prepend}(?:${state.output})${append}`; |
| 274 |
if (state && state.negated === true) { |
| 275 |
source = `^(?!${source}).*$`; |
| 276 |
} |
| 277 |
|
| 278 |
const regex = picomatch.toRegex(source, options); |
| 279 |
if (returnState === true) { |
| 280 |
regex.state = state; |
| 281 |
} |
| 282 |
|
| 283 |
return regex; |
| 284 |
}; |
| 285 |
|
| 286 |
/** |
| 287 |
* Create a regular expression from a parsed glob pattern. |
| 288 |
* |
| 289 |
* ```js |
| 290 |
* const picomatch = require('picomatch'); |
| 291 |
* // picomatch.makeRe(state[, options]); |
| 292 |
* |
| 293 |
* const result = picomatch.makeRe('*.js'); |
| 294 |
* console.log(result); |
| 295 |
* //=> /^(?:(?!\.)(?=.)[^/]*?\.js)$/ |
| 296 |
* ``` |
| 297 |
* @param {String} `state` The object returned from the `.parse` method. |
| 298 |
* @param {Object} `options` |
| 299 |
* @param {Boolean} `returnOutput` Implementors may use this argument to return the compiled output, instead of a regular expression. This is not exposed on the options to prevent end-users from mutating the result. |
| 300 |
* @param {Boolean} `returnState` Implementors may use this argument to return the state from the parsed glob with the returned regular expression. |
| 301 |
* @return {RegExp} Returns a regex created from the given pattern. |
| 302 |
* @api public |
| 303 |
*/ |
| 304 |
|
| 305 |
picomatch.makeRe = (input, options = {}, returnOutput = false, returnState = false) => { |
| 306 |
if (!input || typeof input !== 'string') { |
| 307 |
throw new TypeError('Expected a non-empty string'); |
| 308 |
} |
| 309 |
|
| 310 |
let parsed = { negated: false, fastpaths: true }; |
| 311 |
|
| 312 |
if (options.fastpaths !== false && (input[0] === '.' || input[0] === '*')) { |
| 313 |
parsed.output = parse.fastpaths(input, options); |
| 314 |
} |
| 315 |
|
| 316 |
if (!parsed.output) { |
| 317 |
parsed = parse(input, options); |
| 318 |
} |
| 319 |
|
| 320 |
return picomatch.compileRe(parsed, options, returnOutput, returnState); |
| 321 |
}; |
| 322 |
|
| 323 |
/** |
| 324 |
* Create a regular expression from the given regex source string. |
| 325 |
* |
| 326 |
* ```js |
| 327 |
* const picomatch = require('picomatch'); |
| 328 |
* // picomatch.toRegex(source[, options]); |
| 329 |
* |
| 330 |
* const { output } = picomatch.parse('*.js'); |
| 331 |
* console.log(picomatch.toRegex(output)); |
| 332 |
* //=> /^(?:(?!\.)(?=.)[^/]*?\.js)$/ |
| 333 |
* ``` |
| 334 |
* @param {String} `source` Regular expression source string. |
| 335 |
* @param {Object} `options` |
| 336 |
* @return {RegExp} |
| 337 |
* @api public |
| 338 |
*/ |
| 339 |
|
| 340 |
picomatch.toRegex = (source, options) => { |
| 341 |
try { |
| 342 |
const opts = options || {}; |
| 343 |
return new RegExp(source, opts.flags || (opts.nocase ? 'i' : '')); |
| 344 |
} catch (err) { |
| 345 |
if (options && options.debug === true) throw err; |
| 346 |
return /$^/; |
| 347 |
} |
| 348 |
}; |
| 349 |
|
| 350 |
/** |
| 351 |
* Picomatch constants. |
| 352 |
* @return {Object} |
| 353 |
*/ |
| 354 |
|
| 355 |
picomatch.constants = constants; |
| 356 |
|
| 357 |
/** |
| 358 |
* Expose "picomatch" |
| 359 |
*/ |
| 360 |
|
| 361 |
module.exports = picomatch; |