提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.4 KiB

  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.parsePath = exports.stringifyPath = exports.escapeKey = void 0;
  4. const escapeKey = (key) => key.replace(/\\/g, '\\\\').replace(/\./g, '\\.');
  5. exports.escapeKey = escapeKey;
  6. const stringifyPath = (path) => path
  7. .map(String)
  8. .map(exports.escapeKey)
  9. .join('.');
  10. exports.stringifyPath = stringifyPath;
  11. const parsePath = (string, legacyPaths) => {
  12. const result = [];
  13. let segment = '';
  14. for (let i = 0; i < string.length; i++) {
  15. let char = string.charAt(i);
  16. if (!legacyPaths && char === '\\') {
  17. const escaped = string.charAt(i + 1);
  18. if (escaped === '\\') {
  19. segment += '\\';
  20. i++;
  21. continue;
  22. }
  23. else if (escaped !== '.') {
  24. throw Error('invalid path');
  25. }
  26. }
  27. const isEscapedDot = char === '\\' && string.charAt(i + 1) === '.';
  28. if (isEscapedDot) {
  29. segment += '.';
  30. i++;
  31. continue;
  32. }
  33. const isEndOfSegment = char === '.';
  34. if (isEndOfSegment) {
  35. result.push(segment);
  36. segment = '';
  37. continue;
  38. }
  39. segment += char;
  40. }
  41. const lastSegment = segment;
  42. result.push(lastSegment);
  43. return result;
  44. };
  45. exports.parsePath = parsePath;
  46. //# sourceMappingURL=pathstringifier.js.map