path-treeify 1.3.0-beta.45ab046 β 1.3.0-beta.56a60fd
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 +8 -2
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +1 -1
- package/dist/types/index.d.ts +73 -46
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
<div align="center">
|
|
2
|
+
<img alt="LOGO" width="320" src="https://assets-amu.pages.dev/path-treeify/logo.png">
|
|
3
|
+
</div>
|
|
4
|
+
<br/>
|
|
2
5
|
|
|
3
6
|
> π [δΈζζζ‘£ (Chinese README)](https://github.com/isaaxite/path-treeify/blob/main/docs/README.zh-CN.md)
|
|
4
7
|
|
|
@@ -14,6 +17,9 @@
|
|
|
14
17
|
<a href="https://nodejs.org">
|
|
15
18
|
<img alt="node" src="https://img.shields.io/node/v/path-treeify">
|
|
16
19
|
</a>
|
|
20
|
+
<a href="https://github.com/isaaxite/path-treeify/blob/main/CHANGELOG.md">
|
|
21
|
+
<img alt="CHANGELOG" src="https://img.shields.io/badge/changelog-maintained-brightgreen">
|
|
22
|
+
</a>
|
|
17
23
|
<a href="https://github.com/isaaxite/path-treeify/blob/main/LICENSE">
|
|
18
24
|
<img alt="GitHub License" src="https://img.shields.io/github/license/isaaxite/path-treeify">
|
|
19
25
|
</a>
|
|
@@ -253,4 +259,4 @@ const tree = treeify.build();
|
|
|
253
259
|
|
|
254
260
|
## License
|
|
255
261
|
|
|
256
|
-
[MIT](https://github.com/isaaxite/path-treeify/blob/main/LICENSE) Β© [isaaxite](https://github.com/isaaxite)
|
|
262
|
+
[MIT](https://github.com/isaaxite/path-treeify/blob/main/LICENSE) Β© [isaaxite](https://github.com/isaaxite)
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var t=require("fs"),
|
|
1
|
+
"use strict";var e,t=require("fs"),r=require("path");class i{static isValid(e){try{return t.accessSync(e,t.constants.F_OK),!0}catch{return!1}}static isDirectory(e){try{return t.statSync(e).isDirectory()}catch{return!1}}static isFile(e){try{return t.statSync(e).isFile()}catch{return!1}}}exports.PathTreeNodeType=void 0,(e=exports.PathTreeNodeType||(exports.PathTreeNodeType={})).Dir="dir",e.File="file",e.Unknown="unknown";class s{constructor(){this.parent=null,this.value="",this.children=[],this.type=exports.PathTreeNodeType.Unknown}getPath(){let e="",t=this;for(;t.parent;)e=e?`${t.value}${r.sep}${e}`:t.value,t=t.parent;return e}}exports.PathTreeNode=s,exports.PathTreeify=class{applyFilter(e,t){return!(!this.fileVisible&&!i.isDirectory(e))&&(!this.userFilter||this.userFilter({name:t,dirPath:r.dirname(e)}))}constructor({filter:e,base:t,fileVisible:r}){if(this.fileVisible=!1,"boolean"==typeof r&&r&&(this.fileVisible=r),void 0!==e&&(this.validateFilter(e),this.userFilter=e),!t||!i.isValid(t))throw new Error(`${t} is not a valid path!`);if(!i.isDirectory(t))throw new Error(`${t} is not a dirPath!`);this.base=t}validateFilter(e){if("function"!=typeof e)throw new TypeError("filter must be a function")}initNode(){return new s}buildChildren(e,s){const n=[],o=t.readdirSync(e);for(const t of o){const o=r.join(e,t);if(!this.applyFilter(o,t))continue;const l=this.initNode();l.value=t,l.parent=s,n.push(l),this.fileVisible&&i.isFile(o)?l.type=exports.PathTreeNodeType.File:(l.type=exports.PathTreeNodeType.Dir,l.children=this.buildChildren(o,l))}return n}checkRelativePaths(e){if(!Array.isArray(e))throw new Error("Expected array, got "+typeof e);for(let t=0;t<e.length;t++){const s=e[t];if("string"!=typeof s)throw new Error(`Item at index ${t} is not a string, got ${typeof s}`);const n=r.resolve(this.base,s);if(!i.isValid(n))throw new Error(`Path does not exist or is not accessible: ${n} (from relative path: ${s})`);if(!(i.isDirectory(n)||this.fileVisible&&i.isFile(n)))throw new Error(`Path is not a directory: ${n} (from relative path: ${s})`)}}formatSegments(e){return e.map(e=>e.split(/[/\\]/).filter(Boolean).join(r.sep)).filter(Boolean)}getAllEntriesUnderBase(){return t.readdirSync(this.base).filter(e=>{const t=r.resolve(this.base,e);return this.applyFilter(t,e)})}buildBySegments(e){const t=this.initNode(),s=this.formatSegments(e);this.checkRelativePaths(s);for(const e of s){const s=r.resolve(this.base,e),n=this.initNode();n.value=e,n.parent=t,t.children.push(n),this.fileVisible&&i.isFile(s)?n.type=exports.PathTreeNodeType.File:(n.type=exports.PathTreeNodeType.Dir,n.children=this.buildChildren(s,n))}return t}buildByFilter(e){const t=this.getAllEntriesUnderBase();return this.buildBySegments(t.filter(e))}getPathBy(e){const t=e.getPath();return{relative:t,absolute:r.resolve(this.base,t)}}buildBy(e){if(Array.isArray(e))return this.buildBySegments(e);if("function"==typeof e)return this.buildByFilter(e);throw new TypeError("buildBy: expected an array of strings or a filter function, but received "+typeof e)}build(){const e=this.getAllEntriesUnderBase();return this.buildBySegments(e)}};
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{readdirSync as t,
|
|
1
|
+
import{readdirSync as t,accessSync as i,constants as e,statSync as r}from"fs";import{sep as s,dirname as n,join as l,resolve as o}from"path";class a{static isValid(t){try{return i(t,e.F_OK),!0}catch{return!1}}static isDirectory(t){try{return r(t).isDirectory()}catch{return!1}}static isFile(t){try{return r(t).isFile()}catch{return!1}}}var h;!function(t){t.Dir="dir",t.File="file",t.Unknown="unknown"}(h||(h={}));class c{constructor(){this.parent=null,this.value="",this.children=[],this.type=h.Unknown}getPath(){let t="",i=this;for(;i.parent;)t=t?`${i.value}${s}${t}`:i.value,i=i.parent;return t}}class u{applyFilter(t,i){return!(!this.fileVisible&&!a.isDirectory(t))&&(!this.userFilter||this.userFilter({name:i,dirPath:n(t)}))}constructor({filter:t,base:i,fileVisible:e}){if(this.fileVisible=!1,"boolean"==typeof e&&e&&(this.fileVisible=e),void 0!==t&&(this.validateFilter(t),this.userFilter=t),!i||!a.isValid(i))throw new Error(`${i} is not a valid path!`);if(!a.isDirectory(i))throw new Error(`${i} is not a dirPath!`);this.base=i}validateFilter(t){if("function"!=typeof t)throw new TypeError("filter must be a function")}initNode(){return new c}buildChildren(i,e){const r=[],s=t(i);for(const t of s){const s=l(i,t);if(!this.applyFilter(s,t))continue;const n=this.initNode();n.value=t,n.parent=e,r.push(n),this.fileVisible&&a.isFile(s)?n.type=h.File:(n.type=h.Dir,n.children=this.buildChildren(s,n))}return r}checkRelativePaths(t){if(!Array.isArray(t))throw new Error("Expected array, got "+typeof t);for(let i=0;i<t.length;i++){const e=t[i];if("string"!=typeof e)throw new Error(`Item at index ${i} is not a string, got ${typeof e}`);const r=o(this.base,e);if(!a.isValid(r))throw new Error(`Path does not exist or is not accessible: ${r} (from relative path: ${e})`);if(!(a.isDirectory(r)||this.fileVisible&&a.isFile(r)))throw new Error(`Path is not a directory: ${r} (from relative path: ${e})`)}}formatSegments(t){return t.map(t=>t.split(/[/\\]/).filter(Boolean).join(s)).filter(Boolean)}getAllEntriesUnderBase(){return t(this.base).filter(t=>{const i=o(this.base,t);return this.applyFilter(i,t)})}buildBySegments(t){const i=this.initNode(),e=this.formatSegments(t);this.checkRelativePaths(e);for(const t of e){const e=o(this.base,t),r=this.initNode();r.value=t,r.parent=i,i.children.push(r),this.fileVisible&&a.isFile(e)?r.type=h.File:(r.type=h.Dir,r.children=this.buildChildren(e,r))}return i}buildByFilter(t){const i=this.getAllEntriesUnderBase();return this.buildBySegments(i.filter(t))}getPathBy(t){const i=t.getPath();return{relative:i,absolute:o(this.base,i)}}buildBy(t){if(Array.isArray(t))return this.buildBySegments(t);if("function"==typeof t)return this.buildByFilter(t);throw new TypeError("buildBy: expected an array of strings or a filter function, but received "+typeof t)}build(){const t=this.getAllEntriesUnderBase();return this.buildBySegments(t)}}export{c as PathTreeNode,h as PathTreeNodeType,u as PathTreeify};
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,93 +1,120 @@
|
|
|
1
|
-
/** A filter function that determines whether
|
|
1
|
+
/** A filter function that determines whether an entry should be included in the tree */
|
|
2
2
|
type FilterFunction = (params: {
|
|
3
3
|
name: string;
|
|
4
4
|
dirPath: string;
|
|
5
5
|
}) => boolean;
|
|
6
6
|
/** Constructor options for PathTreeify */
|
|
7
7
|
interface PathTreeifyProps {
|
|
8
|
+
/** The root directory to scan */
|
|
8
9
|
base: string;
|
|
10
|
+
/** Optional filter applied to every entry during recursive traversal */
|
|
9
11
|
filter?: FilterFunction;
|
|
12
|
+
/** When true, files are included as leaf nodes alongside directories. Defaults to false */
|
|
13
|
+
fileVisible?: boolean;
|
|
10
14
|
}
|
|
11
|
-
/**
|
|
12
|
-
declare
|
|
13
|
-
|
|
14
|
-
|
|
15
|
+
/** Classification of a node in the path tree */
|
|
16
|
+
export declare enum PathTreeNodeType {
|
|
17
|
+
Dir = "dir",
|
|
18
|
+
File = "file",
|
|
19
|
+
/** Assigned before the node's type has been resolved */
|
|
20
|
+
Unknown = "unknown"
|
|
21
|
+
}
|
|
22
|
+
/** Represents a single entry (directory or file) in the path tree */
|
|
23
|
+
export declare class PathTreeNode {
|
|
15
24
|
/** Reference to the parent node; null for the root node */
|
|
16
25
|
parent: PathTreeNode | null;
|
|
17
|
-
/** The
|
|
26
|
+
/** The entry name of this node (not a full path) */
|
|
18
27
|
value: string;
|
|
19
|
-
/** Child nodes
|
|
28
|
+
/** Child nodes; non-empty only for directory nodes */
|
|
20
29
|
children: PathTreeNode[];
|
|
21
|
-
|
|
30
|
+
/** Whether this node is a directory, a file, or not yet resolved */
|
|
31
|
+
type: PathTreeNodeType;
|
|
22
32
|
/**
|
|
23
|
-
* Walks up the parent chain to compute this node's relative
|
|
24
|
-
* @returns
|
|
33
|
+
* Walks up the parent chain to compute this node's relative path from the tree root.
|
|
34
|
+
* @returns The relative path string using the platform separator
|
|
25
35
|
*/
|
|
26
|
-
getPath():
|
|
27
|
-
relative: string;
|
|
28
|
-
absolute: string;
|
|
29
|
-
};
|
|
36
|
+
getPath(): string;
|
|
30
37
|
}
|
|
31
|
-
/** Builds a tree of
|
|
38
|
+
/** Builds a tree of {@link PathTreeNode} entries rooted at a given base path */
|
|
32
39
|
export declare class PathTreeify {
|
|
33
40
|
/** The root directory to scan */
|
|
34
41
|
private base;
|
|
35
|
-
/** Optional filter applied to each directory during traversal */
|
|
36
|
-
private filter?;
|
|
37
|
-
constructor({ filter, base }: Partial<PathTreeifyProps>);
|
|
38
42
|
/**
|
|
39
|
-
*
|
|
40
|
-
*
|
|
43
|
+
* Optional user-supplied filter. When set, every entry must pass this predicate
|
|
44
|
+
* in addition to the built-in visibility check.
|
|
41
45
|
*/
|
|
42
|
-
private
|
|
46
|
+
private userFilter?;
|
|
47
|
+
/** When true, files are included as leaf nodes during traversal. Defaults to false */
|
|
48
|
+
private fileVisible;
|
|
49
|
+
/**
|
|
50
|
+
* Determines whether a given entry should be included in the tree.
|
|
51
|
+
* - If {@link fileVisible} is false, non-directory entries are always excluded.
|
|
52
|
+
* - If a {@link userFilter} is set, the entry must also satisfy it.
|
|
53
|
+
* @param absPath - Absolute path of the entry to test
|
|
54
|
+
* @param name - Entry name (filename or directory name)
|
|
55
|
+
*/
|
|
56
|
+
private applyFilter;
|
|
57
|
+
constructor({ filter, base, fileVisible }: Partial<PathTreeifyProps>);
|
|
43
58
|
/**
|
|
44
|
-
*
|
|
45
|
-
*
|
|
59
|
+
* Asserts that the provided value is a callable {@link FilterFunction}.
|
|
60
|
+
* Throws a TypeError if the check fails.
|
|
46
61
|
*/
|
|
62
|
+
private validateFilter;
|
|
63
|
+
/** Creates a new unattached {@link PathTreeNode} */
|
|
47
64
|
private initNode;
|
|
48
65
|
/**
|
|
49
|
-
* Recursively reads
|
|
50
|
-
*
|
|
66
|
+
* Recursively reads {@link dirPath} and builds child nodes for each entry that
|
|
67
|
+
* passes {@link applyFilter}. Directories are traversed depth-first;
|
|
68
|
+
* files (when {@link fileVisible} is true) become leaf nodes.
|
|
51
69
|
* @param dirPath - Absolute path of the directory to read
|
|
52
|
-
* @param parent - The parent node to attach
|
|
70
|
+
* @param parent - The parent node to attach child nodes to
|
|
53
71
|
*/
|
|
54
72
|
private buildChildren;
|
|
55
73
|
/**
|
|
56
|
-
* Validates that
|
|
57
|
-
*
|
|
58
|
-
*
|
|
74
|
+
* Validates that every entry in {@link relativeSegments} refers to an accessible
|
|
75
|
+
* path under {@link base}. When {@link fileVisible} is false, each path must be
|
|
76
|
+
* a directory; when true, regular files are also accepted.
|
|
77
|
+
* @param relativeSegments - Relative path strings to validate
|
|
59
78
|
*/
|
|
60
79
|
private checkRelativePaths;
|
|
61
80
|
/**
|
|
62
|
-
*
|
|
63
|
-
* and
|
|
81
|
+
* Normalises an array of path strings by splitting on both slash styles,
|
|
82
|
+
* dropping empty segments, and rejoining with the platform separator.
|
|
83
|
+
* Entries that reduce to an empty string (e.g. `"///"`) are removed.
|
|
84
|
+
* @param segments - Raw path strings to normalise
|
|
85
|
+
*/
|
|
86
|
+
private formatSegments;
|
|
87
|
+
/**
|
|
88
|
+
* Returns the names of all immediate entries under {@link base} that pass
|
|
89
|
+
* {@link applyFilter}.
|
|
64
90
|
*/
|
|
65
|
-
private
|
|
66
|
-
/** Returns the names of all immediate subdirectories under the base path */
|
|
67
|
-
private getAllDirNamesUnderBase;
|
|
91
|
+
private getAllEntriesUnderBase;
|
|
68
92
|
/**
|
|
69
|
-
* Builds a
|
|
70
|
-
*
|
|
93
|
+
* Builds a subtree containing only the entries identified by {@link segments}.
|
|
94
|
+
* Paths are normalised via {@link formatSegments} and validated before use.
|
|
95
|
+
* @param segments - Relative paths to include as top-level nodes
|
|
71
96
|
*/
|
|
72
|
-
private
|
|
97
|
+
private buildBySegments;
|
|
73
98
|
/**
|
|
74
|
-
* Builds a
|
|
75
|
-
*
|
|
99
|
+
* Builds a subtree from top-level entries whose names satisfy {@link filter}.
|
|
100
|
+
* Note: this predicate only affects top-level selection, not recursive traversal.
|
|
101
|
+
* For recursive filtering use the `filter` constructor option.
|
|
102
|
+
* @param filter - Predicate applied to each top-level entry name
|
|
76
103
|
*/
|
|
77
104
|
private buildByFilter;
|
|
78
105
|
/**
|
|
79
|
-
*
|
|
80
|
-
*
|
|
106
|
+
* Returns the relative and absolute paths for a given node by delegating to
|
|
107
|
+
* {@link PathTreeNode.getPath} and resolving against {@link base}.
|
|
81
108
|
*/
|
|
82
109
|
getPathBy(node: PathTreeNode): {
|
|
83
110
|
relative: string;
|
|
84
111
|
absolute: string;
|
|
85
112
|
};
|
|
86
|
-
/** Overload: build the tree from an explicit list of relative
|
|
87
|
-
buildBy(
|
|
88
|
-
/** Overload: build the tree from a predicate applied to top-level
|
|
89
|
-
buildBy(filter: (
|
|
90
|
-
/** Builds a full tree from all immediate
|
|
113
|
+
/** Overload: build the tree from an explicit list of relative path segments */
|
|
114
|
+
buildBy(segments: string[]): PathTreeNode;
|
|
115
|
+
/** Overload: build the tree from a predicate applied to top-level entry names */
|
|
116
|
+
buildBy(filter: (segment: string) => boolean): PathTreeNode;
|
|
117
|
+
/** Builds a full tree from all immediate entries under the base path */
|
|
91
118
|
build(): PathTreeNode;
|
|
92
119
|
}
|
|
93
120
|
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "path-treeify",
|
|
3
|
-
"version": "1.3.0-beta.
|
|
3
|
+
"version": "1.3.0-beta.56a60fd",
|
|
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",
|
|
@@ -95,6 +95,7 @@
|
|
|
95
95
|
"rollup": "^4.59.0",
|
|
96
96
|
"tap-xunit": "^2.4.1",
|
|
97
97
|
"tslib": "^2.8.1",
|
|
98
|
+
"tsx": "^4.21.0",
|
|
98
99
|
"typescript": "^5.9.3"
|
|
99
100
|
},
|
|
100
101
|
"engines": {
|