utils.js 7.2 KB

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