title: Finding all unique paths in tree structure
body: <p>Let's say I have a directory structure laid out in a text file</p>
  
  <pre><code>root1
      child1
      child2
          grandchild1
          grandchild2
      child3
  root2
      child1
      child2
          grandchild1
              greatgrandchild1
  </code></pre>
  
  <p>How can I turn the above tree structure into a nested array that looks like this:</p>
  
  <pre><code>[
      [ "root1", "child1" ],
      [ "root1", "child2", "grandchild1" ],
      [ "root1", "child2", "grandchild2" ],
      [ "root1", "child3" ],
      [ "root2", "child1" ],
      [ "root2", "child2", "grandchild1", "greatgrandchild1" ]
  ]
  </code></pre>
  
  <p><strong>edit</strong></p>
  
  <p>Getting further but still having issues walking through the tree recursively</p>
  
  <pre><code>var $text = ''
  + 'root1\n'
  + '  r1 child1\n'
  + '  r1 child2\n'
  + '    r1 grandchild1\n'
  + '    r1 grandchild2\n'
  + '  r1 child3\n'
  + 'root2\n'
  + '  r2 child1\n'
  + '    r2 c1\n'
  + '      r2 c1 g1\n'
  + '  r2 child2\n'
  + '    r2 grandchild1\n'
  + '      r2 greatgrandchild1\n'
  + 'test!\n'
  + 'root3\n'
  + '  r3 child1\n'
  + '    r3 c1\n'
  + '      r3 c1 g1\n'
  + '  r3 child3\n'
  + '    r3 grandchild1\n'
  + '      r3 greatgrandchild1';
  
  var dirGen = (function(trees) {
    "use strict";
  
    var indent = /[\s\t]/g;
    var lTrim = /[\s\t]*/;
    var $trees = decompose(trees);
    var $root = [];
  
    function init() {
      var paths = $trees.map(treeMap)
      $test(paths);
    }
  
    function treeMap(tree, n, arr) {
      var base = new LinkedList();
      return bfs(-1, tree, base);
    }
  
    function bfs(n, tree, base) {
      var l, t;
      n++;
      //base case
      if (n === tree.length) return trails(base);
      l = tree.length;
      t = tree[n];
      var cur = { label: t.replace(lTrim, ""), depth: depth(t), children: [] };
      //set root
      if (n === 0) {
        base.tree = cur;
        return bfs(n, tree, base);
      }
  
      base.push(cur);
  
      return bfs(n, tree, base);
  
    }
  
    function depth(str) {
      var d = str.match(indent);
      if (d === null) return 0;
      return d.length;
    }
  
    function trails(arr) {
      return arr;
    }
  
    function LinkedList() {}
  
    LinkedList.prototype.push = function(node) {   
      var l = this.tree.children.length;
      var j = l - 1;
      if (l === 0) {
        this.tree.children.push(node);
        return;
      }
      //walk through children array in reverse to get parent
      while (j &gt; -1) {
        var d = this.tree.children[j].depth;
        //child
        if (node.depth &gt; d) {
          console.log(this.tree.children[j], node)
          return this.tree.children[j].children.push(node);
        }
        //sibling
        if (node.depth === d) {
  
        }
  
        j--;
      }
  
    }
  
    function decompose(arr) {
      var treeBreak = /[\r\n](?=\w)/gm;
      var lines = /[\r\n]+(?!\s)*/g; 
      return arr.split(treeBreak).map(function(str) {
        return str.split(lines)
      });
    }
  
    function $test(str) {
      var json = JSON.stringify(str, null, 2);
      var wtf = "&lt;pre&gt;" + json + "&lt;/pre&gt;";
      document.write(wtf);
    }
  
    return init;
  
  })($text);
  
  dirGen();
  </code></pre>
  
  <p>The code so far gets me this json array:</p>
  
  <p><a href="https://i.stack.imgur.com/czWdh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/czWdh.png" alt="enter image description here"></a></p>