index.js 763 B

123456789101112131415161718192021222324
  1. 'use strict';
  2. var path = require('path');
  3. var isglob = require('is-glob');
  4. var pathDirname = require('path-dirname');
  5. var isWin32 = require('os').platform() === 'win32';
  6. module.exports = function globParent(str) {
  7. // flip windows path separators
  8. if (isWin32 && str.indexOf('/') < 0) str = str.split('\\').join('/');
  9. // special case for strings ending in enclosure containing path separator
  10. if (/[\{\[].*[\/]*.*[\}\]]$/.test(str)) str += '/';
  11. // preserves full path in case of trailing path separator
  12. str += 'a';
  13. // remove path parts that are globby
  14. do {str = pathDirname.posix(str)}
  15. while (isglob(str) || /(^|[^\\])([\{\[]|\([^\)]+$)/.test(str));
  16. // remove escape chars and return result
  17. return str.replace(/\\([\*\?\|\[\]\(\)\{\}])/g, '$1');
  18. };