4
1

index.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict';
  2. var isGlob = require('is-glob');
  3. var pathPosixDirname = require('path').posix.dirname;
  4. var isWin32 = require('os').platform() === 'win32';
  5. var slash = '/';
  6. var backslash = /\\/g;
  7. var enclosure = /[\{\[].*[\}\]]$/;
  8. var globby = /(^|[^\\])([\{\[]|\([^\)]+$)/;
  9. var escaped = /\\([\!\*\?\|\[\]\(\)\{\}])/g;
  10. /**
  11. * @param {string} str
  12. * @param {Object} opts
  13. * @param {boolean} [opts.flipBackslashes=true]
  14. * @returns {string}
  15. */
  16. module.exports = function globParent(str, opts) {
  17. var options = Object.assign({ flipBackslashes: true }, opts);
  18. // flip windows path separators
  19. if (options.flipBackslashes && isWin32 && str.indexOf(slash) < 0) {
  20. str = str.replace(backslash, slash);
  21. }
  22. // special case for strings ending in enclosure containing path separator
  23. if (enclosure.test(str)) {
  24. str += slash;
  25. }
  26. // preserves full path in case of trailing path separator
  27. str += 'a';
  28. // remove path parts that are globby
  29. do {
  30. str = pathPosixDirname(str);
  31. } while (isGlob(str) || globby.test(str));
  32. // remove escape chars and return result
  33. return str.replace(escaped, '$1');
  34. };