@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,232 @@
|
|
|
1
|
+
export type Path = string | string[] | Pathway
|
|
2
|
+
/**
|
|
3
|
+
* Class representing a normalized pathway.
|
|
4
|
+
*/
|
|
5
|
+
export class Pathway {
|
|
6
|
+
private segments: string[];
|
|
7
|
+
private isAbsolutePath: boolean;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Creates a Pathway instance.
|
|
11
|
+
* @param path - The pathway to normalize, either as a string or an array of strings.
|
|
12
|
+
* An empty array or "." represents the current directory.
|
|
13
|
+
*/
|
|
14
|
+
constructor(path: string | string[] | null | undefined) {
|
|
15
|
+
if (path == null || path === "." || (Array.isArray(path) && path.length === 0)) {
|
|
16
|
+
this.segments = [];
|
|
17
|
+
this.isAbsolutePath = false;
|
|
18
|
+
} else {
|
|
19
|
+
this.isAbsolutePath = Array.isArray(path) ? path[0].startsWith('/') : path.startsWith('/');
|
|
20
|
+
this.segments = this.normalizePath(path);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Factory method to create a Pathway instance.
|
|
26
|
+
* @param path - The pathway to normalize, either as a string or an array of strings.
|
|
27
|
+
* @returns A new Pathway instance.
|
|
28
|
+
*/
|
|
29
|
+
public static of(path: string | string[] | null | undefined | Pathway): Pathway {
|
|
30
|
+
if(path !== undefined && path !== null && typeof path === "object" && !Array.isArray(path)) {
|
|
31
|
+
return path
|
|
32
|
+
} else {
|
|
33
|
+
return new Pathway(path);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Normalizes a pathway by converting it into an array of segments.
|
|
39
|
+
* @param path - The pathway to normalize, either as a string or an array of strings.
|
|
40
|
+
* @returns An array of normalized path segments, or an empty array if the input is null or undefined.
|
|
41
|
+
*/
|
|
42
|
+
private normalizePath(path: string | string[] | null | undefined): string[] {
|
|
43
|
+
if (path == null) {
|
|
44
|
+
return [];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const segments = Array.isArray(path) ? path : path.split('/');
|
|
48
|
+
return segments
|
|
49
|
+
.reduce((acc, segment) => {
|
|
50
|
+
if (segment === "." || segment === "") {
|
|
51
|
+
return acc;
|
|
52
|
+
}
|
|
53
|
+
return acc.concat(segment.split('/'));
|
|
54
|
+
}, [] as string[])
|
|
55
|
+
.filter(segment => segment.length > 0);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Returns the normalized path segments.
|
|
60
|
+
* @returns An array of normalized path segments.
|
|
61
|
+
*/
|
|
62
|
+
public getSegments(): string[] {
|
|
63
|
+
return this.segments;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
public isCurrentDir(){
|
|
67
|
+
return this.getSegments().length === 0
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Converts the normalized path segments back to a string.
|
|
72
|
+
* @returns The normalized path as a string.
|
|
73
|
+
*/
|
|
74
|
+
public toString(): string {
|
|
75
|
+
return this.isAbsolute() ? '/' + this.segments.join('/') : this.segments.join('/');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Checks if the pathway is absolute.
|
|
80
|
+
* @returns True if the pathway is absolute, false otherwise.
|
|
81
|
+
*/
|
|
82
|
+
public isAbsolute(): boolean {
|
|
83
|
+
return this.isAbsolutePath;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Checks if the pathway is relative.
|
|
88
|
+
* @returns True if the pathway is relative, false otherwise.
|
|
89
|
+
*/
|
|
90
|
+
public isRelative(): boolean {
|
|
91
|
+
return !this.isAbsolutePath;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Resolves the current pathway with another pathway or string.
|
|
96
|
+
* @param otherPath - The other pathway or string to resolve against.
|
|
97
|
+
* @returns A new Pathway instance representing the resolved path.
|
|
98
|
+
*/
|
|
99
|
+
public resolve(otherPath: Pathway | string | string[]): Pathway {
|
|
100
|
+
const otherSegments = otherPath instanceof Pathway ? otherPath.getSegments() : this.normalizePath(otherPath);
|
|
101
|
+
const resolvedSegments = [...this.segments];
|
|
102
|
+
|
|
103
|
+
if (otherPath instanceof Pathway ? otherPath.isAbsolute() : (Array.isArray(otherPath) ? otherPath[0].startsWith('/') : otherPath.startsWith('/'))) {
|
|
104
|
+
return new Pathway(otherSegments);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
for (const segment of otherSegments) {
|
|
108
|
+
if (segment === '..') {
|
|
109
|
+
if (resolvedSegments.length > 0 && resolvedSegments[resolvedSegments.length - 1] !== '..') {
|
|
110
|
+
resolvedSegments.pop();
|
|
111
|
+
} else {
|
|
112
|
+
resolvedSegments.push(segment);
|
|
113
|
+
}
|
|
114
|
+
} else if (segment !== '.') {
|
|
115
|
+
resolvedSegments.push(segment);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return new Pathway(this.isAbsolute() ? ["/", ...resolvedSegments] : resolvedSegments);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Gets the parent directory of the current pathway.
|
|
124
|
+
* @returns A new Pathway instance representing the parent directory.
|
|
125
|
+
*/
|
|
126
|
+
public getParent(): Pathway {
|
|
127
|
+
if (this.segments.length === 0) {
|
|
128
|
+
return new Pathway(null);
|
|
129
|
+
}
|
|
130
|
+
const parentSegments = this.segments.slice(0, -1);
|
|
131
|
+
return new Pathway(this.isAbsolute() ? ["/", ...parentSegments] : parentSegments);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Gets the current directory of the pathway.
|
|
136
|
+
* @returns The current directory as a string.
|
|
137
|
+
*/
|
|
138
|
+
public getCurrentDirectory(): string {
|
|
139
|
+
if (this.segments.length === 0) {
|
|
140
|
+
return '';
|
|
141
|
+
}
|
|
142
|
+
return this.segments[this.segments.length - 1];
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Static method to get the absolute path of the root.
|
|
147
|
+
* @returns A new Pathway instance representing the root path.
|
|
148
|
+
*/
|
|
149
|
+
public static ofRoot(): Pathway {
|
|
150
|
+
return new Pathway('/');
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Determines the relative path from this pathway to another pathway.
|
|
155
|
+
* @param otherPath - The other pathway to relativize against.
|
|
156
|
+
* @returns A new Pathway instance representing the relative path.
|
|
157
|
+
*/
|
|
158
|
+
public relativize(otherPath: Pathway | string | string[]): Pathway {
|
|
159
|
+
const otherSegments = otherPath instanceof Pathway ? otherPath.getSegments() : this.normalizePath(otherPath);
|
|
160
|
+
|
|
161
|
+
if (this.isAbsolute() !== (otherPath instanceof Pathway ? otherPath.isAbsolute() : (Array.isArray(otherPath) ? otherPath[0].startsWith('/') : otherPath.startsWith('/')))) {
|
|
162
|
+
throw new Error("Cannot relativize between absolute and relative paths");
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
let commonLength = 0;
|
|
166
|
+
const maxLength = Math.min(this.segments.length, otherSegments.length);
|
|
167
|
+
for (let i = 0; i < maxLength; i++) {
|
|
168
|
+
if (this.segments[i] !== otherSegments[i]) {
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
commonLength++;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const upSegments = this.segments.slice(commonLength).map(() => '..');
|
|
175
|
+
const downSegments = otherSegments.slice(commonLength);
|
|
176
|
+
|
|
177
|
+
return new Pathway(upSegments.concat(downSegments));
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Checks if the given path is a descendant of the current pathway.
|
|
182
|
+
* @param otherPath - The other pathway to check.
|
|
183
|
+
* @returns True if the given path is a descendant, false otherwise.
|
|
184
|
+
*/
|
|
185
|
+
public isDescendant(otherPath: Pathway | string | string[]): boolean {
|
|
186
|
+
const otherSegments = otherPath instanceof Pathway ? otherPath.getSegments() : this.normalizePath(otherPath);
|
|
187
|
+
|
|
188
|
+
if (otherSegments.length < this.segments.length) {
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
for (let i = 0; i < this.segments.length; i++) {
|
|
193
|
+
if (this.segments[i] !== otherSegments[i]) {
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return true;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
static isRelativePath(path : Path) : boolean {
|
|
203
|
+
const pathway = typeof path === "object" && !Array.isArray(path) ? path : Pathway.of(path)
|
|
204
|
+
return pathway.isRelative()
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
static asString(path : Path) : string {
|
|
208
|
+
if(typeof path === "string"){
|
|
209
|
+
return path
|
|
210
|
+
} else {
|
|
211
|
+
const pathway = typeof path === "object" && !Array.isArray(path)? path : Pathway.of(path)
|
|
212
|
+
return pathway.toString()
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
static getName(path: string | string[] | Pathway) {
|
|
217
|
+
let name: string
|
|
218
|
+
switch (typeof path) {
|
|
219
|
+
case "string": {
|
|
220
|
+
name = path
|
|
221
|
+
}
|
|
222
|
+
break
|
|
223
|
+
case "object": {
|
|
224
|
+
const segments = Array.isArray(path) ? path : path.getSegments()
|
|
225
|
+
name = segments[segments.length - 1]
|
|
226
|
+
}
|
|
227
|
+
break
|
|
228
|
+
}
|
|
229
|
+
return name;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "./tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"target": "ES2015",
|
|
5
|
+
"lib": [
|
|
6
|
+
"ES2015"
|
|
7
|
+
],
|
|
8
|
+
"outDir": "./dist/es2015",
|
|
9
|
+
"declaration": false,
|
|
10
|
+
"declarationMap": false,
|
|
11
|
+
"sourceMap": false,
|
|
12
|
+
"downlevelIteration": true,
|
|
13
|
+
"composite": false
|
|
14
|
+
},
|
|
15
|
+
"include": [
|
|
16
|
+
"src/**/*"
|
|
17
|
+
],
|
|
18
|
+
"exclude": [
|
|
19
|
+
"node_modules",
|
|
20
|
+
"dist",
|
|
21
|
+
"**/*.test.ts"
|
|
22
|
+
]
|
|
23
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"lib": ["ES2020"],
|
|
6
|
+
"skipLibCheck": true,
|
|
7
|
+
"moduleResolution": "bundler",
|
|
8
|
+
"resolveJsonModule": true,
|
|
9
|
+
"isolatedModules": true,
|
|
10
|
+
"strict": true,
|
|
11
|
+
"outDir": "./dist",
|
|
12
|
+
"rootDir": "./src",
|
|
13
|
+
"declaration": true,
|
|
14
|
+
"declarationMap": true,
|
|
15
|
+
"allowImportingTsExtensions": false
|
|
16
|
+
},
|
|
17
|
+
"include": ["src/**/*"],
|
|
18
|
+
"exclude": ["node_modules", "dist", "**/*.test.ts"]
|
|
19
|
+
}
|