I\'m walking large directory tree recursively (from 100,000 to 1,000,000 objects) and need to add each file or directory to deeply nested object.
Let\'s assume I got fi
There you go :)
function buildTree(pathes, getValueCB) {
var currentPath, lastPath, node, parent, map = {
"": {
children: []
}
},
stack = [""]
for (let path of pathes) {
let nodes = path.split("/");
for (let i = 0; i < nodes.length; i++) {
currentPath = "/" + nodes.slice(1, i + 1).join("/")
lastPath = stack[stack.length - 1]
parent = map[lastPath]
if (!map[currentPath]) {
node = {
name: currentPath,
value: getValueCB(currentPath),
children: []
}
parent.children.push(node);
map[currentPath] = node;
}
stack.push(currentPath)
}
stack = stack.slice(0, 1)
}
return map[""].children[0];
}
function getFileSizeSync() {
return 200
}
var tree = buildTree(["/path/to/file1", "/path/to/file2"], function(path) {
return getFileSizeSync(path)
})
console.log (tree)
Here's the updated version that calculates the size recursively. (I can't put it into a snippet, that's why i leave the old one)
var fs = require('fs')
var Path = require ('path')
function calcSize (node) {
var children = node.children;
node.value = children.reduce (function (size, child) {
return size + child.value || reduceSize (child);
}, 0)
return node.value;
}
function getFileSizeSync(path) {
var size = fs.statSync(path).size
return size
}
function buildTree(pathes, getValueCB) {
var currentPath, lastPath, node, parent, map = {
"": {
children: []
}
},
stack = [""]
for (let path of pathes) {
let nodes = path.split(Path.sep);
for (let i = 0; i < nodes.length; i++) {
currentPath = Path.sep + nodes.slice(1, i + 1).join(Path.sep)
lastPath = stack[stack.length - 1]
parent = map[lastPath]
if (!map[currentPath]) {
node = {
name: currentPath,
value: getFileSizeSync(currentPath),
children: []
}
parent.children.push(node);
map[currentPath] = node;
}
stack.push(currentPath)
}
stack = stack.slice(0, 1)
}
calcSize (map[""])
return map[""].children[0];
}
var tree = buildTree(["/path/to/file1", "/path/to/file2"])
console.log (tree)