Create nested object of file paths

前端 未结 1 1853
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-25 13:49

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

相关标签:
1条回答
  • 2021-01-25 14:21

    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)
    
    0 讨论(0)
提交回复
热议问题