123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- 'use strict';
- var extend = require('extend-shallow');
- var unique = require('array-unique');
- var toRegex = require('to-regex');
- var compilers = require('./lib/compilers');
- var parsers = require('./lib/parsers');
- var Extglob = require('./lib/extglob');
- var utils = require('./lib/utils');
- var MAX_LENGTH = 1024 * 64;
- function extglob(pattern, options) {
- return extglob.create(pattern, options).output;
- }
- extglob.match = function(list, pattern, options) {
- if (typeof pattern !== 'string') {
- throw new TypeError('expected pattern to be a string');
- }
- list = utils.arrayify(list);
- var isMatch = extglob.matcher(pattern, options);
- var len = list.length;
- var idx = -1;
- var matches = [];
- while (++idx < len) {
- var ele = list[idx];
- if (isMatch(ele)) {
- matches.push(ele);
- }
- }
-
- if (typeof options === 'undefined') {
- return unique(matches);
- }
- if (matches.length === 0) {
- if (options.failglob === true) {
- throw new Error('no matches found for "' + pattern + '"');
- }
- if (options.nonull === true || options.nullglob === true) {
- return [pattern.split('\\').join('')];
- }
- }
- return options.nodupes !== false ? unique(matches) : matches;
- };
- extglob.isMatch = function(str, pattern, options) {
- if (typeof pattern !== 'string') {
- throw new TypeError('expected pattern to be a string');
- }
- if (typeof str !== 'string') {
- throw new TypeError('expected a string');
- }
- if (pattern === str) {
- return true;
- }
- if (pattern === '' || pattern === ' ' || pattern === '.') {
- return pattern === str;
- }
- var isMatch = utils.memoize('isMatch', pattern, options, extglob.matcher);
- return isMatch(str);
- };
- extglob.contains = function(str, pattern, options) {
- if (typeof str !== 'string') {
- throw new TypeError('expected a string');
- }
- if (pattern === '' || pattern === ' ' || pattern === '.') {
- return pattern === str;
- }
- var opts = extend({}, options, {contains: true});
- opts.strictClose = false;
- opts.strictOpen = false;
- return extglob.isMatch(str, pattern, opts);
- };
- extglob.matcher = function(pattern, options) {
- if (typeof pattern !== 'string') {
- throw new TypeError('expected pattern to be a string');
- }
- function matcher() {
- var re = extglob.makeRe(pattern, options);
- return function(str) {
- return re.test(str);
- };
- }
- return utils.memoize('matcher', pattern, options, matcher);
- };
- extglob.create = function(pattern, options) {
- if (typeof pattern !== 'string') {
- throw new TypeError('expected pattern to be a string');
- }
- function create() {
- var ext = new Extglob(options);
- var ast = ext.parse(pattern, options);
- return ext.compile(ast, options);
- }
- return utils.memoize('create', pattern, options, create);
- };
- extglob.capture = function(pattern, str, options) {
- var re = extglob.makeRe(pattern, extend({capture: true}, options));
- function match() {
- return function(string) {
- var match = re.exec(string);
- if (!match) {
- return null;
- }
- return match.slice(1);
- };
- }
- var capture = utils.memoize('capture', pattern, options, match);
- return capture(str);
- };
- extglob.makeRe = function(pattern, options) {
- if (pattern instanceof RegExp) {
- return pattern;
- }
- if (typeof pattern !== 'string') {
- throw new TypeError('expected pattern to be a string');
- }
- if (pattern.length > MAX_LENGTH) {
- throw new Error('expected pattern to be less than ' + MAX_LENGTH + ' characters');
- }
- function makeRe() {
- var opts = extend({strictErrors: false}, options);
- if (opts.strictErrors === true) opts.strict = true;
- var res = extglob.create(pattern, opts);
- return toRegex(res.output, opts);
- }
- var regex = utils.memoize('makeRe', pattern, options, makeRe);
- if (regex.source.length > MAX_LENGTH) {
- throw new SyntaxError('potentially malicious regex detected');
- }
- return regex;
- };
- extglob.cache = utils.cache;
- extglob.clearCache = function() {
- extglob.cache.__data__ = {};
- };
- extglob.Extglob = Extglob;
- extglob.compilers = compilers;
- extglob.parsers = parsers;
- module.exports = extglob;
|