@hamak/shared-utils 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/dist/core-utils-chrono.d.ts +8 -0
  2. package/dist/core-utils-chrono.d.ts.map +1 -0
  3. package/dist/core-utils-chrono.js +16 -0
  4. package/dist/core-utils-comparator.d.ts +11 -0
  5. package/dist/core-utils-comparator.d.ts.map +1 -0
  6. package/dist/core-utils-comparator.js +22 -0
  7. package/dist/core-utils-file.d.ts +10 -0
  8. package/dist/core-utils-file.d.ts.map +1 -0
  9. package/dist/core-utils-file.js +1 -0
  10. package/dist/core-utils-filesystem.d.ts +69 -0
  11. package/dist/core-utils-filesystem.d.ts.map +1 -0
  12. package/dist/core-utils-filesystem.js +156 -0
  13. package/dist/core-utils-fqn.d.ts +11 -0
  14. package/dist/core-utils-fqn.d.ts.map +1 -0
  15. package/dist/core-utils-fqn.js +19 -0
  16. package/dist/core-utils-pathway-resolver.d.ts +20 -0
  17. package/dist/core-utils-pathway-resolver.d.ts.map +1 -0
  18. package/dist/core-utils-pathway-resolver.js +31 -0
  19. package/dist/core-utils-pathway.d.ts +84 -0
  20. package/dist/core-utils-pathway.d.ts.map +1 -0
  21. package/dist/core-utils-pathway.js +206 -0
  22. package/dist/core-utils-registry.d.ts +7 -0
  23. package/dist/core-utils-registry.d.ts.map +1 -0
  24. package/dist/core-utils-registry.js +22 -0
  25. package/dist/core-utils-resolver.d.ts +33 -0
  26. package/dist/core-utils-resolver.d.ts.map +1 -0
  27. package/dist/core-utils-resolver.js +65 -0
  28. package/dist/core-utils-stack.d.ts +20 -0
  29. package/dist/core-utils-stack.d.ts.map +1 -0
  30. package/dist/core-utils-stack.js +47 -0
  31. package/dist/core-utils-types-hkt.d.ts +12 -0
  32. package/dist/core-utils-types-hkt.d.ts.map +1 -0
  33. package/dist/core-utils-types-hkt.js +1 -0
  34. package/dist/core-utils-types.d.ts +83 -0
  35. package/dist/core-utils-types.d.ts.map +1 -0
  36. package/dist/core-utils-types.js +39 -0
  37. package/dist/core-utils-validations.d.ts +21 -0
  38. package/dist/core-utils-validations.d.ts.map +1 -0
  39. package/dist/core-utils-validations.js +18 -0
  40. package/dist/core-utils.d.ts +39 -0
  41. package/dist/core-utils.d.ts.map +1 -0
  42. package/dist/core-utils.js +113 -0
  43. package/dist/es2015/core-utils-chrono.js +16 -0
  44. package/dist/es2015/core-utils-comparator.js +22 -0
  45. package/dist/es2015/core-utils-file.js +1 -0
  46. package/dist/es2015/core-utils-filesystem.js +157 -0
  47. package/dist/es2015/core-utils-fqn.js +19 -0
  48. package/dist/es2015/core-utils-pathway-resolver.js +31 -0
  49. package/dist/es2015/core-utils-pathway.js +206 -0
  50. package/dist/es2015/core-utils-registry.js +22 -0
  51. package/dist/es2015/core-utils-resolver.js +68 -0
  52. package/dist/es2015/core-utils-stack.js +47 -0
  53. package/dist/es2015/core-utils-types-hkt.js +1 -0
  54. package/dist/es2015/core-utils-types.js +39 -0
  55. package/dist/es2015/core-utils-validations.js +18 -0
  56. package/dist/es2015/core-utils.js +113 -0
  57. package/dist/es2015/index.js +24 -0
  58. package/dist/es2015/itinerary/hyper-layer-node.js +21 -0
  59. package/dist/es2015/itinerary/itinerary.js +276 -0
  60. package/dist/es2015/itinerary/stack.js +47 -0
  61. package/dist/index.d.ts +19 -0
  62. package/dist/index.d.ts.map +1 -0
  63. package/dist/index.js +24 -0
  64. package/dist/itinerary/hyper-layer-node.d.ts +12 -0
  65. package/dist/itinerary/hyper-layer-node.d.ts.map +1 -0
  66. package/dist/itinerary/hyper-layer-node.js +21 -0
  67. package/dist/itinerary/itinerary.d.ts +86 -0
  68. package/dist/itinerary/itinerary.d.ts.map +1 -0
  69. package/dist/itinerary/itinerary.js +275 -0
  70. package/dist/itinerary/stack.d.ts +20 -0
  71. package/dist/itinerary/stack.d.ts.map +1 -0
  72. package/dist/itinerary/stack.js +47 -0
  73. package/package.json +41 -0
  74. package/src/core-utils-chrono.ts +21 -0
  75. package/src/core-utils-comparator.ts +27 -0
  76. package/src/core-utils-file.ts +10 -0
  77. package/src/core-utils-filesystem.spec.ts +63 -0
  78. package/src/core-utils-filesystem.ts +232 -0
  79. package/src/core-utils-fqn.ts +17 -0
  80. package/src/core-utils-pathway-resolver.ts +36 -0
  81. package/src/core-utils-pathway.ts +232 -0
  82. package/src/core-utils-registry.ts +23 -0
  83. package/src/core-utils-resolver.ts +75 -0
  84. package/src/core-utils-stack.spec.ts +42 -0
  85. package/src/core-utils-stack.ts +62 -0
  86. package/src/core-utils-types-hkt.ts +9 -0
  87. package/src/core-utils-types.ts +127 -0
  88. package/src/core-utils-validations.ts +36 -0
  89. package/src/core-utils.spec.ts +56 -0
  90. package/src/core-utils.ts +139 -0
  91. package/src/index.ts +34 -0
  92. package/src/itinerary/hyper-layer-node.ts +25 -0
  93. package/src/itinerary/itinerary.spec.ts +388 -0
  94. package/src/itinerary/itinerary.ts +363 -0
  95. package/src/itinerary/stack.spec.ts +46 -0
  96. package/src/itinerary/stack.ts +62 -0
  97. package/tsconfig.es2015.json +24 -0
  98. package/tsconfig.json +19 -0
  99. package/vitest.config.ts +8 -0
@@ -0,0 +1,113 @@
1
+ function includes(arr, t) {
2
+ return arr === undefined ? false : arr.includes(t);
3
+ }
4
+ export function isNonNullObject(data) {
5
+ if (data === null || data === undefined) {
6
+ return false;
7
+ }
8
+ const dataType = typeof data;
9
+ return dataType === 'object' && !Array.isArray(data);
10
+ }
11
+ function isBlank(s) {
12
+ return s === null || s === undefined || s.trim() === "";
13
+ }
14
+ function isDefined(data) {
15
+ return data !== null && data !== undefined;
16
+ }
17
+ function isNotDefined(data) {
18
+ return !isDefined(data);
19
+ }
20
+ export function isNullish(o) {
21
+ return o === null || o === undefined;
22
+ }
23
+ export function isEmptyObject(object) {
24
+ for (const property in object) {
25
+ return false;
26
+ }
27
+ return true;
28
+ }
29
+ export function equalSet(a, b) {
30
+ return a.size === b.size && [...a].every((x) => b.has(x));
31
+ }
32
+ export const DEFAULT_EQUAL_FN = (a, b) => a === b;
33
+ export function areSameDefined(a, b, equalFn) {
34
+ if (a === undefined || b === undefined) {
35
+ return false;
36
+ }
37
+ if (a === null || b === null) {
38
+ return false;
39
+ }
40
+ return a === b || (equalFn ? equalFn(a, b) : false);
41
+ }
42
+ /**
43
+ * Warning: non performant O(n2)
44
+ * @param arr
45
+ * @param equals
46
+ */
47
+ export function removeDuplicates(arr, equals = DEFAULT_EQUAL_FN) {
48
+ return arr.filter((item, index) => arr.findIndex((e, i) => i < index && equals(item, e)) === -1);
49
+ }
50
+ // export function removeDuplicatesAndUndefined<T>(arr : (T|undefined)[], equals : EqualFn<T> = DEFAULT_EQUAL_FN) : T[] {
51
+ // return arr.filter((item, index) : item is T => item !== undefined && arr.findIndex((e,i) => i < index && equals(item, e)) === -1)
52
+ // }
53
+ function intersection(o1, o2) {
54
+ return Object.keys(o1).filter({}.hasOwnProperty.bind(o2));
55
+ }
56
+ function deepEqual(obj1, obj2) {
57
+ if (obj1 === obj2) {
58
+ return true;
59
+ }
60
+ if (obj1 === null || obj2 === null || typeof obj1 !== 'object' || typeof obj2 !== 'object') {
61
+ return false;
62
+ }
63
+ if (Array.isArray(obj1) !== Array.isArray(obj2)) {
64
+ return false;
65
+ }
66
+ const keys1 = Object.keys(obj1);
67
+ const keys2 = Object.keys(obj2);
68
+ if (keys1.length !== keys2.length) {
69
+ return false;
70
+ }
71
+ for (const key of keys1) {
72
+ if (!keys2.includes(key)) {
73
+ return false;
74
+ }
75
+ if (!deepEqual(obj1[key], obj2[key])) {
76
+ return false;
77
+ }
78
+ }
79
+ return true;
80
+ }
81
+ function escapeForXPath(input) {
82
+ if (input.includes("'")) {
83
+ if (input.includes('"')) {
84
+ // Si la chaîne contient à la fois des guillemets simples et doubles
85
+ const parts = input.split("'");
86
+ return "concat(" + parts.map(part => `'${part}'`).join(", \"'\", ") + ")";
87
+ }
88
+ else {
89
+ // Si la chaîne ne contient que des guillemets simples
90
+ return `"${input}"`;
91
+ }
92
+ }
93
+ else {
94
+ // Si la chaîne ne contient que des guillemets doubles ou aucun guillemet
95
+ return `'${input}'`;
96
+ }
97
+ }
98
+ // TODO rename this function
99
+ function cascadeFilter(arr, fns) {
100
+ if (arr.length === 0) {
101
+ return arr;
102
+ }
103
+ else {
104
+ for (const filterFn of fns) {
105
+ const filtered = arr.filter(filterFn);
106
+ if (filtered.length > 0) {
107
+ return filtered;
108
+ }
109
+ }
110
+ return arr;
111
+ }
112
+ }
113
+ export const utils = { includes, isNonNullObject, isEmptyObject, equalSet, isDefined, isBlank, isNotDefined, removeDuplicates, areSameDefined, intersection, deepEqual, escapeForXPath, cascadeFilter };
@@ -0,0 +1,16 @@
1
+ export class Chronometre {
2
+ constructor() {
3
+ this.start = Date.now();
4
+ }
5
+ duration() {
6
+ return Date.now() - this.start;
7
+ }
8
+ restart() {
9
+ const d = this.duration();
10
+ this.start = Date.now();
11
+ return d;
12
+ }
13
+ }
14
+ export function chronometre() {
15
+ return new Chronometre();
16
+ }
@@ -0,0 +1,22 @@
1
+ export function reverseOrder(compareFn) {
2
+ return (a, b, fns) => -compareFn(a, b, fns);
3
+ }
4
+ export function compare(a, b, ...fns) {
5
+ if (fns.length === 0) {
6
+ if (typeof a === "number" && typeof b === "number") {
7
+ fns = [(x) => x];
8
+ }
9
+ else {
10
+ throw new Error(`Expecting at least one weight function when arguments are not numbers`);
11
+ }
12
+ }
13
+ for (const weightFn of fns) {
14
+ const aWeight = weightFn(a);
15
+ const bWeight = weightFn(b);
16
+ if (aWeight != bWeight) {
17
+ return a < b ? -1 : 1;
18
+ }
19
+ }
20
+ return 0;
21
+ }
22
+ export const compareDesc = reverseOrder(compare);
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,157 @@
1
+ import { Pathway } from './core-utils-pathway';
2
+ export function fileSystemNodeInitialState(contentPresent = true) {
3
+ return {
4
+ contentLoaded: contentPresent,
5
+ contentLoading: false,
6
+ contentLoadError: undefined,
7
+ contentHistory: [],
8
+ extensionStates: {}
9
+ };
10
+ }
11
+ function isRoot(node) {
12
+ switch (node.type) {
13
+ case "directory": {
14
+ return node.name === null || node.name === undefined || node.name.trim() === "" || node.name.trim() === "/";
15
+ }
16
+ case "file": {
17
+ return false;
18
+ }
19
+ }
20
+ }
21
+ export class DirectoryNodePathwayResolver {
22
+ constructor(root) {
23
+ this.root = root;
24
+ }
25
+ resolve(path) {
26
+ return fsutils.resolveFileNode(this.root, path);
27
+ }
28
+ changeDIr(path) {
29
+ const fileNode = this.resolve(path);
30
+ if (fileNode === undefined) {
31
+ throw new Error(`No such directory: ${Pathway.asString(path)}`);
32
+ }
33
+ else {
34
+ switch (fileNode.type) {
35
+ case "directory": return new DirectoryNodePathwayResolver(fileNode);
36
+ case "file": throw new Error(`Path is not directory: ${Pathway.asString(path)}`);
37
+ }
38
+ }
39
+ }
40
+ }
41
+ class FilesystemUtil {
42
+ createRootDir(children, state = fileSystemNodeInitialState()) {
43
+ return this.createDir("", children, state);
44
+ }
45
+ createDir(name, children, state = fileSystemNodeInitialState()) {
46
+ return {
47
+ type: "directory", name, state,
48
+ children: children.reduce((acc, child) => (Object.assign(Object.assign({}, acc), { [child.name]: child })), {})
49
+ };
50
+ }
51
+ createDirs(path, children, state = fileSystemNodeInitialState()) {
52
+ const pathway = Pathway.of(path);
53
+ const dir = pathway.getSegments().reverse().reduce((acc, segment) => {
54
+ return this.createDir(segment, acc === undefined ? children : [acc]);
55
+ }, undefined);
56
+ if (dir === undefined) {
57
+ throw new Error(`Unable de create dir '${path}'`);
58
+ }
59
+ return pathway.isAbsolute() ? this.createRootDir([dir]) : dir;
60
+ }
61
+ createFile(name, content, schema = "xs:any", state = fileSystemNodeInitialState()) {
62
+ return { type: "file", name, content, schema, state };
63
+ }
64
+ resolveFileNode(fromDir, path) {
65
+ if (fromDir === undefined) {
66
+ return undefined;
67
+ }
68
+ const pathway = Pathway.of(path);
69
+ if (pathway.isAbsolute() && !isRoot(fromDir)) {
70
+ return undefined;
71
+ }
72
+ const result = pathway.getSegments().reduce((acc, segment) => {
73
+ var _a;
74
+ if (acc === undefined) {
75
+ return undefined;
76
+ }
77
+ else {
78
+ switch (acc.type) {
79
+ case "directory":
80
+ {
81
+ return (_a = acc.children) === null || _a === void 0 ? void 0 : _a[segment];
82
+ }
83
+ break;
84
+ case "file":
85
+ {
86
+ return undefined;
87
+ }
88
+ break;
89
+ default: {
90
+ return undefined;
91
+ }
92
+ }
93
+ }
94
+ }, fromDir);
95
+ return result;
96
+ }
97
+ asFile(fsn) {
98
+ return (fsn === null || fsn === void 0 ? void 0 : fsn.type) === "file" ? fsn : undefined;
99
+ }
100
+ asDirectory(fsn) {
101
+ return (fsn === null || fsn === void 0 ? void 0 : fsn.type) === "directory" ? fsn : undefined;
102
+ }
103
+ resolveDescendent(fromDir, path) {
104
+ const pathway = Pathway.of(path);
105
+ return pathway.isCurrentDir() ? undefined : this.resolveFileNode(fromDir, pathway);
106
+ }
107
+ /**
108
+ * Set a file node content a specified location. It create intermediary directories and target if missed location.
109
+ * @param fromDir
110
+ * @param path
111
+ * @param content
112
+ * @param schema
113
+ * @param state
114
+ */
115
+ createDescendent(fromDir, path, content, schema = "xs:any", state = fileSystemNodeInitialState(), override = false) {
116
+ if (!fromDir)
117
+ return undefined;
118
+ const pathway = Pathway.of(path);
119
+ if (pathway.isCurrentDir())
120
+ return undefined;
121
+ const segments = pathway.getSegments();
122
+ if (segments.length === 0)
123
+ return undefined;
124
+ if (fromDir.type !== "directory") {
125
+ throw new Error("Cannot create descendent on a non-directory node.");
126
+ }
127
+ let currentDir = fromDir;
128
+ for (let i = 0; i < segments.length - 1; i++) {
129
+ const segment = segments[i];
130
+ let nextNode = currentDir.children[segment];
131
+ if (!nextNode) {
132
+ // Create intermediate directory
133
+ const newDir = this.createDir(segment, []);
134
+ currentDir.children[segment] = newDir;
135
+ nextNode = newDir;
136
+ }
137
+ if (nextNode.type !== "directory") {
138
+ throw new Error(`Path conflict: '${segment}' is a file, expected a directory.`);
139
+ }
140
+ currentDir = nextNode;
141
+ }
142
+ const fileName = segments[segments.length - 1];
143
+ const existingNode = currentDir.children[fileName];
144
+ if (existingNode) {
145
+ if (existingNode.type !== "file") {
146
+ throw new Error(`Path conflict: '${fileName}' exists and is not a file.`);
147
+ }
148
+ if (!override) {
149
+ throw new Error(`File '${fileName}' already exists and override is disabled.`);
150
+ }
151
+ }
152
+ const fileNode = this.createFile(fileName, content, schema, state);
153
+ currentDir.children[fileName] = fileNode;
154
+ return fileNode;
155
+ }
156
+ }
157
+ export const fsutils = new FilesystemUtil();
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Unresolved qualified name by either namespace of prefix
3
+ */
4
+ export class LexicalQualifiedName {
5
+ constructor(namespace, prefix, name) {
6
+ this.namespace = namespace;
7
+ this.prefix = prefix;
8
+ this.name = name;
9
+ }
10
+ isEqualTo(other) {
11
+ if ((this.name === undefined && other.name === undefined)
12
+ || (this.name === null && other.name === null)) {
13
+ return false;
14
+ }
15
+ else {
16
+ return this.namespace === other.namespace && this.prefix === other.prefix && this.name === other.name;
17
+ }
18
+ }
19
+ }
@@ -0,0 +1,31 @@
1
+ import { Pathway } from "./core-utils-pathway";
2
+ export class RecordPathwayResolver {
3
+ constructor(record) {
4
+ this.record = record;
5
+ }
6
+ resolve(path) {
7
+ const key = Pathway.asString(path);
8
+ return this.record[key];
9
+ }
10
+ }
11
+ export class EmptyPathwayResolver {
12
+ resolve(path) {
13
+ return undefined;
14
+ }
15
+ }
16
+ export class RelativePathwayResolver {
17
+ constructor(parentResolver, path) {
18
+ this.parentResolver = parentResolver;
19
+ this.path = path;
20
+ this.relativePathway = Pathway.of(path);
21
+ }
22
+ resolve(path) {
23
+ const pathway = Pathway.of(path);
24
+ if (pathway.isAbsolute()) {
25
+ return this.parentResolver.resolve(path);
26
+ }
27
+ else {
28
+ return this.parentResolver.resolve(this.relativePathway.resolve(pathway));
29
+ }
30
+ }
31
+ }
@@ -0,0 +1,206 @@
1
+ /**
2
+ * Class representing a normalized pathway.
3
+ */
4
+ export class Pathway {
5
+ /**
6
+ * Creates a Pathway instance.
7
+ * @param path - The pathway to normalize, either as a string or an array of strings.
8
+ * An empty array or "." represents the current directory.
9
+ */
10
+ constructor(path) {
11
+ if (path == null || path === "." || (Array.isArray(path) && path.length === 0)) {
12
+ this.segments = [];
13
+ this.isAbsolutePath = false;
14
+ }
15
+ else {
16
+ this.isAbsolutePath = Array.isArray(path) ? path[0].startsWith('/') : path.startsWith('/');
17
+ this.segments = this.normalizePath(path);
18
+ }
19
+ }
20
+ /**
21
+ * Factory method to create a Pathway instance.
22
+ * @param path - The pathway to normalize, either as a string or an array of strings.
23
+ * @returns A new Pathway instance.
24
+ */
25
+ static of(path) {
26
+ if (path !== undefined && path !== null && typeof path === "object" && !Array.isArray(path)) {
27
+ return path;
28
+ }
29
+ else {
30
+ return new Pathway(path);
31
+ }
32
+ }
33
+ /**
34
+ * Normalizes a pathway by converting it into an array of segments.
35
+ * @param path - The pathway to normalize, either as a string or an array of strings.
36
+ * @returns An array of normalized path segments, or an empty array if the input is null or undefined.
37
+ */
38
+ normalizePath(path) {
39
+ if (path == null) {
40
+ return [];
41
+ }
42
+ const segments = Array.isArray(path) ? path : path.split('/');
43
+ return segments
44
+ .reduce((acc, segment) => {
45
+ if (segment === "." || segment === "") {
46
+ return acc;
47
+ }
48
+ return acc.concat(segment.split('/'));
49
+ }, [])
50
+ .filter(segment => segment.length > 0);
51
+ }
52
+ /**
53
+ * Returns the normalized path segments.
54
+ * @returns An array of normalized path segments.
55
+ */
56
+ getSegments() {
57
+ return this.segments;
58
+ }
59
+ isCurrentDir() {
60
+ return this.getSegments().length === 0;
61
+ }
62
+ /**
63
+ * Converts the normalized path segments back to a string.
64
+ * @returns The normalized path as a string.
65
+ */
66
+ toString() {
67
+ return this.isAbsolute() ? '/' + this.segments.join('/') : this.segments.join('/');
68
+ }
69
+ /**
70
+ * Checks if the pathway is absolute.
71
+ * @returns True if the pathway is absolute, false otherwise.
72
+ */
73
+ isAbsolute() {
74
+ return this.isAbsolutePath;
75
+ }
76
+ /**
77
+ * Checks if the pathway is relative.
78
+ * @returns True if the pathway is relative, false otherwise.
79
+ */
80
+ isRelative() {
81
+ return !this.isAbsolutePath;
82
+ }
83
+ /**
84
+ * Resolves the current pathway with another pathway or string.
85
+ * @param otherPath - The other pathway or string to resolve against.
86
+ * @returns A new Pathway instance representing the resolved path.
87
+ */
88
+ resolve(otherPath) {
89
+ const otherSegments = otherPath instanceof Pathway ? otherPath.getSegments() : this.normalizePath(otherPath);
90
+ const resolvedSegments = [...this.segments];
91
+ if (otherPath instanceof Pathway ? otherPath.isAbsolute() : (Array.isArray(otherPath) ? otherPath[0].startsWith('/') : otherPath.startsWith('/'))) {
92
+ return new Pathway(otherSegments);
93
+ }
94
+ for (const segment of otherSegments) {
95
+ if (segment === '..') {
96
+ if (resolvedSegments.length > 0 && resolvedSegments[resolvedSegments.length - 1] !== '..') {
97
+ resolvedSegments.pop();
98
+ }
99
+ else {
100
+ resolvedSegments.push(segment);
101
+ }
102
+ }
103
+ else if (segment !== '.') {
104
+ resolvedSegments.push(segment);
105
+ }
106
+ }
107
+ return new Pathway(this.isAbsolute() ? ["/", ...resolvedSegments] : resolvedSegments);
108
+ }
109
+ /**
110
+ * Gets the parent directory of the current pathway.
111
+ * @returns A new Pathway instance representing the parent directory.
112
+ */
113
+ getParent() {
114
+ if (this.segments.length === 0) {
115
+ return new Pathway(null);
116
+ }
117
+ const parentSegments = this.segments.slice(0, -1);
118
+ return new Pathway(this.isAbsolute() ? ["/", ...parentSegments] : parentSegments);
119
+ }
120
+ /**
121
+ * Gets the current directory of the pathway.
122
+ * @returns The current directory as a string.
123
+ */
124
+ getCurrentDirectory() {
125
+ if (this.segments.length === 0) {
126
+ return '';
127
+ }
128
+ return this.segments[this.segments.length - 1];
129
+ }
130
+ /**
131
+ * Static method to get the absolute path of the root.
132
+ * @returns A new Pathway instance representing the root path.
133
+ */
134
+ static ofRoot() {
135
+ return new Pathway('/');
136
+ }
137
+ /**
138
+ * Determines the relative path from this pathway to another pathway.
139
+ * @param otherPath - The other pathway to relativize against.
140
+ * @returns A new Pathway instance representing the relative path.
141
+ */
142
+ relativize(otherPath) {
143
+ const otherSegments = otherPath instanceof Pathway ? otherPath.getSegments() : this.normalizePath(otherPath);
144
+ if (this.isAbsolute() !== (otherPath instanceof Pathway ? otherPath.isAbsolute() : (Array.isArray(otherPath) ? otherPath[0].startsWith('/') : otherPath.startsWith('/')))) {
145
+ throw new Error("Cannot relativize between absolute and relative paths");
146
+ }
147
+ let commonLength = 0;
148
+ const maxLength = Math.min(this.segments.length, otherSegments.length);
149
+ for (let i = 0; i < maxLength; i++) {
150
+ if (this.segments[i] !== otherSegments[i]) {
151
+ break;
152
+ }
153
+ commonLength++;
154
+ }
155
+ const upSegments = this.segments.slice(commonLength).map(() => '..');
156
+ const downSegments = otherSegments.slice(commonLength);
157
+ return new Pathway(upSegments.concat(downSegments));
158
+ }
159
+ /**
160
+ * Checks if the given path is a descendant of the current pathway.
161
+ * @param otherPath - The other pathway to check.
162
+ * @returns True if the given path is a descendant, false otherwise.
163
+ */
164
+ isDescendant(otherPath) {
165
+ const otherSegments = otherPath instanceof Pathway ? otherPath.getSegments() : this.normalizePath(otherPath);
166
+ if (otherSegments.length < this.segments.length) {
167
+ return false;
168
+ }
169
+ for (let i = 0; i < this.segments.length; i++) {
170
+ if (this.segments[i] !== otherSegments[i]) {
171
+ return false;
172
+ }
173
+ }
174
+ return true;
175
+ }
176
+ static isRelativePath(path) {
177
+ const pathway = typeof path === "object" && !Array.isArray(path) ? path : Pathway.of(path);
178
+ return pathway.isRelative();
179
+ }
180
+ static asString(path) {
181
+ if (typeof path === "string") {
182
+ return path;
183
+ }
184
+ else {
185
+ const pathway = typeof path === "object" && !Array.isArray(path) ? path : Pathway.of(path);
186
+ return pathway.toString();
187
+ }
188
+ }
189
+ static getName(path) {
190
+ let name;
191
+ switch (typeof path) {
192
+ case "string":
193
+ {
194
+ name = path;
195
+ }
196
+ break;
197
+ case "object":
198
+ {
199
+ const segments = Array.isArray(path) ? path : path.getSegments();
200
+ name = segments[segments.length - 1];
201
+ }
202
+ break;
203
+ }
204
+ return name;
205
+ }
206
+ }
@@ -0,0 +1,22 @@
1
+ export class SimpleRegistry {
2
+ constructor() {
3
+ this.records = new Map();
4
+ }
5
+ resolve(namespace, prefix, variableName) {
6
+ if (prefix !== undefined) {
7
+ throw new Error(`Not support namespace prefix for name resolution : '${prefix}'`);
8
+ }
9
+ else {
10
+ const record = this.records.get(namespace);
11
+ return record === null || record === void 0 ? void 0 : record[variableName];
12
+ }
13
+ }
14
+ register(namespace, variableName, t) {
15
+ let namespaceRecord = this.records.get(namespace);
16
+ if (namespaceRecord === undefined) {
17
+ namespaceRecord = {};
18
+ this.records.set(namespace, namespaceRecord);
19
+ }
20
+ namespaceRecord[variableName] = t;
21
+ }
22
+ }
@@ -0,0 +1,68 @@
1
+ export class RecordResolver {
2
+ constructor(variables, parent) {
3
+ this.variables = variables;
4
+ this.parent = parent;
5
+ }
6
+ resolve(namespace, prefix, variableName) {
7
+ var _a, _b;
8
+ return (_a = this.variables[variableName]) !== null && _a !== void 0 ? _a : (_b = this.parent) === null || _b === void 0 ? void 0 : _b.resolve(undefined, undefined, variableName);
9
+ }
10
+ }
11
+ export class QualifiedNameResolverImpl {
12
+ constructor(ignoreNs = false) {
13
+ this.ignoreNs = ignoreNs;
14
+ this.entries = new Map();
15
+ }
16
+ resolve(namespace, prefix, name) {
17
+ var _a;
18
+ if (!this.ignoreNs && namespace !== undefined && namespace !== null) {
19
+ throw new Error(`Not support namespace URI but passed as parameter: ${namespace}`);
20
+ }
21
+ return (_a = this.entries.get(namespace)) === null || _a === void 0 ? void 0 : _a.get(name);
22
+ }
23
+ setEntries(prefix, entries) {
24
+ const prefixEntry = this.getOrCreatePrefixEntry(prefix);
25
+ Object.entries(entries).forEach(([k, v]) => prefixEntry.set(k, v));
26
+ }
27
+ setEntry(prefix, name, entry) {
28
+ const prefixEntry = this.getOrCreatePrefixEntry(prefix);
29
+ prefixEntry.set(name, entry);
30
+ }
31
+ getOrCreatePrefixEntry(prefix) {
32
+ let prefixEntry = this.entries.get(prefix);
33
+ if (prefixEntry === undefined) {
34
+ prefixEntry = new Map();
35
+ this.entries.set(prefix, prefixEntry);
36
+ }
37
+ return prefixEntry;
38
+ }
39
+ }
40
+ export class SimpleResolver {
41
+ constructor(parent, namespace, variableName, variableValue) {
42
+ this.parent = parent;
43
+ this.namespace = namespace;
44
+ this.variableName = variableName;
45
+ this.variableValue = variableValue;
46
+ }
47
+ resolve(namespace, prefix, variableName) {
48
+ var _a;
49
+ if (this.namespace === namespace && this.variableName === variableName) {
50
+ return this.variableValue;
51
+ }
52
+ return (_a = this.parent) === null || _a === void 0 ? void 0 : _a.resolve(namespace, prefix, variableName);
53
+ }
54
+ }
55
+ export class ChainedResolver {
56
+ constructor(resolvers) {
57
+ this.resolvers = resolvers;
58
+ }
59
+ resolve(namespace, prefix, variableName) {
60
+ for (const resolver of this.resolvers) {
61
+ const s = resolver.resolve(namespace, prefix, variableName);
62
+ if (s !== undefined) {
63
+ return s;
64
+ }
65
+ }
66
+ return undefined;
67
+ }
68
+ }