util.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.isInteger = exports.isRoot = exports.toPath = void 0;
  4. exports.unescapeComponent = unescapeComponent;
  5. exports.escapeComponent = escapeComponent;
  6. exports.parseJsonPointer = parseJsonPointer;
  7. exports.formatJsonPointer = formatJsonPointer;
  8. exports.isChild = isChild;
  9. exports.isPathEqual = isPathEqual;
  10. exports.parent = parent;
  11. exports.isValidIndex = isValidIndex;
  12. const r1 = /~1/g;
  13. const r2 = /~0/g;
  14. const r3 = /~/g;
  15. const r4 = /\//g;
  16. /**
  17. * Un-escapes a JSON pointer path component.
  18. */
  19. function unescapeComponent(component) {
  20. if (component.indexOf('~') === -1)
  21. return component;
  22. return component.replace(r1, '/').replace(r2, '~');
  23. }
  24. /**
  25. * Escapes a JSON pointer path component.
  26. */
  27. function escapeComponent(component) {
  28. if (component.indexOf('/') === -1 && component.indexOf('~') === -1)
  29. return component;
  30. return component.replace(r3, '~0').replace(r4, '~1');
  31. }
  32. /**
  33. * Convert JSON pointer like "/foo/bar" to array like ["", "foo", "bar"], while
  34. * also un-escaping reserved characters.
  35. */
  36. function parseJsonPointer(pointer) {
  37. if (!pointer)
  38. return [];
  39. // TODO: Performance of this line can be improved: (1) don't use .split(); (2) don't use .map().
  40. return pointer.slice(1).split('/').map(unescapeComponent);
  41. }
  42. /**
  43. * Escape and format a path array like ["", "foo", "bar"] to JSON pointer
  44. * like "/foo/bar".
  45. */
  46. function formatJsonPointer(path) {
  47. if ((0, exports.isRoot)(path))
  48. return '';
  49. return '/' + path.map((component) => escapeComponent(String(component))).join('/');
  50. }
  51. const toPath = (pointer) => (typeof pointer === 'string' ? parseJsonPointer(pointer) : pointer);
  52. exports.toPath = toPath;
  53. /**
  54. * Returns true if `parent` contains `child` path, false otherwise.
  55. */
  56. function isChild(parent, child) {
  57. if (parent.length >= child.length)
  58. return false;
  59. for (let i = 0; i < parent.length; i++)
  60. if (parent[i] !== child[i])
  61. return false;
  62. return true;
  63. }
  64. function isPathEqual(p1, p2) {
  65. if (p1.length !== p2.length)
  66. return false;
  67. for (let i = 0; i < p1.length; i++)
  68. if (p1[i] !== p2[i])
  69. return false;
  70. return true;
  71. }
  72. // export function getSharedPath(one: Path, two: Path): Path {
  73. // const min = Math.min(one.length, two.length);
  74. // const res: string[] = [];
  75. // for (let i = 0; i < min; i++) {
  76. // if (one[i] === two[i]) res.push(one[i]);
  77. // else break;
  78. // }
  79. // return res as Path;
  80. // }
  81. /**
  82. * Returns true if JSON Pointer points to root value, false otherwise.
  83. */
  84. const isRoot = (path) => !path.length;
  85. exports.isRoot = isRoot;
  86. /**
  87. * Returns parent path, e.g. for ['foo', 'bar', 'baz'] returns ['foo', 'bar'].
  88. */
  89. function parent(path) {
  90. if (path.length < 1)
  91. throw new Error('NO_PARENT');
  92. return path.slice(0, path.length - 1);
  93. }
  94. /**
  95. * Check if path component can be a valid array index.
  96. */
  97. function isValidIndex(index) {
  98. if (typeof index === 'number')
  99. return true;
  100. const n = Number.parseInt(index, 10);
  101. return String(n) === index && n >= 0;
  102. }
  103. const isInteger = (str) => {
  104. const len = str.length;
  105. let i = 0;
  106. let charCode;
  107. while (i < len) {
  108. charCode = str.charCodeAt(i);
  109. if (charCode >= 48 && charCode <= 57) {
  110. i++;
  111. continue;
  112. }
  113. return false;
  114. }
  115. return true;
  116. };
  117. exports.isInteger = isInteger;