@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.
- package/README.md +82 -0
- package/dist/es2015/filesystem/fs-node-abstract.types.js +1 -0
- package/dist/es2015/index.js +9 -0
- package/dist/es2015/itinerary/hyper-layer-node.js +21 -0
- package/dist/es2015/itinerary/itinerary.js +276 -0
- package/dist/es2015/itinerary/itinerary.spec.js +296 -0
- package/dist/es2015/itinerary/stack.js +47 -0
- package/dist/es2015/itinerary/stack.spec.js +35 -0
- package/dist/es2015/path/pathway-resolver.js +31 -0
- package/dist/es2015/path/pathway.js +206 -0
- package/dist/filesystem/fs-node-abstract.types.d.ts +34 -0
- package/dist/filesystem/fs-node-abstract.types.d.ts.map +1 -0
- package/dist/filesystem/fs-node-abstract.types.js +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -0
- package/dist/itinerary/hyper-layer-node.d.ts +12 -0
- package/dist/itinerary/hyper-layer-node.d.ts.map +1 -0
- package/dist/itinerary/hyper-layer-node.js +21 -0
- package/dist/itinerary/itinerary.d.ts +86 -0
- package/dist/itinerary/itinerary.d.ts.map +1 -0
- package/dist/itinerary/itinerary.js +275 -0
- package/dist/itinerary/itinerary.spec.d.ts +2 -0
- package/dist/itinerary/itinerary.spec.d.ts.map +1 -0
- package/dist/itinerary/itinerary.spec.js +294 -0
- package/dist/itinerary/stack.d.ts +20 -0
- package/dist/itinerary/stack.d.ts.map +1 -0
- package/dist/itinerary/stack.js +47 -0
- package/dist/itinerary/stack.spec.d.ts +2 -0
- package/dist/itinerary/stack.spec.d.ts.map +1 -0
- package/dist/itinerary/stack.spec.js +34 -0
- package/dist/path/pathway-resolver.d.ts +20 -0
- package/dist/path/pathway-resolver.d.ts.map +1 -0
- package/dist/path/pathway-resolver.js +31 -0
- package/dist/path/pathway.d.ts +84 -0
- package/dist/path/pathway.d.ts.map +1 -0
- package/dist/path/pathway.js +206 -0
- package/package.json +41 -0
- package/src/filesystem/fs-node-abstract.types.ts +34 -0
- package/src/index.ts +11 -0
- package/src/itinerary/hyper-layer-node.ts +25 -0
- package/src/itinerary/itinerary.spec.ts +388 -0
- package/src/itinerary/itinerary.ts +363 -0
- package/src/itinerary/stack.spec.ts +46 -0
- package/src/itinerary/stack.ts +62 -0
- package/src/path/pathway-resolver.ts +36 -0
- package/src/path/pathway.ts +232 -0
- package/tsconfig.es2015.json +23 -0
- package/tsconfig.json +19 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import stack from "./stack";
|
|
2
|
+
import { describe, it, expect } from 'vitest';
|
|
3
|
+
describe("Core util stack", () => {
|
|
4
|
+
it("fromArray should work", () => {
|
|
5
|
+
const itinerary = stack.fromArray([1, 2, 3, 4]);
|
|
6
|
+
expect(itinerary?.value).toBe(4);
|
|
7
|
+
expect(itinerary?.parent?.parent?.parent?.value).toBe(1);
|
|
8
|
+
expect(stack.fromArray([])).toBe(undefined);
|
|
9
|
+
expect(stack.fromArray(undefined)).toBe(undefined);
|
|
10
|
+
});
|
|
11
|
+
it("reduce should work", () => {
|
|
12
|
+
const itinerary = stack.fromArray([1, 2, 3, 4]);
|
|
13
|
+
const result = stack.reduce(itinerary, (acc, t) => acc + t, 0);
|
|
14
|
+
expect(result).toBe(10);
|
|
15
|
+
});
|
|
16
|
+
it("reduce order should work", () => {
|
|
17
|
+
const itinerary = stack.fromArray([1, 2, 3, 4]);
|
|
18
|
+
const result = stack.reduce(itinerary, (acc, t) => [...acc, t], []);
|
|
19
|
+
expect(result).toStrictEqual([1, 2, 3, 4]);
|
|
20
|
+
});
|
|
21
|
+
it("equal should work", () => {
|
|
22
|
+
expect(stack.equal(undefined, undefined)).toBe(false);
|
|
23
|
+
expect(stack.equal(null, null)).toBe(false);
|
|
24
|
+
expect(stack.equal(undefined, null)).toBe(false);
|
|
25
|
+
expect(stack.equal(null, undefined)).toBe(false);
|
|
26
|
+
expect(stack.equal(stack.fromArray([1, 2, 3, 4]), undefined)).toBe(false);
|
|
27
|
+
expect(stack.equal(stack.fromArray([1, 2, 3, 4]), null)).toBe(false);
|
|
28
|
+
expect(stack.equal(undefined, stack.fromArray([1, 2, 3, 4]))).toBe(false);
|
|
29
|
+
expect(stack.equal(null, stack.fromArray([1, 2, 3, 4]))).toBe(false);
|
|
30
|
+
expect(stack.equal(stack.fromArray([1]), stack.fromArray([2]))).toBe(false);
|
|
31
|
+
expect(stack.equal(stack.fromArray([1]), stack.fromArray([1, 2]))).toBe(false);
|
|
32
|
+
expect(stack.equal(stack.fromArray([1, 2, 3, 4]), stack.fromArray([1, 2, 3, 4]))).toBe(true);
|
|
33
|
+
});
|
|
34
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Path, Pathway } from "./pathway";
|
|
2
|
+
export interface PathwayResolver<T> {
|
|
3
|
+
resolve(path: string | string[] | Pathway): T | undefined;
|
|
4
|
+
}
|
|
5
|
+
export declare class RecordPathwayResolver<T> implements PathwayResolver<T> {
|
|
6
|
+
readonly record: Record<string, T>;
|
|
7
|
+
constructor(record: Record<string, T>);
|
|
8
|
+
resolve(path: string | string[] | Pathway): T | undefined;
|
|
9
|
+
}
|
|
10
|
+
export declare class EmptyPathwayResolver<T> implements PathwayResolver<T> {
|
|
11
|
+
resolve(path: string | string[] | Pathway): T | undefined;
|
|
12
|
+
}
|
|
13
|
+
export declare class RelativePathwayResolver<T> implements PathwayResolver<T> {
|
|
14
|
+
readonly parentResolver: PathwayResolver<T>;
|
|
15
|
+
readonly path: Path;
|
|
16
|
+
private relativePathway;
|
|
17
|
+
constructor(parentResolver: PathwayResolver<T>, path: Path);
|
|
18
|
+
resolve(path: string | string[] | Pathway): T | undefined;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=pathway-resolver.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pathway-resolver.d.ts","sourceRoot":"","sources":["../../src/path/pathway-resolver.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,IAAI,EAAE,OAAO,EAAC,MAAM,WAAW,CAAC;AAExC,MAAM,WAAW,eAAe,CAAC,CAAC;IAChC,OAAO,CAAC,IAAI,EAAG,MAAM,GAAG,MAAM,EAAE,GAAG,OAAO,GAAI,CAAC,GAAG,SAAS,CAAA;CAC5D;AAED,qBAAa,qBAAqB,CAAC,CAAC,CAAE,YAAW,eAAe,CAAC,CAAC,CAAC;IACrD,QAAQ,CAAC,MAAM,EAAG,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC;gBAA1B,MAAM,EAAG,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC;IAG/C,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,OAAO,GAAG,CAAC,GAAG,SAAS;CAI1D;AAED,qBAAa,oBAAoB,CAAC,CAAC,CAAE,YAAW,eAAe,CAAC,CAAC,CAAC;IAChE,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,OAAO,GAAG,CAAC,GAAG,SAAS;CAG1D;AAED,qBAAa,uBAAuB,CAAC,CAAC,CAAE,YAAW,eAAe,CAAC,CAAC,CAAC;IAEvD,QAAQ,CAAC,cAAc,EAAG,eAAe,CAAC,CAAC,CAAC;IAAE,QAAQ,CAAC,IAAI,EAAG,IAAI;IAD9E,OAAO,CAAC,eAAe,CAAU;gBACZ,cAAc,EAAG,eAAe,CAAC,CAAC,CAAC,EAAW,IAAI,EAAG,IAAI;IAG9E,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,OAAO,GAAG,CAAC,GAAG,SAAS;CAQ1D"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Pathway } from "./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,84 @@
|
|
|
1
|
+
export type Path = string | string[] | Pathway;
|
|
2
|
+
/**
|
|
3
|
+
* Class representing a normalized pathway.
|
|
4
|
+
*/
|
|
5
|
+
export declare class Pathway {
|
|
6
|
+
private segments;
|
|
7
|
+
private isAbsolutePath;
|
|
8
|
+
/**
|
|
9
|
+
* Creates a Pathway instance.
|
|
10
|
+
* @param path - The pathway to normalize, either as a string or an array of strings.
|
|
11
|
+
* An empty array or "." represents the current directory.
|
|
12
|
+
*/
|
|
13
|
+
constructor(path: string | string[] | null | undefined);
|
|
14
|
+
/**
|
|
15
|
+
* Factory method to create a Pathway instance.
|
|
16
|
+
* @param path - The pathway to normalize, either as a string or an array of strings.
|
|
17
|
+
* @returns A new Pathway instance.
|
|
18
|
+
*/
|
|
19
|
+
static of(path: string | string[] | null | undefined | Pathway): Pathway;
|
|
20
|
+
/**
|
|
21
|
+
* Normalizes a pathway by converting it into an array of segments.
|
|
22
|
+
* @param path - The pathway to normalize, either as a string or an array of strings.
|
|
23
|
+
* @returns An array of normalized path segments, or an empty array if the input is null or undefined.
|
|
24
|
+
*/
|
|
25
|
+
private normalizePath;
|
|
26
|
+
/**
|
|
27
|
+
* Returns the normalized path segments.
|
|
28
|
+
* @returns An array of normalized path segments.
|
|
29
|
+
*/
|
|
30
|
+
getSegments(): string[];
|
|
31
|
+
isCurrentDir(): boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Converts the normalized path segments back to a string.
|
|
34
|
+
* @returns The normalized path as a string.
|
|
35
|
+
*/
|
|
36
|
+
toString(): string;
|
|
37
|
+
/**
|
|
38
|
+
* Checks if the pathway is absolute.
|
|
39
|
+
* @returns True if the pathway is absolute, false otherwise.
|
|
40
|
+
*/
|
|
41
|
+
isAbsolute(): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Checks if the pathway is relative.
|
|
44
|
+
* @returns True if the pathway is relative, false otherwise.
|
|
45
|
+
*/
|
|
46
|
+
isRelative(): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Resolves the current pathway with another pathway or string.
|
|
49
|
+
* @param otherPath - The other pathway or string to resolve against.
|
|
50
|
+
* @returns A new Pathway instance representing the resolved path.
|
|
51
|
+
*/
|
|
52
|
+
resolve(otherPath: Pathway | string | string[]): Pathway;
|
|
53
|
+
/**
|
|
54
|
+
* Gets the parent directory of the current pathway.
|
|
55
|
+
* @returns A new Pathway instance representing the parent directory.
|
|
56
|
+
*/
|
|
57
|
+
getParent(): Pathway;
|
|
58
|
+
/**
|
|
59
|
+
* Gets the current directory of the pathway.
|
|
60
|
+
* @returns The current directory as a string.
|
|
61
|
+
*/
|
|
62
|
+
getCurrentDirectory(): string;
|
|
63
|
+
/**
|
|
64
|
+
* Static method to get the absolute path of the root.
|
|
65
|
+
* @returns A new Pathway instance representing the root path.
|
|
66
|
+
*/
|
|
67
|
+
static ofRoot(): Pathway;
|
|
68
|
+
/**
|
|
69
|
+
* Determines the relative path from this pathway to another pathway.
|
|
70
|
+
* @param otherPath - The other pathway to relativize against.
|
|
71
|
+
* @returns A new Pathway instance representing the relative path.
|
|
72
|
+
*/
|
|
73
|
+
relativize(otherPath: Pathway | string | string[]): Pathway;
|
|
74
|
+
/**
|
|
75
|
+
* Checks if the given path is a descendant of the current pathway.
|
|
76
|
+
* @param otherPath - The other pathway to check.
|
|
77
|
+
* @returns True if the given path is a descendant, false otherwise.
|
|
78
|
+
*/
|
|
79
|
+
isDescendant(otherPath: Pathway | string | string[]): boolean;
|
|
80
|
+
static isRelativePath(path: Path): boolean;
|
|
81
|
+
static asString(path: Path): string;
|
|
82
|
+
static getName(path: string | string[] | Pathway): string;
|
|
83
|
+
}
|
|
84
|
+
//# sourceMappingURL=pathway.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pathway.d.ts","sourceRoot":"","sources":["../../src/path/pathway.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,IAAI,GAAG,MAAM,GAAG,MAAM,EAAE,GAAG,OAAO,CAAA;AAC9C;;GAEG;AACH,qBAAa,OAAO;IAClB,OAAO,CAAC,QAAQ,CAAW;IAC3B,OAAO,CAAC,cAAc,CAAU;IAEhC;;;;OAIG;gBACS,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,IAAI,GAAG,SAAS;IAUtD;;;;OAIG;WACW,EAAE,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,IAAI,GAAG,SAAS,GAAG,OAAO,GAAG,OAAO;IAQ/E;;;;OAIG;IACH,OAAO,CAAC,aAAa;IAgBrB;;;OAGG;IACI,WAAW,IAAI,MAAM,EAAE;IAIvB,YAAY;IAInB;;;OAGG;IACI,QAAQ,IAAI,MAAM;IAIzB;;;OAGG;IACI,UAAU,IAAI,OAAO;IAI5B;;;OAGG;IACI,UAAU,IAAI,OAAO;IAI5B;;;;OAIG;IACI,OAAO,CAAC,SAAS,EAAE,OAAO,GAAG,MAAM,GAAG,MAAM,EAAE,GAAG,OAAO;IAuB/D;;;OAGG;IACI,SAAS,IAAI,OAAO;IAQ3B;;;OAGG;IACI,mBAAmB,IAAI,MAAM;IAOpC;;;OAGG;WACW,MAAM,IAAI,OAAO;IAI/B;;;;OAIG;IACI,UAAU,CAAC,SAAS,EAAE,OAAO,GAAG,MAAM,GAAG,MAAM,EAAE,GAAG,OAAO;IAsBlE;;;;OAIG;IACI,YAAY,CAAC,SAAS,EAAE,OAAO,GAAG,MAAM,GAAG,MAAM,EAAE,GAAG,OAAO;IAiBpE,MAAM,CAAC,cAAc,CAAC,IAAI,EAAG,IAAI,GAAI,OAAO;IAK5C,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAG,IAAI,GAAI,MAAM;IASrC,MAAM,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,OAAO;CAgBjD"}
|
|
@@ -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
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hamak/navigation-utils",
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"description": "Navigation utilities for path manipulation and data structure navigation",
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"sideEffects": false,
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/amah/app-framework.git",
|
|
13
|
+
"directory": "packages/utils/navigation-utils"
|
|
14
|
+
},
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsc -p tsconfig.json && tsc -p tsconfig.es2015.json",
|
|
20
|
+
"clean": "rm -rf dist",
|
|
21
|
+
"test": "vitest run",
|
|
22
|
+
"test:watch": "vitest"
|
|
23
|
+
},
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"import": "./dist/index.js",
|
|
28
|
+
"default": "./dist/index.js",
|
|
29
|
+
"legacy": "./dist/es2015/index.js"
|
|
30
|
+
},
|
|
31
|
+
"./es2015": {
|
|
32
|
+
"import": "./dist/es2015/index.js",
|
|
33
|
+
"default": "./dist/es2015/index.js"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"typescript": "~5.4.0",
|
|
39
|
+
"vitest": "^2.0.0"
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Schema information for file content
|
|
3
|
+
*/
|
|
4
|
+
export type FileContentSchema = string | { schemaObject: string, namespace?: string }
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Base interface for filesystem nodes (without state)
|
|
8
|
+
*/
|
|
9
|
+
export interface AbstractFileSystemNodeBase {
|
|
10
|
+
type: 'directory' | 'file'
|
|
11
|
+
name: string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Abstract directory node (without state management)
|
|
16
|
+
*/
|
|
17
|
+
export interface AbstractDirectoryNode<T extends AbstractFileSystemNodeBase = AbstractFileSystemNode> extends AbstractFileSystemNodeBase {
|
|
18
|
+
type: 'directory'
|
|
19
|
+
children: Record<string, T>
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Abstract file node (without state management)
|
|
24
|
+
*/
|
|
25
|
+
export interface AbstractFileNode<T = any> extends AbstractFileSystemNodeBase {
|
|
26
|
+
type: 'file'
|
|
27
|
+
content: T
|
|
28
|
+
schema: string | FileContentSchema
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Union type for abstract filesystem nodes
|
|
33
|
+
*/
|
|
34
|
+
export type AbstractFileSystemNode = AbstractDirectoryNode | AbstractFileNode
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// Path utilities
|
|
2
|
+
export * from './path/pathway';
|
|
3
|
+
export * from './path/pathway-resolver';
|
|
4
|
+
|
|
5
|
+
// Itinerary utilities
|
|
6
|
+
export * from './itinerary/itinerary';
|
|
7
|
+
export * from './itinerary/stack';
|
|
8
|
+
export * from './itinerary/hyper-layer-node';
|
|
9
|
+
|
|
10
|
+
// Filesystem abstractions
|
|
11
|
+
export * from './filesystem/fs-node-abstract.types';
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export interface HyperLayerNode<T> {
|
|
2
|
+
keys?: Record<PropertyKey, any> // Keys to use for property based navigation when applicable
|
|
3
|
+
data? : T
|
|
4
|
+
children? : Record<string, HyperLayerNode<T>> | Array<HyperLayerNode<T>>
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function hyper<T>(data : T, children?: Record<string, HyperLayerNode<T>> | Array<HyperLayerNode<T>>) : HyperLayerNode<T> {
|
|
8
|
+
return {data, children}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Utility function that create Overlay which mirror original object
|
|
13
|
+
* @param o
|
|
14
|
+
*/
|
|
15
|
+
export function hyperReflect(o : any) : HyperLayerNode<any> {
|
|
16
|
+
if(o === null || o === undefined){
|
|
17
|
+
return hyper(o)
|
|
18
|
+
} else if (Array.isArray(o)) {
|
|
19
|
+
return hyper(o, o.map(e => hyperReflect(e)))
|
|
20
|
+
} else if(typeof o === "object"){
|
|
21
|
+
return hyper(o, Object.entries(o).reduce((acc, [k, v]) => ({...acc, [k] : hyperReflect(v)}), {}))
|
|
22
|
+
} else {
|
|
23
|
+
return hyper(o)
|
|
24
|
+
}
|
|
25
|
+
}
|