utils.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. 'use strict';
  2. var utils = module.exports;
  3. var path = require('path');
  4. /**
  5. * Module dependencies
  6. */
  7. var Snapdragon = require('snapdragon');
  8. utils.define = require('define-property');
  9. utils.diff = require('arr-diff');
  10. utils.extend = require('extend-shallow');
  11. utils.pick = require('object.pick');
  12. utils.typeOf = require('kind-of');
  13. utils.unique = require('array-unique');
  14. /**
  15. * Returns true if the given value is effectively an empty string
  16. */
  17. utils.isEmptyString = function(val) {
  18. return String(val) === '' || String(val) === './';
  19. };
  20. /**
  21. * Returns true if the platform is windows, or `path.sep` is `\\`.
  22. * This is defined as a function to allow `path.sep` to be set in unit tests,
  23. * or by the user, if there is a reason to do so.
  24. * @return {Boolean}
  25. */
  26. utils.isWindows = function() {
  27. return path.sep === '\\' || process.platform === 'win32';
  28. };
  29. /**
  30. * Return the last element from an array
  31. */
  32. utils.last = function(arr, n) {
  33. return arr[arr.length - (n || 1)];
  34. };
  35. /**
  36. * Get the `Snapdragon` instance to use
  37. */
  38. utils.instantiate = function(ast, options) {
  39. var snapdragon;
  40. // if an instance was created by `.parse`, use that instance
  41. if (utils.typeOf(ast) === 'object' && ast.snapdragon) {
  42. snapdragon = ast.snapdragon;
  43. // if the user supplies an instance on options, use that instance
  44. } else if (utils.typeOf(options) === 'object' && options.snapdragon) {
  45. snapdragon = options.snapdragon;
  46. // create a new instance
  47. } else {
  48. snapdragon = new Snapdragon(options);
  49. }
  50. utils.define(snapdragon, 'parse', function(str, options) {
  51. var parsed = Snapdragon.prototype.parse.apply(this, arguments);
  52. parsed.input = str;
  53. // escape unmatched brace/bracket/parens
  54. var last = this.parser.stack.pop();
  55. if (last && this.options.strictErrors !== true) {
  56. var open = last.nodes[0];
  57. var inner = last.nodes[1];
  58. if (last.type === 'bracket') {
  59. if (inner.val.charAt(0) === '[') {
  60. inner.val = '\\' + inner.val;
  61. }
  62. } else {
  63. open.val = '\\' + open.val;
  64. var sibling = open.parent.nodes[1];
  65. if (sibling.type === 'star') {
  66. sibling.loose = true;
  67. }
  68. }
  69. }
  70. // add non-enumerable parser reference
  71. utils.define(parsed, 'parser', this.parser);
  72. return parsed;
  73. });
  74. return snapdragon;
  75. };
  76. /**
  77. * Create the key to use for memoization. The key is generated
  78. * by iterating over the options and concatenating key-value pairs
  79. * to the pattern string.
  80. */
  81. utils.createKey = function(pattern, options) {
  82. if (typeof options === 'undefined') {
  83. return pattern;
  84. }
  85. var key = pattern;
  86. for (var prop in options) {
  87. if (options.hasOwnProperty(prop)) {
  88. key += ';' + prop + '=' + String(options[prop]);
  89. }
  90. }
  91. return key;
  92. };
  93. /**
  94. * Cast `val` to an array
  95. * @return {Array}
  96. */
  97. utils.arrayify = function(val) {
  98. if (typeof val === 'string') return [val];
  99. return val ? (Array.isArray(val) ? val : [val]) : [];
  100. };
  101. /**
  102. * Return true if `val` is a non-empty string
  103. */
  104. utils.isString = function(val) {
  105. return typeof val === 'string';
  106. };
  107. /**
  108. * Return true if `val` is a non-empty string
  109. */
  110. utils.isRegex = function(val) {
  111. return utils.typeOf(val) === 'regexp';
  112. };
  113. /**
  114. * Return true if `val` is a non-empty string
  115. */
  116. utils.isObject = function(val) {
  117. return utils.typeOf(val) === 'object';
  118. };
  119. /**
  120. * Escape regex characters in the given string
  121. */
  122. utils.escapeRegex = function(str) {
  123. return str.replace(/[-[\]{}()^$|*+?.\\\/\s]/g, '\\$&');
  124. };
  125. /**
  126. * Combines duplicate characters in the provided string.
  127. * @param {String} `str`
  128. * @returns {String}
  129. */
  130. utils.combineDuplicates = function(str, val) {
  131. if (typeof val === 'string') {
  132. var re = new RegExp('(' + val + ')(?=(?:' + val + ')*\\1)', 'g');
  133. return str.replace(re, '');
  134. }
  135. return str.replace(/(.)(?=.*\1)/g, '');
  136. };
  137. /**
  138. * Returns true if the given `str` has special characters
  139. */
  140. utils.hasSpecialChars = function(str) {
  141. return /(?:(?:(^|\/)[!.])|[*?+()|\[\]{}]|[+@]\()/.test(str);
  142. };
  143. /**
  144. * Normalize slashes in the given filepath.
  145. *
  146. * @param {String} `filepath`
  147. * @return {String}
  148. */
  149. utils.toPosixPath = function(str) {
  150. return str.replace(/\\+/g, '/');
  151. };
  152. /**
  153. * Strip backslashes before special characters in a string.
  154. *
  155. * @param {String} `str`
  156. * @return {String}
  157. */
  158. utils.unescape = function(str) {
  159. return utils.toPosixPath(str.replace(/\\(?=[*+?!.])/g, ''));
  160. };
  161. /**
  162. * Strip the drive letter from a windows filepath
  163. * @param {String} `fp`
  164. * @return {String}
  165. */
  166. utils.stripDrive = function(fp) {
  167. return utils.isWindows() ? fp.replace(/^[a-z]:[\\\/]+?/i, '/') : fp;
  168. };
  169. /**
  170. * Strip the prefix from a filepath
  171. * @param {String} `fp`
  172. * @return {String}
  173. */
  174. utils.stripPrefix = function(str) {
  175. if (str.charAt(0) === '.' && (str.charAt(1) === '/' || str.charAt(1) === '\\')) {
  176. return str.slice(2);
  177. }
  178. return str;
  179. };
  180. /**
  181. * Returns true if `str` is a common character that doesn't need
  182. * to be processed to be used for matching.
  183. * @param {String} `str`
  184. * @return {Boolean}
  185. */
  186. utils.isSimpleChar = function(str) {
  187. return str === '' || str === ' ' || str === '.';
  188. };
  189. /**
  190. * Returns true if the given str is an escaped or
  191. * unescaped path character
  192. */
  193. utils.isSlash = function(str) {
  194. return str === '/' || str === '\\/' || str === '\\' || str === '\\\\';
  195. };
  196. /**
  197. * Returns a function that returns true if the given
  198. * pattern matches or contains a `filepath`
  199. *
  200. * @param {String} `pattern`
  201. * @return {Function}
  202. */
  203. utils.matchPath = function(pattern, options) {
  204. return (options && options.contains)
  205. ? utils.containsPattern(pattern, options)
  206. : utils.equalsPattern(pattern, options);
  207. };
  208. /**
  209. * Returns true if the given (original) filepath or unixified path are equal
  210. * to the given pattern.
  211. */
  212. utils._equals = function(filepath, unixPath, pattern) {
  213. return pattern === filepath || pattern === unixPath;
  214. };
  215. /**
  216. * Returns true if the given (original) filepath or unixified path contain
  217. * the given pattern.
  218. */
  219. utils._contains = function(filepath, unixPath, pattern) {
  220. return filepath.indexOf(pattern) !== -1 || unixPath.indexOf(pattern) !== -1;
  221. };
  222. /**
  223. * Returns a function that returns true if the given
  224. * pattern is the same as a given `filepath`
  225. *
  226. * @param {String} `pattern`
  227. * @return {Function}
  228. */
  229. utils.equalsPattern = function(pattern, options) {
  230. var unixify = utils.unixify(options);
  231. options = options || {};
  232. return function fn(filepath) {
  233. var equal = utils._equals(filepath, unixify(filepath), pattern);
  234. if (equal === true || options.nocase !== true) {
  235. return equal;
  236. }
  237. var lower = filepath.toLowerCase();
  238. return utils._equals(lower, unixify(lower), pattern);
  239. };
  240. };
  241. /**
  242. * Returns a function that returns true if the given
  243. * pattern contains a `filepath`
  244. *
  245. * @param {String} `pattern`
  246. * @return {Function}
  247. */
  248. utils.containsPattern = function(pattern, options) {
  249. var unixify = utils.unixify(options);
  250. options = options || {};
  251. return function(filepath) {
  252. var contains = utils._contains(filepath, unixify(filepath), pattern);
  253. if (contains === true || options.nocase !== true) {
  254. return contains;
  255. }
  256. var lower = filepath.toLowerCase();
  257. return utils._contains(lower, unixify(lower), pattern);
  258. };
  259. };
  260. /**
  261. * Returns a function that returns true if the given
  262. * regex matches the `filename` of a file path.
  263. *
  264. * @param {RegExp} `re` Matching regex
  265. * @return {Function}
  266. */
  267. utils.matchBasename = function(re) {
  268. return function(filepath) {
  269. return re.test(filepath) || re.test(path.basename(filepath));
  270. };
  271. };
  272. /**
  273. * Returns the given value unchanced.
  274. * @return {any}
  275. */
  276. utils.identity = function(val) {
  277. return val;
  278. };
  279. /**
  280. * Determines the filepath to return based on the provided options.
  281. * @return {any}
  282. */
  283. utils.value = function(str, unixify, options) {
  284. if (options && options.unixify === false) {
  285. return str;
  286. }
  287. if (options && typeof options.unixify === 'function') {
  288. return options.unixify(str);
  289. }
  290. return unixify(str);
  291. };
  292. /**
  293. * Returns a function that normalizes slashes in a string to forward
  294. * slashes, strips `./` from beginning of paths, and optionally unescapes
  295. * special characters.
  296. * @return {Function}
  297. */
  298. utils.unixify = function(options) {
  299. var opts = options || {};
  300. return function(filepath) {
  301. if (opts.stripPrefix !== false) {
  302. filepath = utils.stripPrefix(filepath);
  303. }
  304. if (opts.unescape === true) {
  305. filepath = utils.unescape(filepath);
  306. }
  307. if (opts.unixify === true || utils.isWindows()) {
  308. filepath = utils.toPosixPath(filepath);
  309. }
  310. return filepath;
  311. };
  312. };