-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdirTreeFilters.js
More file actions
60 lines (57 loc) · 1.97 KB
/
dirTreeFilters.js
File metadata and controls
60 lines (57 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* Remove all tree nodes below a given depth.
* Note: This feature can be inefficient due to this filtering taking place on a complete directory tree,
* compared to filtering while building the dirTree in the first place.
* To fix we would need to add this feature to directory-tree instead.
* @param {dirTree} tree A directory tree
* @param {number} maxDepth Maximum depth of files/directories to include in tree
*/
function filterToMaxDepth(tree, maxDepth) {
if (tree.children && tree.children.length > 0) {
if (maxDepth <= 0) {
tree.children = [];
} else {
tree.children.forEach(child => {
if (child.type === 'directory') {
filterToMaxDepth(child, maxDepth - 1);
}
})
}
}
}
/**
* Remove files and directories from tree that don't match regexp.
* Note: This feature can be inefficient due to this filtering taking place on a complete directory tree,
* compared to filtering while building the dirTree in the first place.
* To fix we would need to add this feature to directory-tree instead.
* @param {dirTree} tree A directory tree
* @param {Regexp} regexp Regexp to match nodes against
*/
function filterIncluded(tree, regexp) {
if (!tree || !regexp.test(tree.name)) {
return false;
}
if (tree.children && tree.children.length > 0) {
tree.children = tree.children.filter(child => filterIncluded(child, regexp));
}
return true;
}
/**
* Remove empty directories from the given tree
* @param {dirTree} tree A directory tree
*/
function filterEmptyDirectories(tree) {
if (tree.children && tree.children.length > 0) {
tree.children.forEach(child => {
if (child.type === 'directory') {
child.children = filterEmptyDirectories(child);
}
});
}
return tree.children = tree.children.filter(child => child.type === 'file' || child.children.length > 0);
}
module.exports = {
filterToMaxDepth,
filterIncluded,
filterEmptyDirectories,
}