utils.js 3.1 KB

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