utils.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.normalizePath = normalizePath;
  6. function normalizePath(path, stripTrailing) {
  7. if (path === '\\' || path === '/') {
  8. return '/';
  9. }
  10. const len = path.length;
  11. if (len <= 1) {
  12. return path;
  13. } // ensure that win32 namespaces has two leading slashes, so that the path is
  14. // handled properly by the win32 version of path.parse() after being normalized
  15. // https://msdn.microsoft.com/library/windows/desktop/aa365247(v=vs.85).aspx#namespaces
  16. let prefix = '';
  17. if (len > 4 && path[3] === '\\') {
  18. // eslint-disable-next-line prefer-destructuring
  19. const ch = path[2];
  20. if ((ch === '?' || ch === '.') && path.slice(0, 2) === '\\\\') {
  21. // eslint-disable-next-line no-param-reassign
  22. path = path.slice(2);
  23. prefix = '//';
  24. }
  25. }
  26. const segs = path.split(/[/\\]+/);
  27. if (stripTrailing !== false && segs[segs.length - 1] === '') {
  28. segs.pop();
  29. }
  30. return prefix + segs.join('/');
  31. } // eslint-disable-next-line import/prefer-default-export