-
Notifications
You must be signed in to change notification settings - Fork 0
/
datastructure.js
86 lines (68 loc) · 2.71 KB
/
datastructure.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"use strict";
var fs = require('fs');
var readline = require('readline');
var i;
var line;
// Need to convert this to STDIN
// Read data from STDIN
// STDIN must be comma separated data with the following columns:
// userid,parentid,childid,childname,idirectory
var lines = fs.readFileSync('./examples/hierarchy6.csv').toString().split("\n");
// Datastructure objects
var hierarchy_pc = {}; // Intermediary object to store parent to child relationship
var hierarchy = {}; // Final object to store parent to child relationship
/*#-----------------------------------------------------------------------------
# Step 1 of 3
#
# Read lines from STDIN
# STDIN must be comma separated data with the following columns:
# userid,parentid,childid,childname,idirectory
#
#-----------------------------------------------------------------------------*/
for(i in lines) {
line = lines[i];
var line_split = line.split(",");
var userid = line_split[0];
var parentId = line_split[1];
var childId = line_split[2];
var childName = line_split[3];
var isDirectory = line_split[4];
hierarchy_pc[parentId] = {};
hierarchy_pc[parentId].children = {};
hierarchy_pc[parentId].children[childId] = JSON.stringify( {"name": childName, "type": isDirectory} );
}
/*----------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------
# Step 2 of 3 (don't work correctly)
#
# Determine if a child is also a parent; if so, add it to $hierarchy
#
#-----------------------------------------------------------------------------*/
for (var parentId in hierarchy_pc) {
for (var childId in hierarchy_pc[parentId].children) {
hierarchy[parentId] = {};
hierarchy[parentId].children = {};
// Copy $hierarchy_pc to $hierarchy
hierarchy[parentId].children = hierarchy_pc[parentId].children;
// Is child $childId also a parent ?
if (hierarchy[childId] != null) {
hierarchy[parentId].children[childId] = {};
hierarchy[parentId].children[childId].children = {};
hierarchy[parentId].children[childId].children = hierarchy_pc[childId].children;
}
}
}
/*----------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------
# Step 3 of 3
#
# Delete keys whose parent is not <HOME>. They are aliens.
#
#-----------------------------------------------------------------------------*/
for (var parentId in hierarchy ) {
if( parentId.match(/\<HOME\>/i) == null ) {
delete hierarchy[parentId];
}
}
/*----------------------------------------------------------------------------*/
console.log(hierarchy);