path-treeify 1.1.0-beta.94a5d17 → 1.2.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/dist/types/index.d.ts +53 -0
- package/package.json +1 -1
package/dist/types/index.d.ts
CHANGED
|
@@ -1,40 +1,93 @@
|
|
|
1
|
+
/** A filter function that determines whether a directory should be included in the tree */
|
|
1
2
|
type FilterFunction = (params: {
|
|
2
3
|
name: string;
|
|
3
4
|
dirPath: string;
|
|
4
5
|
}) => boolean;
|
|
6
|
+
/** Constructor options for PathTreeify */
|
|
5
7
|
interface PathTreeifyProps {
|
|
6
8
|
base: string;
|
|
7
9
|
filter?: FilterFunction;
|
|
8
10
|
}
|
|
11
|
+
/** Represents a single node (directory) in the path tree */
|
|
9
12
|
declare class PathTreeNode {
|
|
13
|
+
/** The root base path used to resolve absolute paths */
|
|
10
14
|
private base;
|
|
15
|
+
/** Reference to the parent node; null for the root node */
|
|
11
16
|
parent: PathTreeNode | null;
|
|
17
|
+
/** The directory name of this node (not a full path) */
|
|
12
18
|
value: string;
|
|
19
|
+
/** Child nodes representing subdirectories */
|
|
13
20
|
children: PathTreeNode[];
|
|
14
21
|
constructor(base: string);
|
|
22
|
+
/**
|
|
23
|
+
* Walks up the parent chain to compute this node's relative and absolute paths.
|
|
24
|
+
* @returns An object containing the relative path from base and the absolute path
|
|
25
|
+
*/
|
|
15
26
|
getPath(): {
|
|
16
27
|
relative: string;
|
|
17
28
|
absolute: string;
|
|
18
29
|
};
|
|
19
30
|
}
|
|
31
|
+
/** Builds a tree of directory nodes rooted at a given base path */
|
|
20
32
|
export declare class PathTreeify {
|
|
33
|
+
/** The root directory to scan */
|
|
21
34
|
private base;
|
|
35
|
+
/** Optional filter applied to each directory during traversal */
|
|
22
36
|
private filter?;
|
|
23
37
|
constructor({ filter, base }: Partial<PathTreeifyProps>);
|
|
38
|
+
/**
|
|
39
|
+
* Validates that the provided filter is a function, accepts one parameter,
|
|
40
|
+
* and returns a boolean. Throws a TypeError if any condition is violated.
|
|
41
|
+
*/
|
|
24
42
|
private validateFilter;
|
|
43
|
+
/**
|
|
44
|
+
* Creates and optionally attaches a new PathTreeNode to a parent.
|
|
45
|
+
* @param parent - The parent node to attach to, or null for the root
|
|
46
|
+
*/
|
|
25
47
|
private initNode;
|
|
48
|
+
/**
|
|
49
|
+
* Recursively reads a directory and builds child nodes for each subdirectory.
|
|
50
|
+
* Applies the instance-level filter if one is set.
|
|
51
|
+
* @param dirPath - Absolute path of the directory to read
|
|
52
|
+
* @param parent - The parent node to attach children to
|
|
53
|
+
*/
|
|
26
54
|
private buildChildren;
|
|
55
|
+
/**
|
|
56
|
+
* Validates that each entry in the array is a string pointing to
|
|
57
|
+
* an accessible directory relative to the base path.
|
|
58
|
+
* @param relativeDirNames - Array of relative directory path strings to validate
|
|
59
|
+
*/
|
|
27
60
|
private checkRelativePaths;
|
|
61
|
+
/**
|
|
62
|
+
* Strips leading and trailing slashes from each directory name
|
|
63
|
+
* and removes any resulting empty strings.
|
|
64
|
+
*/
|
|
28
65
|
private formatDirnames;
|
|
66
|
+
/** Returns the names of all immediate subdirectories under the base path */
|
|
29
67
|
private getAllDirNamesUnderBase;
|
|
68
|
+
/**
|
|
69
|
+
* Builds a tree rooted at base, containing only the specified subdirectories.
|
|
70
|
+
* @param dirNames - Relative directory names to include as top-level nodes
|
|
71
|
+
*/
|
|
30
72
|
private buildByDirNames;
|
|
73
|
+
/**
|
|
74
|
+
* Builds a tree using only the subdirectories under base that pass the given filter.
|
|
75
|
+
* @param filter - A predicate applied to each top-level directory name
|
|
76
|
+
*/
|
|
31
77
|
private buildByFilter;
|
|
78
|
+
/**
|
|
79
|
+
* Computes the relative and absolute paths for a given node
|
|
80
|
+
* by walking up the parent chain.
|
|
81
|
+
*/
|
|
32
82
|
getPathBy(node: PathTreeNode): {
|
|
33
83
|
relative: string;
|
|
34
84
|
absolute: string;
|
|
35
85
|
};
|
|
86
|
+
/** Overload: build the tree from an explicit list of relative directory names */
|
|
36
87
|
buildBy(dirNames: string[]): PathTreeNode;
|
|
88
|
+
/** Overload: build the tree from a predicate applied to top-level directory names */
|
|
37
89
|
buildBy(filter: (dirName: string) => boolean): PathTreeNode;
|
|
90
|
+
/** Builds a full tree from all immediate subdirectories under the base path */
|
|
38
91
|
build(): PathTreeNode;
|
|
39
92
|
}
|
|
40
93
|
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "path-treeify",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Convert a path or an array of paths into a tree-structured JavaScript object, where each node has a `parent` property that holds a circular reference to its parent node.",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.mjs",
|