3 var utils = module.exports;
4 var path = require('path');
10 var Snapdragon = require('snapdragon');
11 utils.define = require('define-property');
12 utils.diff = require('arr-diff');
13 utils.extend = require('extend-shallow');
14 utils.pick = require('object.pick');
15 utils.typeOf = require('kind-of');
16 utils.unique = require('array-unique');
19 * Returns true if the platform is windows, or `path.sep` is `\\`.
20 * This is defined as a function to allow `path.sep` to be set in unit tests,
21 * or by the user, if there is a reason to do so.
25 utils.isWindows = function() {
26 return path.sep === '\\' || process.platform === 'win32';
30 * Get the `Snapdragon` instance to use
33 utils.instantiate = function(ast, options) {
35 // if an instance was created by `.parse`, use that instance
36 if (utils.typeOf(ast) === 'object' && ast.snapdragon) {
37 snapdragon = ast.snapdragon;
38 // if the user supplies an instance on options, use that instance
39 } else if (utils.typeOf(options) === 'object' && options.snapdragon) {
40 snapdragon = options.snapdragon;
41 // create a new instance
43 snapdragon = new Snapdragon(options);
46 utils.define(snapdragon, 'parse', function(str, options) {
47 var parsed = Snapdragon.prototype.parse.apply(this, arguments);
50 // escape unmatched brace/bracket/parens
51 var last = this.parser.stack.pop();
52 if (last && this.options.strictErrors !== true) {
53 var open = last.nodes[0];
54 var inner = last.nodes[1];
55 if (last.type === 'bracket') {
56 if (inner.val.charAt(0) === '[') {
57 inner.val = '\\' + inner.val;
61 open.val = '\\' + open.val;
62 var sibling = open.parent.nodes[1];
63 if (sibling.type === 'star') {
69 // add non-enumerable parser reference
70 utils.define(parsed, 'parser', this.parser);
78 * Create the key to use for memoization. The key is generated
79 * by iterating over the options and concatenating key-value pairs
80 * to the pattern string.
83 utils.createKey = function(pattern, options) {
84 if (utils.typeOf(options) !== 'object') {
88 var keys = Object.keys(options);
89 for (var i = 0; i < keys.length; i++) {
91 val += ';' + key + '=' + String(options[key]);
97 * Cast `val` to an array
101 utils.arrayify = function(val) {
102 if (typeof val === 'string') return [val];
103 return val ? (Array.isArray(val) ? val : [val]) : [];
107 * Return true if `val` is a non-empty string
110 utils.isString = function(val) {
111 return typeof val === 'string';
115 * Return true if `val` is a non-empty string
118 utils.isObject = function(val) {
119 return utils.typeOf(val) === 'object';
123 * Returns true if the given `str` has special characters
126 utils.hasSpecialChars = function(str) {
127 return /(?:(?:(^|\/)[!.])|[*?+()|\[\]{}]|[+@]\()/.test(str);
131 * Escape regex characters in the given string
134 utils.escapeRegex = function(str) {
135 return str.replace(/[-[\]{}()^$|*+?.\\\/\s]/g, '\\$&');
139 * Normalize slashes in the given filepath.
141 * @param {String} `filepath`
145 utils.toPosixPath = function(str) {
146 return str.replace(/\\+/g, '/');
150 * Strip backslashes before special characters in a string.
152 * @param {String} `str`
156 utils.unescape = function(str) {
157 return utils.toPosixPath(str.replace(/\\(?=[*+?!.])/g, ''));
161 * Strip the prefix from a filepath
162 * @param {String} `fp`
166 utils.stripPrefix = function(str) {
167 if (str.charAt(0) !== '.') {
170 var ch = str.charAt(1);
171 if (utils.isSlash(ch)) {
178 * Returns true if the given str is an escaped or
179 * unescaped path character
182 utils.isSlash = function(str) {
183 return str === '/' || str === '\\/' || str === '\\' || str === '\\\\';
187 * Returns a function that returns true if the given
188 * pattern matches or contains a `filepath`
190 * @param {String} `pattern`
194 utils.matchPath = function(pattern, options) {
195 return (options && options.contains)
196 ? utils.containsPattern(pattern, options)
197 : utils.equalsPattern(pattern, options);
201 * Returns true if the given (original) filepath or unixified path are equal
202 * to the given pattern.
205 utils._equals = function(filepath, unixPath, pattern) {
206 return pattern === filepath || pattern === unixPath;
210 * Returns true if the given (original) filepath or unixified path contain
214 utils._contains = function(filepath, unixPath, pattern) {
215 return filepath.indexOf(pattern) !== -1 || unixPath.indexOf(pattern) !== -1;
219 * Returns a function that returns true if the given
220 * pattern is the same as a given `filepath`
222 * @param {String} `pattern`
226 utils.equalsPattern = function(pattern, options) {
227 var unixify = utils.unixify(options);
228 options = options || {};
230 return function fn(filepath) {
231 var equal = utils._equals(filepath, unixify(filepath), pattern);
232 if (equal === true || options.nocase !== true) {
235 var lower = filepath.toLowerCase();
236 return utils._equals(lower, unixify(lower), pattern);
241 * Returns a function that returns true if the given
242 * pattern contains a `filepath`
244 * @param {String} `pattern`
248 utils.containsPattern = function(pattern, options) {
249 var unixify = utils.unixify(options);
250 options = options || {};
252 return function(filepath) {
253 var contains = utils._contains(filepath, unixify(filepath), pattern);
254 if (contains === true || options.nocase !== true) {
257 var lower = filepath.toLowerCase();
258 return utils._contains(lower, unixify(lower), pattern);
263 * Returns a function that returns true if the given
264 * regex matches the `filename` of a file path.
266 * @param {RegExp} `re` Matching regex
270 utils.matchBasename = function(re) {
271 return function(filepath) {
272 return re.test(path.basename(filepath));
277 * Determines the filepath to return based on the provided options.
281 utils.value = function(str, unixify, options) {
282 if (options && options.unixify === false) {
289 * Returns a function that normalizes slashes in a string to forward
290 * slashes, strips `./` from beginning of paths, and optionally unescapes
291 * special characters.
295 utils.unixify = function(options) {
296 options = options || {};
297 return function(filepath) {
298 if (utils.isWindows() || options.unixify === true) {
299 filepath = utils.toPosixPath(filepath);
301 if (options.stripPrefix !== false) {
302 filepath = utils.stripPrefix(filepath);
304 if (options.unescape === true) {
305 filepath = utils.unescape(filepath);