node-directory-tree | Convert a directory tree to a JS object | Runtime Evironment library
kandi X-RAY | node-directory-tree Summary
kandi X-RAY | node-directory-tree Summary
Convert a directory tree to a JS object.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of node-directory-tree
node-directory-tree Key Features
node-directory-tree Examples and Code Snippets
Community Discussions
Trending Discussions on node-directory-tree
QUESTION
I have a directory structure from NPM package 'directory-tree' that I would like to flatten into a simpler nested structure. I want a tail recursive solution to convert the first object into the second, but I'm having trouble wrapping my head around how to construct it.
Of course the primary conditional is whether or not a 'node' in the first structure is a 'file' or a 'directory'. If it's a file, we simply want the basename of the file to key the relative path. However, if it's a directory, we want the basename of the directory to key an object and recurse down frome there.
I'll use their example to illustrate the structure:
...ANSWER
Answered 2018-Jan-15 at 16:08function copyNode(node, result = {}){
if(node.type === "directory"){
const folder = result[node.name] = {};
for(const sub of node.children)
copyNode(sub, folder);
} else {
result[node.name] = node.path;
}
return result;
}
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install node-directory-tree
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page