2 * parse-glob <https://github.com/jonschlinkert/parse-glob>
4 * Copyright (c) 2015, Jon Schlinkert.
5 * Licensed under the MIT License.
10 var isGlob = require('is-glob');
11 var findBase = require('glob-base');
12 var extglob = require('is-extglob');
13 var dotfile = require('is-dotfile');
19 var cache = module.exports.cache = {};
22 * Parse a glob pattern into tokens.
24 * When no paths or '**' are in the glob, we use a
25 * different strategy for parsing the filename, since
26 * file names can contain braces and other difficult
33 module.exports = function parseGlob(glob) {
34 if (cache.hasOwnProperty(glob)) {
42 // unescape dots and slashes in braces/brackets
45 var parsed = findBase(glob);
46 tok.is.glob = parsed.isGlob;
48 tok.glob = parsed.glob;
49 tok.base = parsed.base;
50 var segs = /([^\/]*)$/.exec(glob);
53 tok.path.dirname = '';
54 tok.path.basename = segs[1] || '';
55 tok.path.dirname = glob.split(tok.path.basename).join('') || '';
56 var basename = (tok.path.basename || '').split('.') || '';
57 tok.path.filename = basename[0] || '';
58 tok.path.extname = basename.slice(1).join('.') || '';
61 if (isGlob(tok.path.dirname) && !tok.path.basename) {
62 if (!/\/$/.test(tok.glob)) {
63 tok.path.basename = tok.glob;
65 tok.path.dirname = tok.base;
68 if (glob.indexOf('/') === -1 && !tok.is.globstar) {
69 tok.path.dirname = '';
70 tok.path.basename = tok.orig;
73 var dot = tok.path.basename.indexOf('.');
75 tok.path.filename = tok.path.basename.slice(0, dot);
76 tok.path.extname = tok.path.basename.slice(dot);
79 if (tok.path.extname.charAt(0) === '.') {
80 var exts = tok.path.extname.split('.');
81 tok.path.ext = exts[exts.length - 1];
84 // unescape dots and slashes in braces/brackets
85 tok.glob = unescape(tok.glob);
86 tok.path.dirname = unescape(tok.path.dirname);
87 tok.path.basename = unescape(tok.path.basename);
88 tok.path.filename = unescape(tok.path.filename);
89 tok.path.extname = unescape(tok.path.extname);
92 var is = (glob && tok.is.glob);
93 tok.is.negated = glob && glob.charAt(0) === '!';
94 tok.is.extglob = glob && extglob(glob);
95 tok.is.braces = has(is, glob, '{');
96 tok.is.brackets = has(is, glob, '[:');
97 tok.is.globstar = has(is, glob, '**');
98 tok.is.dotfile = dotfile(tok.path.basename) || dotfile(tok.path.filename);
99 tok.is.dotdir = dotdir(tok.path.dirname);
100 return (cache[glob] = tok);
104 * Returns true if the glob matches dot-directories.
106 * @param {Object} `tok` The tokens object
107 * @param {Object} `path` The path object
111 function dotdir(base) {
112 if (base.indexOf('/.') !== -1) {
115 if (base.charAt(0) === '.' && base.charAt(1) !== '/') {
122 * Returns true if the pattern has the given `ch`aracter(s)
124 * @param {Object} `glob` The glob pattern.
125 * @param {Object} `ch` The character to test for
129 function has(is, glob, ch) {
130 return is && glob.indexOf(ch) !== -1;
134 * Escape/unescape utils
137 function escape(str) {
138 var re = /\{([^{}]*?)}|\(([^()]*?)\)|\[([^\[\]]*?)\]/g;
139 return str.replace(re, function (outter, braces, parens, brackets) {
140 var inner = braces || parens || brackets;
141 if (!inner) { return outter; }
142 return outter.split(inner).join(esc(inner));
147 str = str.split('/').join('__SLASH__');
148 str = str.split('.').join('__DOT__');
152 function unescape(str) {
153 str = str.split('__SLASH__').join('/');
154 str = str.split('__DOT__').join('.');