correctPath.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.unixify = unixify;
  6. exports.correctPath = correctPath;
  7. var isWin = process.platform === 'win32';
  8. function removeTrailingSeparator(str) {
  9. var i = str.length - 1;
  10. if (i < 2) {
  11. return str;
  12. }
  13. while (isSeparator(str, i)) {
  14. i--;
  15. }
  16. return str.substr(0, i + 1);
  17. }
  18. function isSeparator(str, i) {
  19. var _char = str[i];
  20. return i > 0 && (_char === '/' || isWin && _char === '\\');
  21. }
  22. function normalizePath(str, stripTrailing) {
  23. if (typeof str !== 'string') {
  24. throw new TypeError('expected a string');
  25. }
  26. str = str.replace(/[\\\/]+/g, '/');
  27. if (stripTrailing !== false) {
  28. str = removeTrailingSeparator(str);
  29. }
  30. return str;
  31. }
  32. function unixify(filepath) {
  33. var stripTrailing = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  34. if (isWin) {
  35. filepath = normalizePath(filepath, stripTrailing);
  36. return filepath.replace(/^([a-zA-Z]+:|\.\/)/, '');
  37. }
  38. return filepath;
  39. }
  40. function correctPath(filepath) {
  41. return unixify(filepath.replace(/^\\\\\?\\.:\\/, '\\'));
  42. }