提交学习笔记专用
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.

39 lines
1.2 KiB

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