-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.js
More file actions
139 lines (116 loc) · 2.44 KB
/
parser.js
File metadata and controls
139 lines (116 loc) · 2.44 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
console.log("-- Neo4j Import Nodes --");
//TEST-FILE
var import_data = [
{
"id":"1",
"cname1":"USA",
"cname2":"SWE",
"ccode1":"2",
"ccode2":"20",
"year":"2009",
"trade":"228376",
"continent_name":"America",
"continent_id":"1"
},
{
"id":"2",
"ccode1":"3",
"ccode2":"30",
"cname1":"CAN",
"cname2":"DEN",
"year":"2009",
"trade":"228376",
"continent_name":"America",
"continent_id":"1"
},
{
"id":"3",
"cname1":"MEX",
"cname2":"USA",
"ccode1":"4",
"ccode2":"2",
"year":"2009",
"trade":"228376",
"contname1":"America",
"continent_id":"1"
}
];
var config_nodes = [
{
"name":"$cname1",
"type":"country",
"code":"$ccode1",
},
{
"name":"$cname2",
"code":"$ccode2",
"type":"country"
}
];
var config_relationships = [];
function checkIfNodeVariable(string){
if(string.charAt(0) == "$"){
return true;
}else{
return false;
};
};
function checkIfEmpty(array){
//TODO
return false;
};
function sortObjectKeys(o) {
var sorted = {},
key, a = [];
for (key in o) {
if (o.hasOwnProperty(key)) {
a.push(key);
}
}
a.sort();
for (key = 0; key < a.length; key++) {
sorted[a[key]] = o[a[key]];
}
return sorted;
};
function removeDuplicates(arr) {
var i,
len=arr.length,
out=[],
obj={};
for (i=0;i<len;i++) {
obj[JSON.stringify(arr[i])]=0;
}
for (i in obj) {
out.push(JSON.parse(i));
}
return out;
}
function createNodesFromData(import_data, config_nodes){
var new_nodes = [];
config_nodes.forEach(function(node){
import_data.forEach(function(data){
var new_node = {};
for(var node_key in node){
if(checkIfNodeVariable(node[node_key])){
for(var data_key in data){
if(node[node_key].substr(1) == data_key){
new_node[node_key] = data[data_key];
};
};
}else{
new_node[node_key] = node[node_key];
};
};
if(!(checkIfEmpty(new_node))){
new_nodes.push(sortObjectKeys(new_node));
};
});
});
// Remove duplicates
new_nodes = removeDuplicates(new_nodes);
return new_nodes;
};
//Create all new nodes
var new_nodes = createNodesFromData(import_data, config_nodes);
console.log("pre-sort: " + JSON.stringify(new_nodes)); //Test