forked from khtdr/treeify-paths
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paths-to-tree.js
46 lines (46 loc) · 1.17 KB
/
paths-to-tree.js
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
"use strict";
var Node = (function () {
function Node(path) {
if (path === void 0) { path = ''; }
this.path = path;
this.name = '';
this.children = [];
}
return Node;
}());
exports.Node = Node;
;
function fill(node, paths) {
var cMap = {};
paths.forEach(function (file) {
var parts = file.split('/');
if (!cMap[parts[0]]) {
var fullPath = node.path + '/' + parts[0];
cMap[parts[0]] = {
paths: [],
obj: new Node(fullPath.replace(/^\//, ''))
};
}
if (parts.length == 1) {
cMap[parts[0]].obj.name = parts[0];
}
else {
var dir = parts.shift();
var rest = parts.join('/');
cMap[dir].paths.push(rest);
}
});
var keys = Object.keys(cMap);
keys.sort();
keys.forEach(function (key) {
fill(cMap[key].obj, cMap[key].paths);
node.children.push(cMap[key].obj);
});
return node;
}
function treeifyPaths(paths) {
if (paths === void 0) { paths = []; }
return fill(new Node, paths);
}
exports.__esModule = true;
exports["default"] = treeifyPaths;