Initial commit
[yaffs-website] / node_modules / micromatch / lib / utils.js
1 'use strict';
2
3 var win32 = process && process.platform === 'win32';
4 var path = require('path');
5 var fileRe = require('filename-regex');
6 var utils = module.exports;
7
8 /**
9  * Module dependencies
10  */
11
12 utils.diff = require('arr-diff');
13 utils.unique = require('array-unique');
14 utils.braces = require('braces');
15 utils.brackets = require('expand-brackets');
16 utils.extglob = require('extglob');
17 utils.isExtglob = require('is-extglob');
18 utils.isGlob = require('is-glob');
19 utils.typeOf = require('kind-of');
20 utils.normalize = require('normalize-path');
21 utils.omit = require('object.omit');
22 utils.parseGlob = require('parse-glob');
23 utils.cache = require('regex-cache');
24
25 /**
26  * Get the filename of a filepath
27  *
28  * @param {String} `string`
29  * @return {String}
30  */
31
32 utils.filename = function filename(fp) {
33   var seg = fp.match(fileRe());
34   return seg && seg[0];
35 };
36
37 /**
38  * Returns a function that returns true if the given
39  * pattern is the same as a given `filepath`
40  *
41  * @param {String} `pattern`
42  * @return {Function}
43  */
44
45 utils.isPath = function isPath(pattern, opts) {
46   opts = opts || {};
47   return function(fp) {
48     var unixified = utils.unixify(fp, opts);
49     if(opts.nocase){
50       return pattern.toLowerCase() === unixified.toLowerCase();
51     }
52     return pattern === unixified;
53   };
54 };
55
56 /**
57  * Returns a function that returns true if the given
58  * pattern contains a `filepath`
59  *
60  * @param {String} `pattern`
61  * @return {Function}
62  */
63
64 utils.hasPath = function hasPath(pattern, opts) {
65   return function(fp) {
66     return utils.unixify(pattern, opts).indexOf(fp) !== -1;
67   };
68 };
69
70 /**
71  * Returns a function that returns true if the given
72  * pattern matches or contains a `filepath`
73  *
74  * @param {String} `pattern`
75  * @return {Function}
76  */
77
78 utils.matchPath = function matchPath(pattern, opts) {
79   var fn = (opts && opts.contains)
80     ? utils.hasPath(pattern, opts)
81     : utils.isPath(pattern, opts);
82   return fn;
83 };
84
85 /**
86  * Returns a function that returns true if the given
87  * regex matches the `filename` of a file path.
88  *
89  * @param {RegExp} `re`
90  * @return {Boolean}
91  */
92
93 utils.hasFilename = function hasFilename(re) {
94   return function(fp) {
95     var name = utils.filename(fp);
96     return name && re.test(name);
97   };
98 };
99
100 /**
101  * Coerce `val` to an array
102  *
103  * @param  {*} val
104  * @return {Array}
105  */
106
107 utils.arrayify = function arrayify(val) {
108   return !Array.isArray(val)
109     ? [val]
110     : val;
111 };
112
113 /**
114  * Normalize all slashes in a file path or glob pattern to
115  * forward slashes.
116  */
117
118 utils.unixify = function unixify(fp, opts) {
119   if (opts && opts.unixify === false) return fp;
120   if (opts && opts.unixify === true || win32 || path.sep === '\\') {
121     return utils.normalize(fp, false);
122   }
123   if (opts && opts.unescape === true) {
124     return fp ? fp.toString().replace(/\\(\w)/g, '$1') : '';
125   }
126   return fp;
127 };
128
129 /**
130  * Escape/unescape utils
131  */
132
133 utils.escapePath = function escapePath(fp) {
134   return fp.replace(/[\\.]/g, '\\$&');
135 };
136
137 utils.unescapeGlob = function unescapeGlob(fp) {
138   return fp.replace(/[\\"']/g, '');
139 };
140
141 utils.escapeRe = function escapeRe(str) {
142   return str.replace(/[-[\\$*+?.#^\s{}(|)\]]/g, '\\$&');
143 };
144
145 /**
146  * Expose `utils`
147  */
148
149 module.exports = utils;