@hamak/navigation-utils 0.4.0

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 (49) hide show
  1. package/README.md +82 -0
  2. package/dist/es2015/filesystem/fs-node-abstract.types.js +1 -0
  3. package/dist/es2015/index.js +9 -0
  4. package/dist/es2015/itinerary/hyper-layer-node.js +21 -0
  5. package/dist/es2015/itinerary/itinerary.js +276 -0
  6. package/dist/es2015/itinerary/itinerary.spec.js +296 -0
  7. package/dist/es2015/itinerary/stack.js +47 -0
  8. package/dist/es2015/itinerary/stack.spec.js +35 -0
  9. package/dist/es2015/path/pathway-resolver.js +31 -0
  10. package/dist/es2015/path/pathway.js +206 -0
  11. package/dist/filesystem/fs-node-abstract.types.d.ts +34 -0
  12. package/dist/filesystem/fs-node-abstract.types.d.ts.map +1 -0
  13. package/dist/filesystem/fs-node-abstract.types.js +1 -0
  14. package/dist/index.d.ts +7 -0
  15. package/dist/index.d.ts.map +1 -0
  16. package/dist/index.js +9 -0
  17. package/dist/itinerary/hyper-layer-node.d.ts +12 -0
  18. package/dist/itinerary/hyper-layer-node.d.ts.map +1 -0
  19. package/dist/itinerary/hyper-layer-node.js +21 -0
  20. package/dist/itinerary/itinerary.d.ts +86 -0
  21. package/dist/itinerary/itinerary.d.ts.map +1 -0
  22. package/dist/itinerary/itinerary.js +275 -0
  23. package/dist/itinerary/itinerary.spec.d.ts +2 -0
  24. package/dist/itinerary/itinerary.spec.d.ts.map +1 -0
  25. package/dist/itinerary/itinerary.spec.js +294 -0
  26. package/dist/itinerary/stack.d.ts +20 -0
  27. package/dist/itinerary/stack.d.ts.map +1 -0
  28. package/dist/itinerary/stack.js +47 -0
  29. package/dist/itinerary/stack.spec.d.ts +2 -0
  30. package/dist/itinerary/stack.spec.d.ts.map +1 -0
  31. package/dist/itinerary/stack.spec.js +34 -0
  32. package/dist/path/pathway-resolver.d.ts +20 -0
  33. package/dist/path/pathway-resolver.d.ts.map +1 -0
  34. package/dist/path/pathway-resolver.js +31 -0
  35. package/dist/path/pathway.d.ts +84 -0
  36. package/dist/path/pathway.d.ts.map +1 -0
  37. package/dist/path/pathway.js +206 -0
  38. package/package.json +41 -0
  39. package/src/filesystem/fs-node-abstract.types.ts +34 -0
  40. package/src/index.ts +11 -0
  41. package/src/itinerary/hyper-layer-node.ts +25 -0
  42. package/src/itinerary/itinerary.spec.ts +388 -0
  43. package/src/itinerary/itinerary.ts +363 -0
  44. package/src/itinerary/stack.spec.ts +46 -0
  45. package/src/itinerary/stack.ts +62 -0
  46. package/src/path/pathway-resolver.ts +36 -0
  47. package/src/path/pathway.ts +232 -0
  48. package/tsconfig.es2015.json +23 -0
  49. package/tsconfig.json +19 -0
@@ -0,0 +1,275 @@
1
+ import stack from "./stack";
2
+ export function constructiveItinerary(steps) {
3
+ const segments = [];
4
+ const prototypes = [];
5
+ for (let i = 0; i < steps.length; i += 2) {
6
+ const segment = steps[i];
7
+ const prototype = steps[i + 1];
8
+ switch (typeof segment) {
9
+ case "string":
10
+ case "number":
11
+ {
12
+ const step = typeof segment === "number" ? positionStep(segment) : propertyStep(segment);
13
+ switch (typeof prototype) {
14
+ case "object":
15
+ { // including Array
16
+ segments.push(segment);
17
+ prototypes.push(prototype);
18
+ }
19
+ break;
20
+ default: {
21
+ throw new Error(`Expecting array or object but got : '${typeof prototype}'`);
22
+ }
23
+ }
24
+ }
25
+ break;
26
+ default: {
27
+ throw new Error(`Expecting string or number but gor : '${typeof segment}'`);
28
+ }
29
+ }
30
+ }
31
+ return {
32
+ itinerary: itineraryOf(...segments),
33
+ prototypes: stack.fromArray(prototypes)
34
+ };
35
+ }
36
+ export function areSameItineraryStep(step1, step2) {
37
+ if (step1 == undefined || step2 == undefined) {
38
+ return false;
39
+ }
40
+ else if (step1.type !== step2.type) {
41
+ return false;
42
+ }
43
+ else {
44
+ switch (step1.type) {
45
+ case "property": return step2.type === "property" && step1.propertyName === step2.propertyName;
46
+ case "position": return step2.type === "position" && step1.position === step2.position;
47
+ case "lookup": {
48
+ return step2.type === "lookup"
49
+ && step1.keys?.length === step2.keys?.length
50
+ && step1.keys?.every(pv1 => step2.keys.some(pv2 => areSamePropertyValuePair(pv1, pv2)));
51
+ }
52
+ }
53
+ }
54
+ }
55
+ function areSamePropertyValuePair(p1, p2) {
56
+ return p1.propertyName === p2.propertyName
57
+ && p1.propertyValue === p2.propertyValue;
58
+ }
59
+ export function itineraryOf(...args) {
60
+ return args.reduce((parent, e) => {
61
+ const value = typeof e === 'string' ? propertyStep(e) : positionStep(e);
62
+ return { parent, value };
63
+ }, undefined);
64
+ }
65
+ const dataStep = propertyStep("data");
66
+ const childrenStep = propertyStep("children");
67
+ export function itineraryOverlay(it) {
68
+ return { value: dataStep, parent: itineraryOverlayInner(it) };
69
+ }
70
+ function itineraryOverlayInner(it) {
71
+ if (it === undefined) {
72
+ return undefined;
73
+ }
74
+ else {
75
+ const { value, parent } = it;
76
+ return { value, parent: { value: childrenStep, parent: itineraryOverlayInner(parent) } };
77
+ }
78
+ }
79
+ export function itineraryToStepArray(itinerary) {
80
+ let itn = itinerary;
81
+ const result = [];
82
+ while (itn !== undefined) {
83
+ result.push(itn.value);
84
+ itn = itn.parent;
85
+ }
86
+ return result.reverse();
87
+ }
88
+ export function propertyStep(propertyName) {
89
+ return { type: "property", propertyName };
90
+ }
91
+ export function positionStep(position) {
92
+ return { type: "position", position };
93
+ }
94
+ export function lookupStep(criteria) {
95
+ return { type: "lookup", keys: Object.entries(criteria).map(([k, v]) => ({ propertyName: k, propertyValue: v })) };
96
+ }
97
+ export function xpathFromStack(path) {
98
+ if (path === undefined) {
99
+ return ''; // TODO may need to return '.' here, to be checked
100
+ }
101
+ else {
102
+ const { value, parent } = path;
103
+ const parentXPath = xpathFromStack(parent);
104
+ switch (value.type) {
105
+ case 'property': {
106
+ return `${parentXPath}/${value.propertyName}`;
107
+ }
108
+ case 'position': {
109
+ return `${parentXPath}/*[${value.position + 1}]`;
110
+ }
111
+ case 'lookup': {
112
+ const predicate = value.keys.map(({ propertyName, propertyValue }) => `${propertyName}=$${propertyName}`).join(' and ');
113
+ return `${parentXPath}/*[${predicate}]`;
114
+ }
115
+ }
116
+ }
117
+ }
118
+ export function navigate(from, itinerary, prototype) {
119
+ if (from === undefined) {
120
+ return undefined;
121
+ }
122
+ if (itinerary === undefined) {
123
+ return from;
124
+ }
125
+ const { parent, value: step } = itinerary;
126
+ const current = navigate(from, parent, prototype?.parent);
127
+ return navigateStep(current, step, prototype?.value);
128
+ }
129
+ export function navigateStep(from, step, prototype) {
130
+ if (from === undefined) {
131
+ return undefined;
132
+ }
133
+ if (step === undefined) {
134
+ return from;
135
+ }
136
+ if (Array.isArray(from)) {
137
+ if (step.type === 'lookup') {
138
+ const stp = step;
139
+ const result = from.find(e => e !== undefined && stp.keys.every(({ propertyName, propertyValue }) => e[propertyName] === propertyValue));
140
+ if (result === undefined && prototype !== undefined) {
141
+ from.push(prototype);
142
+ return prototype;
143
+ }
144
+ else {
145
+ return result;
146
+ }
147
+ }
148
+ else if (step.type === 'position') {
149
+ const result = from[step.position];
150
+ if (result === undefined && prototype !== undefined) {
151
+ from[step.position] = prototype;
152
+ return prototype;
153
+ }
154
+ else {
155
+ return result;
156
+ }
157
+ }
158
+ }
159
+ else if (typeof from === 'object') {
160
+ if (step.type === 'property') {
161
+ const result = from === null ? undefined : from[step.propertyName];
162
+ if (result === undefined && prototype !== undefined && from !== null && from !== undefined) {
163
+ from[step.propertyName] = prototype;
164
+ return prototype;
165
+ }
166
+ else {
167
+ return result;
168
+ }
169
+ }
170
+ }
171
+ return undefined;
172
+ }
173
+ export function navigationDepth(from, itinerary, originalPath = xpathFromStack(itinerary)) {
174
+ if (from === undefined || from === null) {
175
+ return { value: undefined, path: '', originalPath };
176
+ }
177
+ if (itinerary === undefined) {
178
+ return { value: from, path: '', originalPath };
179
+ }
180
+ const { parent, value: step } = itinerary;
181
+ const current = navigationDepth(from, parent, originalPath);
182
+ if (current.value === undefined || current.value === null) {
183
+ return current;
184
+ }
185
+ const next = navigateStep(current.value, step);
186
+ if (next === undefined || next === null) {
187
+ return { value: current.value, path: xpathFromStack(parent), originalPath };
188
+ }
189
+ else {
190
+ return { value: next, path: xpathFromStack(itinerary), originalPath };
191
+ }
192
+ }
193
+ class OverlayNavigator {
194
+ navigate(from, itinerary) {
195
+ if (from === undefined) {
196
+ return undefined;
197
+ }
198
+ if (itinerary === undefined) {
199
+ return from;
200
+ }
201
+ const { parent, value: step } = itinerary;
202
+ const current = this.navigate(from, parent);
203
+ return current === undefined ? undefined : this.navigateStep(current, step);
204
+ }
205
+ navigateStep(from, step) {
206
+ if (from === undefined) {
207
+ return undefined;
208
+ }
209
+ if (step === undefined) {
210
+ return from;
211
+ }
212
+ if (Array.isArray(from.children)) {
213
+ if (step.type === 'lookup') {
214
+ const stp = step;
215
+ const result = from.children.find(e => e !== undefined && stp.keys.every(({ propertyName, propertyValue }) => e.keys?.[propertyName] === propertyValue));
216
+ return result;
217
+ }
218
+ else if (step.type === 'position') {
219
+ const result = from.children[step.position];
220
+ return result;
221
+ }
222
+ }
223
+ else if (from.children !== null && typeof from.children === 'object') {
224
+ if (step.type === 'property') {
225
+ const result = from === null ? undefined : from.children[step.propertyName];
226
+ return result;
227
+ }
228
+ }
229
+ return undefined;
230
+ }
231
+ }
232
+ export const overlayNavigator = new OverlayNavigator();
233
+ /**
234
+ * Same as {navigate} but try to path missing data node using prototype parameter
235
+ * @param from
236
+ * @param itinerary
237
+ * @param prototype
238
+ */
239
+ export function ensurePath(from, itinerary, prototype) {
240
+ return navigate(from, itinerary, prototype);
241
+ }
242
+ class AbstractPointerBuilder {
243
+ constructor() {
244
+ this.itinerary = [];
245
+ }
246
+ child(propertyName) {
247
+ this.itinerary.push({ type: "property", propertyName });
248
+ return this;
249
+ }
250
+ lookup(...criterion) {
251
+ criterion.forEach(([propertyName, propertyValue]) => {
252
+ this.itinerary.push({ type: "lookup", keys: [{ propertyName, propertyValue }] });
253
+ });
254
+ }
255
+ }
256
+ class RelativePointerBuilder extends AbstractPointerBuilder {
257
+ constructor(originUuid) {
258
+ super();
259
+ this.originUuid = originUuid;
260
+ this.type = "relative";
261
+ }
262
+ }
263
+ class AbsolutePointerBuilder extends AbstractPointerBuilder {
264
+ constructor() {
265
+ super();
266
+ this.type = "absolute";
267
+ }
268
+ }
269
+ export function pointer(fromId) {
270
+ if (fromId === undefined) {
271
+ return new AbsolutePointerBuilder();
272
+ }
273
+ return new RelativePointerBuilder(fromId);
274
+ }
275
+ export const ROOT = Object.freeze({ type: "absolute", itinerary: [] });
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=itinerary.spec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"itinerary.spec.d.ts","sourceRoot":"","sources":["../../src/itinerary/itinerary.spec.ts"],"names":[],"mappings":""}
@@ -0,0 +1,294 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { areSameItineraryStep, constructiveItinerary, itineraryOf, itineraryOverlay, itineraryToStepArray, navigate, navigationDepth, overlayNavigator, positionStep, propertyStep, xpathFromStack } from './itinerary';
3
+ import stack from './stack';
4
+ import { hyperReflect } from './hyper-layer-node';
5
+ describe('Navigate itinerary', () => {
6
+ it('should work', () => {
7
+ const itinerary = {
8
+ "value": {
9
+ "type": "property",
10
+ "propertyName": "schema"
11
+ },
12
+ "parent": {
13
+ "value": {
14
+ "type": "position",
15
+ "position": 0
16
+ }
17
+ }
18
+ };
19
+ const actual = navigate(sampleData(), itinerary);
20
+ const expected = schemaEntry1.schema;
21
+ expect(actual).toBe(expected);
22
+ });
23
+ it('Conversion to array should work', () => {
24
+ const path = [0, 'schema'];
25
+ const itinerary = itineraryOf(...path);
26
+ const actual = itineraryToStepArray(itinerary);
27
+ const expected = path.map(e => typeof e === 'string' ? propertyStep(e) : positionStep(e));
28
+ expect(actual).toStrictEqual(expected);
29
+ });
30
+ it('Ensure path should work', () => {
31
+ const person = [{
32
+ name: "Elon MUSK",
33
+ }];
34
+ const path = [0, 'address'];
35
+ const itinerary = itineraryOf(...path);
36
+ const proto = { street: "Wall Street" };
37
+ const result = navigate(person, itinerary, stack.fromArray([proto]));
38
+ expect(result).toBe(proto);
39
+ expect(person[0].address).toBe(proto);
40
+ });
41
+ it('Deep navigation with constructiveItinerary should work', () => {
42
+ const person = [{
43
+ name: "Guezo",
44
+ children: []
45
+ }];
46
+ const { itinerary, prototypes } = constructiveItinerary(["children", [], 0, { name: "Glélé" }, "children", [], 0, { name: "Béhanzin" }]);
47
+ const result = navigate(person, stack.concat(itineraryOf(0), itinerary), prototypes);
48
+ expect(person[0]?.name).toBe("Guezo");
49
+ expect(result.name).toBe("Béhanzin");
50
+ });
51
+ it("Overlay navigation should work", () => {
52
+ const person = {
53
+ name: "John Doe",
54
+ age: 40,
55
+ address: {
56
+ street: "123 Main Street",
57
+ lines: ["Apt 4B", "2nd Floor"],
58
+ country: "USA"
59
+ },
60
+ children: [
61
+ {
62
+ name: "Jane Doe",
63
+ age: 10,
64
+ address: {
65
+ street: "456 Maple Avenue",
66
+ lines: ["House 12"],
67
+ country: "USA"
68
+ }
69
+ },
70
+ {
71
+ name: "Jack Doe",
72
+ age: 8
73
+ }
74
+ ]
75
+ };
76
+ const personOverlay = hyperReflect(person);
77
+ const itinerary = itineraryOf("children", 0, "address", "lines", 0);
78
+ expect(navigate(person, itinerary)).toBe("House 12");
79
+ expect(navigate(personOverlay, itineraryOverlay(itinerary))).toBe("House 12");
80
+ expect(overlayNavigator.navigate(personOverlay, itinerary)?.data).toBe("House 12");
81
+ });
82
+ it("Overlay itinerary rewrite should work", () => {
83
+ const toOverlay = (...path) => {
84
+ return xpathFromStack(itineraryOverlay(itineraryOf(...path)));
85
+ };
86
+ expect(toOverlay()).toBe("/data");
87
+ expect(toOverlay("name")).toBe("/children/name/data");
88
+ expect(toOverlay("persons", 0)).toBe("/children/persons/children/*[1]/data");
89
+ });
90
+ });
91
+ const schemaEntry1 = {
92
+ "id": "schema:User",
93
+ "schema": {
94
+ "schemaNodeType": "object",
95
+ "operator": false,
96
+ "facets": {},
97
+ "properties": [
98
+ {
99
+ "uuid": "172302fe-29de-462a-9e27-053463b6d118",
100
+ "schemaNodeType": "property",
101
+ "facets": {},
102
+ "name": "uuid",
103
+ "valueSchema": {
104
+ "schemaNodeType": "scalar",
105
+ "operator": false,
106
+ "facets": {},
107
+ "type": "string"
108
+ }
109
+ },
110
+ {
111
+ "uuid": "733675e6-543e-4013-a071-a9a45145321b",
112
+ "schemaNodeType": "property",
113
+ "facets": {},
114
+ "name": "name",
115
+ "valueSchema": {
116
+ "schemaNodeType": "scalar",
117
+ "operator": false,
118
+ "facets": {},
119
+ "type": "string"
120
+ }
121
+ },
122
+ {
123
+ "uuid": "8376b829-459a-4b40-b2f9-71a6aea506e5",
124
+ "schemaNodeType": "property",
125
+ "facets": {},
126
+ "name": "firstName",
127
+ "valueSchema": {
128
+ "schemaNodeType": "scalar",
129
+ "operator": false,
130
+ "facets": {},
131
+ "type": "string"
132
+ }
133
+ },
134
+ {
135
+ "uuid": "3c39bfc4-90ae-4525-9366-47c7866265f3",
136
+ "schemaNodeType": "property",
137
+ "facets": {},
138
+ "name": "lastName",
139
+ "valueSchema": {
140
+ "schemaNodeType": "scalar",
141
+ "operator": false,
142
+ "facets": {},
143
+ "type": "string"
144
+ }
145
+ },
146
+ {
147
+ "uuid": "a5f05908-8a9f-4879-8dc9-f5214a6aa693",
148
+ "schemaNodeType": "property",
149
+ "facets": {},
150
+ "name": "age",
151
+ "valueSchema": {
152
+ "schemaNodeType": "scalar",
153
+ "operator": false,
154
+ "facets": {},
155
+ "type": "integer"
156
+ }
157
+ }
158
+ ]
159
+ }
160
+ };
161
+ function sampleData() {
162
+ return [
163
+ schemaEntry1,
164
+ {
165
+ "id": "schema:Car",
166
+ "schema": {
167
+ "schemaNodeType": "object",
168
+ "operator": false,
169
+ "facets": {},
170
+ "properties": [
171
+ {
172
+ "uuid": "50fa33fe-03b4-45f2-b5df-da00e742bb89",
173
+ "schemaNodeType": "property",
174
+ "facets": {},
175
+ "name": "uuid",
176
+ "valueSchema": {
177
+ "schemaNodeType": "scalar",
178
+ "operator": false,
179
+ "facets": {},
180
+ "type": "string"
181
+ }
182
+ },
183
+ {
184
+ "uuid": "6ff05784-dde5-4a70-a57b-d93cba8afb00",
185
+ "schemaNodeType": "property",
186
+ "facets": {},
187
+ "name": "name",
188
+ "valueSchema": {
189
+ "schemaNodeType": "scalar",
190
+ "operator": false,
191
+ "facets": {},
192
+ "type": "string"
193
+ }
194
+ },
195
+ {
196
+ "uuid": "a8dae1ce-a82f-46f4-a2a0-4fc177988e3c",
197
+ "schemaNodeType": "property",
198
+ "facets": {},
199
+ "name": "brand",
200
+ "valueSchema": {
201
+ "schemaNodeType": "scalar",
202
+ "operator": false,
203
+ "facets": {},
204
+ "type": "string"
205
+ }
206
+ },
207
+ {
208
+ "uuid": "85342429-673f-420e-b0d0-7e019e7bea2a",
209
+ "schemaNodeType": "property",
210
+ "facets": {},
211
+ "name": "color",
212
+ "valueSchema": {
213
+ "schemaNodeType": "scalar",
214
+ "operator": false,
215
+ "facets": {},
216
+ "type": "string"
217
+ }
218
+ }
219
+ ]
220
+ }
221
+ }
222
+ ];
223
+ }
224
+ // Unit tests for areSameItineraryStep
225
+ describe('areSameItineraryStep', () => {
226
+ it('returns true for two position steps with the same position', () => {
227
+ expect(areSameItineraryStep(positionStep(3), positionStep(3))).toBe(true);
228
+ });
229
+ it('returns false for two position steps with different positions', () => {
230
+ expect(areSameItineraryStep(positionStep(2), positionStep(5))).toBe(false);
231
+ });
232
+ it('returns true for two property steps with the same propertyName', () => {
233
+ expect(areSameItineraryStep(propertyStep('foo'), propertyStep('foo'))).toBe(true);
234
+ });
235
+ it('returns false for two property steps with different propertyNames', () => {
236
+ expect(areSameItineraryStep(propertyStep('foo'), propertyStep('bar'))).toBe(false);
237
+ });
238
+ it('returns false for steps of different types', () => {
239
+ expect(areSameItineraryStep(positionStep(1), propertyStep('1'))).toBe(false);
240
+ });
241
+ it('returns false if one or both steps are null or undefined', () => {
242
+ expect(areSameItineraryStep(positionStep(1), null)).toBe(false);
243
+ expect(areSameItineraryStep(undefined, propertyStep('foo'))).toBe(false);
244
+ expect(areSameItineraryStep(undefined, undefined)).toBe(false);
245
+ });
246
+ });
247
+ describe('navigationDepth with itineraryOf', () => {
248
+ it('should return root when itinerary is undefined', () => {
249
+ const obj = { a: 1 };
250
+ const result = navigationDepth(obj, undefined);
251
+ expect(result.value).toBe(obj);
252
+ expect(result.path).toBe('');
253
+ });
254
+ it('should navigate fully when path exists', () => {
255
+ const obj = { a: { b: { c: 42 } } };
256
+ const itinerary = itineraryOf('a', 'b', 'c');
257
+ const result = navigationDepth(obj, itinerary);
258
+ expect(result.value).toBe(42);
259
+ expect(result.path).toBe('/a/b/c');
260
+ });
261
+ it('should stop at last defined property (null)', () => {
262
+ const obj = { a: { b: null } };
263
+ const itinerary = itineraryOf('a', 'b', 'c');
264
+ const result = navigationDepth(obj, itinerary);
265
+ expect(result.value).toEqual({ b: null });
266
+ expect(result.path).toBe('/a/b');
267
+ });
268
+ it('should stop when root is undefined', () => {
269
+ const result = navigationDepth(undefined, itineraryOf('a', 'b'));
270
+ expect(result.value).toBeUndefined();
271
+ expect(result.path).toBe('');
272
+ });
273
+ it('should navigate inside array with position step', () => {
274
+ const obj = { list: [{ id: 1 }, { id: 2 }] };
275
+ const itinerary = itineraryOf('list', 1);
276
+ const result = navigationDepth(obj, itinerary);
277
+ expect(result.value).toEqual({ id: 2 });
278
+ expect(result.path).toBe('/list/*[2]');
279
+ });
280
+ it('should stop when lookup fails', () => {
281
+ const obj = { list: [{ id: 1 }, { id: 2 }] };
282
+ const lookupStep = {
283
+ type: 'lookup',
284
+ keys: [{ propertyName: 'id', propertyValue: 3 }]
285
+ };
286
+ const itinerary = {
287
+ value: lookupStep,
288
+ parent: itineraryOf('list')
289
+ };
290
+ const result = navigationDepth(obj, itinerary);
291
+ expect(result.value).toEqual(obj.list);
292
+ expect(result.path).toBe('/list');
293
+ });
294
+ });
@@ -0,0 +1,20 @@
1
+ export interface StackElement<T> {
2
+ value: T;
3
+ parent?: StackElement<T>;
4
+ }
5
+ export declare function head<T>(value: T): StackElement<T>;
6
+ export declare function push<T>(head: StackElement<T> | undefined, value: T): StackElement<T>;
7
+ export declare function concat<T>(tail: StackElement<T> | undefined, stack: StackElement<T> | undefined): StackElement<T> | undefined;
8
+ export declare function fromArray<T = any>(t: T[] | undefined): StackElement<T> | undefined;
9
+ export declare function reduce<T, R>(head: StackElement<T> | undefined, reducer: (r: R, t: T) => R, initial: R): R;
10
+ declare function equal<T>(a: StackElement<T> | null | undefined, b: StackElement<T> | null | undefined): boolean;
11
+ declare const stack: {
12
+ push: typeof push;
13
+ head: typeof head;
14
+ concat: typeof concat;
15
+ reduce: typeof reduce;
16
+ fromArray: typeof fromArray;
17
+ equal: typeof equal;
18
+ };
19
+ export default stack;
20
+ //# sourceMappingURL=stack.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stack.d.ts","sourceRoot":"","sources":["../../src/itinerary/stack.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,YAAY,CAAC,CAAC;IAC7B,KAAK,EAAG,CAAC,CAAA;IACT,MAAM,CAAC,EAAG,YAAY,CAAC,CAAC,CAAC,CAAA;CAC1B;AAED,wBAAgB,IAAI,CAAC,CAAC,EAAE,KAAK,EAAG,CAAC,GAAI,YAAY,CAAC,CAAC,CAAC,CAEnD;AAED,wBAAgB,IAAI,CAAC,CAAC,EAAE,IAAI,EAAG,YAAY,CAAC,CAAC,CAAC,GAAG,SAAS,EAAE,KAAK,EAAG,CAAC,GAAI,YAAY,CAAC,CAAC,CAAC,CAEvF;AAED,wBAAgB,MAAM,CAAC,CAAC,EAAE,IAAI,EAAG,YAAY,CAAC,CAAC,CAAC,GAAG,SAAS,EAAE,KAAK,EAAG,YAAY,CAAC,CAAC,CAAC,GAAG,SAAS,GAAI,YAAY,CAAC,CAAC,CAAC,GAAG,SAAS,CAO/H;AAED,wBAAgB,SAAS,CAAC,CAAC,GAAG,GAAG,EAAE,CAAC,EAAG,CAAC,EAAE,GAAG,SAAS,GAAI,YAAY,CAAC,CAAC,CAAC,GAAG,SAAS,CAEpF;AAED,wBAAgB,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,IAAI,EAAG,YAAY,CAAC,CAAC,CAAC,GAAG,SAAS,EAAE,OAAO,EAAG,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAG,CAAC,KAAK,CAAC,EAAE,OAAO,EAAG,CAAC,GAAI,CAAC,CAQ9G;AAED,iBAAS,KAAK,CAAC,CAAC,EAAE,CAAC,EAAC,YAAY,CAAC,CAAC,CAAC,GAAG,IAAI,GAAG,SAAS,EAAE,CAAC,EAAG,YAAY,CAAC,CAAC,CAAC,GAAG,IAAI,GAAG,SAAS,GAAI,OAAO,CAqBxG;AAED,QAAA,MAAM,KAAK;;;;;;;CAAiD,CAAA;AAE5D,eAAe,KAAK,CAAC"}
@@ -0,0 +1,47 @@
1
+ export function head(value) {
2
+ return { value };
3
+ }
4
+ export function push(head, value) {
5
+ return { value, parent: head };
6
+ }
7
+ export function concat(tail, stack) {
8
+ if (tail === undefined)
9
+ return stack;
10
+ if (stack === undefined)
11
+ return tail;
12
+ const { parent, value } = stack;
13
+ return { value, parent: concat(tail, parent) };
14
+ }
15
+ export function fromArray(t) {
16
+ return t?.reduce((acc, t) => push(acc, t), undefined);
17
+ }
18
+ export function reduce(head, reducer, initial) {
19
+ if (head === undefined) {
20
+ return initial;
21
+ }
22
+ const { parent, value } = head;
23
+ const acc = reduce(parent, reducer, initial);
24
+ return reducer(acc, value);
25
+ }
26
+ function equal(a, b) {
27
+ // Not equal if both are nullish
28
+ if (a === undefined || a === null || b === undefined || b === null) {
29
+ return false;
30
+ }
31
+ while (a !== undefined && b !== undefined) {
32
+ if (a.value !== b.value) {
33
+ return false;
34
+ }
35
+ a = a.parent;
36
+ b = b.parent;
37
+ }
38
+ // Equal if traversed all stack at both side
39
+ if ((a === undefined || a === null) && (b === undefined || b === null)) {
40
+ return true;
41
+ }
42
+ else {
43
+ return false;
44
+ }
45
+ }
46
+ const stack = { push, head, concat, reduce, fromArray, equal };
47
+ export default stack;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=stack.spec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stack.spec.d.ts","sourceRoot":"","sources":["../../src/itinerary/stack.spec.ts"],"names":[],"mappings":""}