Initial commit
[yaffs-website] / node_modules / parse-glob / index.js
1 /*!
2  * parse-glob <https://github.com/jonschlinkert/parse-glob>
3  *
4  * Copyright (c) 2015, Jon Schlinkert.
5  * Licensed under the MIT License.
6  */
7
8 'use strict';
9
10 var isGlob = require('is-glob');
11 var findBase = require('glob-base');
12 var extglob = require('is-extglob');
13 var dotfile = require('is-dotfile');
14
15 /**
16  * Expose `cache`
17  */
18
19 var cache = module.exports.cache = {};
20
21 /**
22  * Parse a glob pattern into tokens.
23  *
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
27  * patterns. such as:
28  *
29  *  - `*.{a,b}`
30  *  - `(**|*.js)`
31  */
32
33 module.exports = function parseGlob(glob) {
34   if (cache.hasOwnProperty(glob)) {
35     return cache[glob];
36   }
37
38   var tok = {};
39   tok.orig = glob;
40   tok.is = {};
41
42   // unescape dots and slashes in braces/brackets
43   glob = escape(glob);
44
45   var parsed = findBase(glob);
46   tok.is.glob = parsed.isGlob;
47
48   tok.glob = parsed.glob;
49   tok.base = parsed.base;
50   var segs = /([^\/]*)$/.exec(glob);
51
52   tok.path = {};
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('.') || '';
59   tok.path.ext = '';
60
61   if (isGlob(tok.path.dirname) && !tok.path.basename) {
62     if (!/\/$/.test(tok.glob)) {
63       tok.path.basename = tok.glob;
64     }
65     tok.path.dirname = tok.base;
66   }
67
68   if (glob.indexOf('/') === -1 && !tok.is.globstar) {
69     tok.path.dirname = '';
70     tok.path.basename = tok.orig;
71   }
72
73   var dot = tok.path.basename.indexOf('.');
74   if (dot !== -1) {
75     tok.path.filename = tok.path.basename.slice(0, dot);
76     tok.path.extname = tok.path.basename.slice(dot);
77   }
78
79   if (tok.path.extname.charAt(0) === '.') {
80     var exts = tok.path.extname.split('.');
81     tok.path.ext = exts[exts.length - 1];
82   }
83
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);
90
91   // Booleans
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);
101 }
102
103 /**
104  * Returns true if the glob matches dot-directories.
105  *
106  * @param  {Object} `tok` The tokens object
107  * @param  {Object} `path` The path object
108  * @return {Object}
109  */
110
111 function dotdir(base) {
112   if (base.indexOf('/.') !== -1) {
113     return true;
114   }
115   if (base.charAt(0) === '.' && base.charAt(1) !== '/') {
116     return true;
117   }
118   return false;
119 }
120
121 /**
122  * Returns true if the pattern has the given `ch`aracter(s)
123  *
124  * @param  {Object} `glob` The glob pattern.
125  * @param  {Object} `ch` The character to test for
126  * @return {Object}
127  */
128
129 function has(is, glob, ch) {
130   return is && glob.indexOf(ch) !== -1;
131 }
132
133 /**
134  * Escape/unescape utils
135  */
136
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));
143   });
144 }
145
146 function esc(str) {
147   str = str.split('/').join('__SLASH__');
148   str = str.split('.').join('__DOT__');
149   return str;
150 }
151
152 function unescape(str) {
153   str = str.split('__SLASH__').join('/');
154   str = str.split('__DOT__').join('.');
155   return str;
156 }