@hamak/shared-utils 0.4.7 → 0.4.9
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/package.json +4 -1
- package/src/core-utils-chrono.ts +0 -21
- package/src/core-utils-comparator.ts +0 -27
- package/src/core-utils-file.ts +0 -10
- package/src/core-utils-filesystem.spec.ts +0 -63
- package/src/core-utils-filesystem.ts +0 -232
- package/src/core-utils-fqn.ts +0 -17
- package/src/core-utils-pathway-resolver.ts +0 -36
- package/src/core-utils-pathway.ts +0 -270
- package/src/core-utils-registry.ts +0 -23
- package/src/core-utils-resolver.ts +0 -75
- package/src/core-utils-stack.spec.ts +0 -42
- package/src/core-utils-stack.ts +0 -62
- package/src/core-utils-types-hkt.ts +0 -9
- package/src/core-utils-types.ts +0 -127
- package/src/core-utils-validations.ts +0 -36
- package/src/core-utils.spec.ts +0 -56
- package/src/core-utils.ts +0 -139
- package/src/index.ts +0 -34
- package/src/itinerary/hyper-layer-node.ts +0 -25
- package/src/itinerary/itinerary.spec.ts +0 -388
- package/src/itinerary/itinerary.ts +0 -363
- package/src/itinerary/stack.spec.ts +0 -46
- package/src/itinerary/stack.ts +0 -62
- package/tsconfig.es2015.json +0 -21
- package/tsconfig.json +0 -19
- package/vitest.config.ts +0 -8
package/package.json
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hamak/shared-utils",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.9",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Shared utilities for path navigation, filesystem operations, and common helpers",
|
|
7
7
|
"main": "dist/index.js",
|
|
8
8
|
"types": "dist/index.d.ts",
|
|
9
9
|
"sideEffects": false,
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
10
13
|
"repository": {
|
|
11
14
|
"type": "git",
|
|
12
15
|
"url": "https://github.com/amah/app-framework.git",
|
package/src/core-utils-chrono.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
export class Chronometre {
|
|
2
|
-
private start : number
|
|
3
|
-
constructor() {
|
|
4
|
-
this.start = Date.now()
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
duration() : number {
|
|
8
|
-
return Date.now() - this.start
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
restart() : number {
|
|
12
|
-
const d = this.duration()
|
|
13
|
-
this.start = Date.now()
|
|
14
|
-
|
|
15
|
-
return d
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function chronometre() {
|
|
20
|
-
return new Chronometre()
|
|
21
|
-
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
export type Order = -1 | 0 | 1
|
|
2
|
-
export type Weighted<T, W> = {value: T, weight: W}
|
|
3
|
-
export type WeightFn<T> = (t : T) => number
|
|
4
|
-
export type CompareFn<T> = (a : T, b : T, ...fns : [WeightFn<T>, ...WeightFn<T>[]]) => Order
|
|
5
|
-
export function reverseOrder<T>(compareFn : CompareFn<T>) : CompareFn<T> {
|
|
6
|
-
return (a, b, fns) => - compareFn(a, b, fns) as Order
|
|
7
|
-
}
|
|
8
|
-
export function compare<T>(a : T, b : T, ...fns : WeightFn<T>[]) : Order {
|
|
9
|
-
if(fns.length === 0){
|
|
10
|
-
if(typeof a === "number" && typeof b === "number"){
|
|
11
|
-
fns = [(x : T) => x as unknown as number]
|
|
12
|
-
} else {
|
|
13
|
-
throw new Error(`Expecting at least one weight function when arguments are not numbers`)
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
for (const weightFn of fns) {
|
|
18
|
-
const aWeight = weightFn(a)
|
|
19
|
-
const bWeight = weightFn(b)
|
|
20
|
-
if(aWeight != bWeight){
|
|
21
|
-
return a < b ? -1 : 1
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
return 0
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export const compareDesc = reverseOrder(compare)
|
package/src/core-utils-file.ts
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { DirectoryNode, fsutils } from './core-utils-filesystem';
|
|
2
|
-
describe("Core utils filesystem", () => {
|
|
3
|
-
it("fsutils createDirectory - creates a file with intermediate directories", () => {
|
|
4
|
-
const root = fsutils.createRootDir([]);
|
|
5
|
-
const file = fsutils.createDescendent(root, "a/b/c.txt", "hello");
|
|
6
|
-
|
|
7
|
-
expect(file).toBeDefined();
|
|
8
|
-
expect(file?.type).toBe("file");
|
|
9
|
-
expect(file?.content).toBe("hello");
|
|
10
|
-
|
|
11
|
-
const dirA = root.children["a"];
|
|
12
|
-
expect(dirA?.type).toBe("directory");
|
|
13
|
-
|
|
14
|
-
const dirB = (dirA as DirectoryNode).children["b"];
|
|
15
|
-
expect(dirB?.type).toBe("directory");
|
|
16
|
-
|
|
17
|
-
const fileC = (dirB as DirectoryNode).children["c.txt"];
|
|
18
|
-
expect(fileC?.type).toBe("file");
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
it("fsutils createDirectory - throws if path conflicts with a file in intermediate segment", () => {
|
|
22
|
-
const conflict = fsutils.createFile("conflict", "not a dir");
|
|
23
|
-
const root = fsutils.createRootDir([conflict]);
|
|
24
|
-
|
|
25
|
-
expect(() =>
|
|
26
|
-
fsutils.createDescendent(root, "conflict/x.txt", "data")
|
|
27
|
-
).toThrow(/is a file/);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it("fsutils createDirectory - throws if file exists and override is false", () => {
|
|
31
|
-
const existing = fsutils.createFile("existing.txt", "original");
|
|
32
|
-
const root = fsutils.createRootDir([
|
|
33
|
-
fsutils.createDir("dir", [existing])
|
|
34
|
-
]);
|
|
35
|
-
|
|
36
|
-
expect(() =>
|
|
37
|
-
fsutils.createDescendent(root, "dir/existing.txt", "new content", "xs:any", undefined, false)
|
|
38
|
-
).toThrow(/already exists/);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it("fsutils createDirectory - overrides file if override is true", () => {
|
|
42
|
-
const existing = fsutils.createFile("existing.txt", "original");
|
|
43
|
-
const root = fsutils.createRootDir([
|
|
44
|
-
fsutils.createDir("dir", [existing])
|
|
45
|
-
]);
|
|
46
|
-
|
|
47
|
-
const newFile = fsutils.createDescendent(root, "dir/existing.txt", "new content", "xs:any", undefined, true);
|
|
48
|
-
|
|
49
|
-
expect(newFile?.content).toBe("new content");
|
|
50
|
-
expect((root.children["dir"] as DirectoryNode).children["existing.txt"]).toBe(newFile);
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it("fsutils createDirectory - returns undefined if path is '.'", () => {
|
|
54
|
-
const root = fsutils.createRootDir([]);
|
|
55
|
-
const result = fsutils.createDescendent(root, ".", "data");
|
|
56
|
-
expect(result).toBeUndefined();
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
it("fsutils createDirectory - returns undefined if fromDir is undefined", () => {
|
|
60
|
-
const result = fsutils.createDescendent(undefined, "a/b.txt", "data");
|
|
61
|
-
expect(result).toBeUndefined();
|
|
62
|
-
});
|
|
63
|
-
});
|
|
@@ -1,232 +0,0 @@
|
|
|
1
|
-
import { Path, Pathway } from './core-utils-pathway';
|
|
2
|
-
import { Holder } from './core-utils-types';
|
|
3
|
-
import { PathwayResolver } from './core-utils-pathway-resolver';
|
|
4
|
-
|
|
5
|
-
//export type FileContentSchema = string | {schemaObject : SchemaNode, namespace? : string}
|
|
6
|
-
export type FileContentSchema = string | { schemaObject: string /*TypeString*/, namespace?: string }
|
|
7
|
-
|
|
8
|
-
export interface FileSystemState {
|
|
9
|
-
root: DirectoryNode
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export interface FileMemoState {
|
|
13
|
-
originalContent: Holder
|
|
14
|
-
modified: boolean // tell whether the current file content is modified compared to original
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export interface FileSystemNodeState {
|
|
18
|
-
contentLoaded: boolean
|
|
19
|
-
contentLoading: boolean
|
|
20
|
-
contentLoadError?: { code?: string, message?: string }
|
|
21
|
-
memo?: FileMemoState
|
|
22
|
-
contentHistory: any[]
|
|
23
|
-
|
|
24
|
-
extensionStates: Record<string, unknown>
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
export function fileSystemNodeInitialState(contentPresent = true): FileSystemNodeState {
|
|
29
|
-
return {
|
|
30
|
-
contentLoaded: contentPresent,
|
|
31
|
-
contentLoading: false,
|
|
32
|
-
contentLoadError: undefined,
|
|
33
|
-
contentHistory: [],
|
|
34
|
-
extensionStates: {}
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
export interface FileSystemNodeBase {
|
|
40
|
-
type: 'directory' | 'file'
|
|
41
|
-
name: string
|
|
42
|
-
state: FileSystemNodeState
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
export interface DirectoryNode extends FileSystemNodeBase {
|
|
47
|
-
type: 'directory'
|
|
48
|
-
children: Record<string, FileSystemNode>
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export interface FileNode<T = any> extends FileSystemNodeBase {
|
|
52
|
-
type: 'file'
|
|
53
|
-
content: T
|
|
54
|
-
schema: string | FileContentSchema
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export type FileSystemNode = DirectoryNode | FileNode
|
|
58
|
-
|
|
59
|
-
function isRoot(node : FileSystemNode) : boolean {
|
|
60
|
-
switch (node.type) {
|
|
61
|
-
case "directory":{
|
|
62
|
-
return node.name === null || node.name === undefined || node.name.trim() === "" || node.name.trim() === "/"
|
|
63
|
-
}
|
|
64
|
-
case "file":{
|
|
65
|
-
return false
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export class DirectoryNodePathwayResolver implements PathwayResolver<FileSystemNode> {
|
|
71
|
-
constructor(readonly root: FileSystemNode) {
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
resolve(path: string | Pathway | string[]): FileSystemNode | undefined {
|
|
75
|
-
return fsutils.resolveFileNode(this.root, path)
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
changeDIr(path : Path) : DirectoryNodePathwayResolver {
|
|
79
|
-
const fileNode = this.resolve(path)
|
|
80
|
-
if(fileNode === undefined){
|
|
81
|
-
throw new Error(`No such directory: ${Pathway.asString(path)}`)
|
|
82
|
-
} else {
|
|
83
|
-
switch (fileNode.type) {
|
|
84
|
-
case "directory": return new DirectoryNodePathwayResolver(fileNode)
|
|
85
|
-
case "file": throw new Error(`Path is not directory: ${Pathway.asString(path)}`)
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
class FilesystemUtil{
|
|
92
|
-
createRootDir(children : FileSystemNode[], state = fileSystemNodeInitialState()) : DirectoryNode {
|
|
93
|
-
return this.createDir("", children, state)
|
|
94
|
-
}
|
|
95
|
-
createDir(name : string, children : FileSystemNode[], state = fileSystemNodeInitialState()) : DirectoryNode {
|
|
96
|
-
return {
|
|
97
|
-
type: "directory", name, state,
|
|
98
|
-
children: children.reduce((acc, child) => ({...acc, [child.name]: child}), {} as Record<string, FileSystemNode>)
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
createDirs(path : Path, children : FileSystemNode[], state = fileSystemNodeInitialState()) : DirectoryNode {
|
|
102
|
-
const pathway = Pathway.of(path)
|
|
103
|
-
const dir = pathway.getSegments().reverse().reduce((acc, segment) => {
|
|
104
|
-
return this.createDir(segment, acc === undefined ? children : [acc])
|
|
105
|
-
}, undefined as DirectoryNode | undefined)
|
|
106
|
-
|
|
107
|
-
if(dir === undefined){
|
|
108
|
-
throw new Error(`Unable de create dir '${path}'`)
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return pathway.isAbsolute() ? this.createRootDir([dir]) : dir
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
createFile(name : string, content : any, schema = "xs:any", state = fileSystemNodeInitialState()) : FileNode {
|
|
115
|
-
return {type: "file", name, content, schema, state}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
resolveFileNode(fromDir: FileSystemNode | undefined, path : Path) : FileSystemNode | undefined {
|
|
119
|
-
if(fromDir === undefined){
|
|
120
|
-
return undefined
|
|
121
|
-
}
|
|
122
|
-
const pathway = Pathway.of(path)
|
|
123
|
-
if (pathway.isAbsolute() && !isRoot(fromDir)) {
|
|
124
|
-
return undefined
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
const result = pathway.getSegments().reduce((acc, segment) => {
|
|
128
|
-
if (acc === undefined) {
|
|
129
|
-
return undefined
|
|
130
|
-
} else {
|
|
131
|
-
switch (acc.type) {
|
|
132
|
-
case "directory": {
|
|
133
|
-
return acc.children?.[segment]
|
|
134
|
-
}
|
|
135
|
-
break;
|
|
136
|
-
case "file": {
|
|
137
|
-
return undefined
|
|
138
|
-
}
|
|
139
|
-
break;
|
|
140
|
-
default: {
|
|
141
|
-
return undefined
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}, fromDir as FileSystemNode | undefined)
|
|
146
|
-
|
|
147
|
-
return result
|
|
148
|
-
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
asFile<T = any>(fsn : FileSystemNode | undefined) : FileNode<T> | undefined {
|
|
152
|
-
return fsn?.type === "file" ? fsn : undefined
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
asDirectory(fsn : FileSystemNode | undefined) : DirectoryNode | undefined {
|
|
156
|
-
return fsn?.type === "directory" ? fsn : undefined
|
|
157
|
-
}
|
|
158
|
-
resolveDescendent(fromDir: FileSystemNode | undefined, path : Path) : FileSystemNode | undefined {
|
|
159
|
-
const pathway = Pathway.of(path)
|
|
160
|
-
return pathway.isCurrentDir() ? undefined : this.resolveFileNode(fromDir, pathway)
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* Set a file node content a specified location. It create intermediary directories and target if missed location.
|
|
165
|
-
* @param fromDir
|
|
166
|
-
* @param path
|
|
167
|
-
* @param content
|
|
168
|
-
* @param schema
|
|
169
|
-
* @param state
|
|
170
|
-
*/
|
|
171
|
-
createDescendent(
|
|
172
|
-
fromDir: FileSystemNode | undefined,
|
|
173
|
-
path: Path,
|
|
174
|
-
content: any,
|
|
175
|
-
schema = "xs:any",
|
|
176
|
-
state = fileSystemNodeInitialState(),
|
|
177
|
-
override = false
|
|
178
|
-
): FileNode | undefined {
|
|
179
|
-
if (!fromDir) return undefined;
|
|
180
|
-
|
|
181
|
-
const pathway = Pathway.of(path);
|
|
182
|
-
if (pathway.isCurrentDir()) return undefined;
|
|
183
|
-
|
|
184
|
-
const segments = pathway.getSegments();
|
|
185
|
-
if (segments.length === 0) return undefined;
|
|
186
|
-
|
|
187
|
-
if (fromDir.type !== "directory") {
|
|
188
|
-
throw new Error("Cannot create descendent on a non-directory node.");
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
let currentDir = fromDir;
|
|
192
|
-
|
|
193
|
-
for (let i = 0; i < segments.length - 1; i++) {
|
|
194
|
-
const segment = segments[i];
|
|
195
|
-
let nextNode = currentDir.children[segment];
|
|
196
|
-
|
|
197
|
-
if (!nextNode) {
|
|
198
|
-
// Create intermediate directory
|
|
199
|
-
const newDir: DirectoryNode = this.createDir(segment, []);
|
|
200
|
-
currentDir.children[segment] = newDir;
|
|
201
|
-
nextNode = newDir;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
if (nextNode.type !== "directory") {
|
|
205
|
-
throw new Error(`Path conflict: '${segment}' is a file, expected a directory.`);
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
currentDir = nextNode;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
const fileName = segments[segments.length - 1];
|
|
212
|
-
const existingNode = currentDir.children[fileName];
|
|
213
|
-
|
|
214
|
-
if (existingNode) {
|
|
215
|
-
if (existingNode.type !== "file") {
|
|
216
|
-
throw new Error(`Path conflict: '${fileName}' exists and is not a file.`);
|
|
217
|
-
}
|
|
218
|
-
if (!override) {
|
|
219
|
-
throw new Error(`File '${fileName}' already exists and override is disabled.`);
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
const fileNode: FileNode = this.createFile(fileName, content, schema, state);
|
|
224
|
-
|
|
225
|
-
currentDir.children[fileName] = fileNode;
|
|
226
|
-
|
|
227
|
-
return fileNode;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
export const fsutils = new FilesystemUtil()
|
package/src/core-utils-fqn.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Unresolved qualified name by either namespace of prefix
|
|
3
|
-
*/
|
|
4
|
-
export class LexicalQualifiedName {
|
|
5
|
-
constructor(public readonly namespace: string | undefined, public readonly prefix : string | undefined, public readonly name : string) {
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
isEqualTo(other : LexicalQualifiedName) : boolean {
|
|
9
|
-
if((this.name === undefined && other.name === undefined)
|
|
10
|
-
|| (this.name === null && other.name === null)){
|
|
11
|
-
return false
|
|
12
|
-
} else {
|
|
13
|
-
return this.namespace === other.namespace && this.prefix === other.prefix && this.name === other.name
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import {Path, Pathway} from "./core-utils-pathway";
|
|
2
|
-
|
|
3
|
-
export interface PathwayResolver<T>{
|
|
4
|
-
resolve(path : string | string[] | Pathway) : T | undefined
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export class RecordPathwayResolver<T> implements PathwayResolver<T> {
|
|
8
|
-
constructor(readonly record : Record<string, T>) {
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
resolve(path: string | string[] | Pathway): T | undefined {
|
|
12
|
-
const key = Pathway.asString(path)
|
|
13
|
-
return this.record[key]
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export class EmptyPathwayResolver<T> implements PathwayResolver<T> {
|
|
18
|
-
resolve(path: string | string[] | Pathway): T | undefined {
|
|
19
|
-
return undefined
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export class RelativePathwayResolver<T> implements PathwayResolver<T> {
|
|
24
|
-
private relativePathway : Pathway
|
|
25
|
-
constructor(readonly parentResolver : PathwayResolver<T>, readonly path : Path) {
|
|
26
|
-
this.relativePathway = Pathway.of(path)
|
|
27
|
-
}
|
|
28
|
-
resolve(path: string | string[] | Pathway): T | undefined {
|
|
29
|
-
const pathway = Pathway.of(path)
|
|
30
|
-
if(pathway.isAbsolute()){
|
|
31
|
-
return this.parentResolver.resolve(path)
|
|
32
|
-
} else {
|
|
33
|
-
return this.parentResolver.resolve(this.relativePathway.resolve(pathway))
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
}
|
|
@@ -1,270 +0,0 @@
|
|
|
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
|
-
}
|
|
233
|
-
|
|
234
|
-
/**
|
|
235
|
-
* Converts a path (string or string array) to an array of path segments.
|
|
236
|
-
* If already an array, returns it as-is.
|
|
237
|
-
* If a string, splits by '/' and filters out empty segments.
|
|
238
|
-
*
|
|
239
|
-
* @param path - The path to convert to segments
|
|
240
|
-
* @returns Array of path segments
|
|
241
|
-
*
|
|
242
|
-
* @example
|
|
243
|
-
* pathSteps('/foo/bar/baz') // ['foo', 'bar', 'baz']
|
|
244
|
-
* pathSteps(['foo', 'bar']) // ['foo', 'bar']
|
|
245
|
-
* pathSteps('foo//bar/') // ['foo', 'bar']
|
|
246
|
-
*/
|
|
247
|
-
export function pathSteps(path: string | string[]): string[] {
|
|
248
|
-
if (Array.isArray(path)) {
|
|
249
|
-
return path;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
return path.split('/').filter(s => s.trim().length > 0);
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
/**
|
|
256
|
-
* Returns the parent path segments by removing the last segment.
|
|
257
|
-
* Uses pathSteps internally to normalize the input path.
|
|
258
|
-
*
|
|
259
|
-
* @param path - The path to get parent segments from
|
|
260
|
-
* @returns Array of parent path segments (all but last segment)
|
|
261
|
-
*
|
|
262
|
-
* @example
|
|
263
|
-
* parentPathSteps('/foo/bar/baz') // ['foo', 'bar']
|
|
264
|
-
* parentPathSteps(['foo', 'bar']) // ['foo']
|
|
265
|
-
* parentPathSteps(['foo']) // []
|
|
266
|
-
*/
|
|
267
|
-
export function parentPathSteps(path: string | string[]): string[] {
|
|
268
|
-
const steps = pathSteps(path);
|
|
269
|
-
return steps.length > 0 ? steps.slice(0, steps.length - 1) : steps;
|
|
270
|
-
}
|