選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

utils.js 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. * Returns true if the given `str` has special characters
  103. */
  104. utils.hasSpecialChars = function(str) {
  105. return /(?:(?:(^|\/)[!.])|[*?+()|\[\]{}]|[+@]\()/.test(str);
  106. };
  107. /**
  108. * Escape regex characters in the given string
  109. */
  110. utils.escapeRegex = function(str) {
  111. return str.replace(/[-[\]{}()^$|*+?.\\\/\s]/g, '\\$&');
  112. };
  113. /**
  114. * Normalize slashes in the given filepath.
  115. *
  116. * @param {String} `filepath`
  117. * @return {String}
  118. */
  119. utils.toPosixPath = function(str) {
  120. return str.replace(/\\+/g, '/');
  121. };
  122. /**
  123. * Strip backslashes before special characters in a string.
  124. *
  125. * @param {String} `str`
  126. * @return {String}
  127. */
  128. utils.unescape = function(str) {
  129. return utils.toPosixPath(str.replace(/\\(?=[*+?!.])/g, ''));
  130. };
  131. /**
  132. * Strip the prefix from a filepath
  133. * @param {String} `fp`
  134. * @return {String}
  135. */
  136. utils.stripPrefix = function(str) {
  137. if (str.charAt(0) !== '.') {
  138. return str;
  139. }
  140. var ch = str.charAt(1);
  141. if (utils.isSlash(ch)) {
  142. return str.slice(2);
  143. }
  144. return str;
  145. };
  146. /**
  147. * Returns true if the given str is an escaped or
  148. * unescaped path character
  149. */
  150. utils.isSlash = function(str) {
  151. return str === '/' || str === '\\/' || str === '\\' || str === '\\\\';
  152. };
  153. /**
  154. * Returns a function that returns true if the given
  155. * pattern matches or contains a `filepath`
  156. *
  157. * @param {String} `pattern`
  158. * @return {Function}
  159. */
  160. utils.matchPath = function(pattern, options) {
  161. return (options && options.contains)
  162. ? utils.containsPattern(pattern, options)
  163. : utils.equalsPattern(pattern, options);
  164. };
  165. /**
  166. * Returns true if the given (original) filepath or unixified path are equal
  167. * to the given pattern.
  168. */
  169. utils._equals = function(filepath, unixPath, pattern) {
  170. return pattern === filepath || pattern === unixPath;
  171. };
  172. /**
  173. * Returns true if the given (original) filepath or unixified path contain
  174. * the given pattern.
  175. */
  176. utils._contains = function(filepath, unixPath, pattern) {
  177. return filepath.indexOf(pattern) !== -1 || unixPath.indexOf(pattern) !== -1;
  178. };
  179. /**
  180. * Returns a function that returns true if the given
  181. * pattern is the same as a given `filepath`
  182. *
  183. * @param {String} `pattern`
  184. * @return {Function}
  185. */
  186. utils.equalsPattern = function(pattern, options) {
  187. var unixify = utils.unixify(options);
  188. options = options || {};
  189. return function fn(filepath) {
  190. var equal = utils._equals(filepath, unixify(filepath), pattern);
  191. if (equal === true || options.nocase !== true) {
  192. return equal;
  193. }
  194. var lower = filepath.toLowerCase();
  195. return utils._equals(lower, unixify(lower), pattern);
  196. };
  197. };
  198. /**
  199. * Returns a function that returns true if the given
  200. * pattern contains a `filepath`
  201. *
  202. * @param {String} `pattern`
  203. * @return {Function}
  204. */
  205. utils.containsPattern = function(pattern, options) {
  206. var unixify = utils.unixify(options);
  207. options = options || {};
  208. return function(filepath) {
  209. var contains = utils._contains(filepath, unixify(filepath), pattern);
  210. if (contains === true || options.nocase !== true) {
  211. return contains;
  212. }
  213. var lower = filepath.toLowerCase();
  214. return utils._contains(lower, unixify(lower), pattern);
  215. };
  216. };
  217. /**
  218. * Returns a function that returns true if the given
  219. * regex matches the `filename` of a file path.
  220. *
  221. * @param {RegExp} `re` Matching regex
  222. * @return {Function}
  223. */
  224. utils.matchBasename = function(re) {
  225. return function(filepath) {
  226. return re.test(path.basename(filepath));
  227. };
  228. };
  229. /**
  230. * Determines the filepath to return based on the provided options.
  231. * @return {any}
  232. */
  233. utils.value = function(str, unixify, options) {
  234. if (options && options.unixify === false) {
  235. return str;
  236. }
  237. return unixify(str);
  238. };
  239. /**
  240. * Returns a function that normalizes slashes in a string to forward
  241. * slashes, strips `./` from beginning of paths, and optionally unescapes
  242. * special characters.
  243. * @return {Function}
  244. */
  245. utils.unixify = function(options) {
  246. options = options || {};
  247. return function(filepath) {
  248. if (utils.isWindows() || options.unixify === true) {
  249. filepath = utils.toPosixPath(filepath);
  250. }
  251. if (options.stripPrefix !== false) {
  252. filepath = utils.stripPrefix(filepath);
  253. }
  254. if (options.unescape === true) {
  255. filepath = utils.unescape(filepath);
  256. }
  257. return filepath;
  258. };
  259. };