-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathotherDirection.js
488 lines (426 loc) · 15.3 KB
/
otherDirection.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
const fs = require('fs');
//lets try reading the csv file first to see the order of the headers
function getFiles(){
let fileList = [];
files = fs.readdirSync('./jsons');
files.forEach(function(file) {
if(file.match(/[0-9]{9}.json/) != null){
fileList.push(file);
}
});
return fileList;
}
function reverseRead(){
Array.prototype.unique = function() {
var a = this.concat();
for(var i=0; i<a.length; ++i) {
for(var j=i+1; j<a.length; ++j) {
if(a[i] === a[j])
a.splice(j--, 1);
}
}
return a;
};
let fileList = getFiles()
let jsonList = []
let jsonIndex = {}
for(let i = 0; i < fileList.length; i++){
let currFile = fileList[i];
let currFileContents = fs.readFileSync('./jsons/' + currFile );
let currJSON = JSON.parse(currFileContents)
jsonList.push(currJSON)
jsonIndex[currJSON['conceptId']] = i
}
let referenced = [];
let clean = [];
let cleancheck = [];
for(let i = 0; i < jsonList.length; i++){
let currFile = jsonList[i];
let keys = Object.keys(currFile);
for(let j = 0; j < keys.length; j++){
let nextObj = currFile[keys[j]]
if(keys[j].includes('Source')){
if(!clean.includes(i)){
clean.push(i)
cleancheck.push(i)
}
}
if(Array.isArray(nextObj)){
//iterate through array and look for json
let toReplace = []
for(let k = 0; k < nextObj.length; k++){
let toCheck = nextObj[k]
if(toCheck.match(/[0-9]{9}.json/) != null){
let cId = toCheck.substring(toCheck.length - 14, toCheck.length - 5)
toReplace.push(jsonList[jsonIndex[cId]])
if(!referenced.includes(jsonIndex[cId])){
referenced.push(jsonIndex[cId])
}
}
}
if(toReplace.length > 0){
currFile[keys[j]] = toReplace;
}
}
else if(typeof nextObj == 'string'){
if(nextObj.match(/[0-9]{9}.json/) != null){
let cId = nextObj.substring(nextObj.length - 14, nextObj.length - 5)
currFile[keys[j]] = jsonList[jsonIndex[cId]];
if(!referenced.includes(jsonIndex[cId])){
referenced.push(jsonIndex[cId])
}
}
//check if string is JSON
}
else{
let currKeys = Object.keys(nextObj)
let toReplace = {};
for(let k = 0; k < currKeys.length; k++){
if(currKeys[k].match(/[0-9]{9}.json/)){
//let cId = currKeys[k].substring(0,9);
let cId = currKeys[k].substring(currKeys[k].length - 14, currKeys[k].length - 5)
toReplace[nextObj[currKeys[k]]] = jsonList[jsonIndex[cId]];
if(!referenced.includes(jsonIndex[cId])){
referenced.push(jsonIndex[cId])
}
}
}
if(Object.keys(nextObj).length > 0){
currFile[keys[j]] = toReplace;
}
//check object for JSON
}
}
}
let cIds = [];
for(let i = 0; i < referenced.length; i++){
if(!cIds.includes(jsonList[referenced[i]]['conceptId'])){
cIds.push(jsonList[referenced[i]]['conceptId'])
}
}
for(let i = 0; i < jsonList.length; i++){
if(!cIds.includes(jsonList[i]['conceptId'])){
if(!clean.includes(i)){
clean.push(i);
}
}
}
let firstRow = ''
let firstRowSplit = [];
let finalFileName = ''
fs.readdirSync('./csv/').forEach(file => {
if(file.includes(".csv")){
finalFileName = file;
let currCSVContents = fs.readFileSync('./csv/' + file).toString('utf8');
firstRow = currCSVContents.substring(0,currCSVContents.indexOf('\n'))
firstRowSplit = CSVToArray(firstRow)
}
});
let finalMatrix = []
let finalHeader = []
let finalConcepts = []
let maxes = []
for(let i = 0; i < firstRowSplit.length; i++){
let head = firstRowSplit[i]
if(!head.includes('conceptId')){
finalHeader.push(head)
}
}
//change to make it recursive
for(let i = 0; i < clean.length; i ++){
let conceptSeen = [jsonList[clean[i]]['conceptId']]
let final = {}
recurseRead(jsonList[clean[i]],final, '', conceptSeen, 0)
finalMatrix.push(final)
//finalHeader.concat(Object.keys(final)).unique()
let keys = Object.keys(final)
let finalArr = []
let max = 0;
for(let j = 0; j < keys.length; j++){
if(!finalHeader.includes(keys[j]) && !finalConcepts.includes(keys[j])){
if(!keys[j].includes('conceptId')){
if(!keys[j].includes('subcollection') && !keys[j].includes('subcollections')){
finalHeader.push(keys[j])
}
}
else{
finalConcepts.push(keys[j])
}
}
if(final[keys[j]].length > max){
max = final[keys[j]].length
}
}
maxes.push(max)
//console.log(finalArr)
}
/*
let toExcel = "";
toExcel += keys.map(function(value){
if(value.indexOf(',') != -1){
return "\"" + value + "\"";
}
else if(value == '0'){
return ''
}
else{
return value;
}
}).join(",");
for(let j = 0; j < finalArr.length; j++){
toExcel += '\n'
toExcel += finalArr[j].map(function(value){
if(value.indexOf(',') != -1){
return "\"" + value + "\"";
}
else{
return value;
}
}).join(",");
}
//console.log(toExcel)
fs.writeFileSync('testOutput1.csv', toExcel)
*/
//
//reorder the finalHeader
//console.log(finalConcepts)
//try while organizing data: if key == subcollection, use single key from thing
let first = false;
for(let i = 0; i < finalHeader.length; i++){
let found = false;
for(let j = 0; j < finalConcepts.length; j++){
if(finalConcepts[j].includes(finalHeader[i])){
finalHeader.splice(i,0,finalConcepts[j])
i += 1;
j = finalConcepts.length
found =true
}
}
if(found == false && first == false){
finalHeader.splice(i,0,'conceptId')
i += 1;
first = true;
}
}
//console.log(finalHeader)
let toExcel = ''
toExcel += finalHeader.map(function(value){
if(value.indexOf('conceptId') != -1){
return 'conceptId'
}
if(value.indexOf(',') != -1){
return "\"" + value + "\"";
}
else if(value == '0'){
return ''
}
else{
return value;
}
}).join(",");
//console.log(finalMatrix[1])
for(let i =0 ; i < finalMatrix.length; i++){
let max = maxes[i]
let finalArr = [];
let currItem = finalMatrix[i]
for(let k = 0; k < max; k++){
let toInsert = []
for(let j = 0; j < finalHeader.length; j++){
toInsert.push('')
}
finalArr.push(toInsert);
}
for(let j = 0; j < finalHeader.length; j++){
let currKey = finalHeader[j]
if(currItem.hasOwnProperty(currKey)){
let currArr = currItem[currKey]
if(currKey != '0'){
for(let k = 0; k < currArr.length; k++){
finalArr[k][j] = currArr[k]
}
}
}
}
for(let j = 0; j < finalArr.length; j++){
toExcel += '\n'
toExcel += finalArr[j].map(function(value){
if(value.indexOf(',') != -1){
return "\"" + value + "\"";
}
else{
return value;
}
}).join(",");
}
}
if(finalFileName == ''){
fs.writeFileSync('./csv/output.csv', toExcel)
}
else{
fs.writeFileSync('./csv/' + finalFileName, toExcel)
}
}
function CSVToArray(strData){
strData = strData.trim();
let arr = [];
let finalPush = true;
while(strData.indexOf(",") != -1 ){
let toPush = "";
if(strData.substring(0,1) == "\""){
strData = strData.substring(1);
let nextLook = strData.indexOf('\"\"')
while(nextLook != -1){
console.log(nextLook)
toPush += strData.substring(0,nextLook) + '\"\"'
strData = strData.substring(strData.indexOf("\"\"") + 2);
nextLook = strData.indexOf('\"\"')
}
toPush += strData.substring(0, strData.indexOf("\""));
strData = strData.substring(strData.indexOf("\"") + 1);
strData = strData.substring(strData.indexOf(',')+1)
if(strData.trim() == ''){
finalPush = false
}
}
else{
toPush = strData.substring(0, strData.indexOf(','));
strData = strData.substring(strData.indexOf(',') + 1)
}
arr.push(toPush)
//let nextQuote = strData.indexOf("\"")
}
if(finalPush == true){
arr.push(strData);
}
// Return the parsed data.
return( arr );
}
function recurseRead(curr,final, key, conceptSeen, /*isSource,*/ depth){
let keys = Object.keys(curr)
let toPrint = []
if(curr.hasOwnProperty('conceptId') && key != ''){
let nextObj = curr['conceptId']
if(key == 'subcollections' || key == 'subcollection'){
let found = -1;
let firstWithoutconceptId = -1;
for(let i = 0; i < keys.length; i++){
if(final.hasOwnProperty('conceptId' + keys[i]) && keys[i] != '' && keys[i] != 'subcollections'&& keys[i] != 'subcollection' && found == -1){
found = i;
}
if(!keys[i].includes('conceptId') && firstWithoutconceptId == -1 && keys[i] != 'subcollections'&& keys[i] != 'subcollection'){
firstWithoutconceptId = i
}
}
let toChange = ''
if(found == -1){
toChange = keys[firstWithoutconceptId]
}
else{
toChange = keys[found]
}
if(final.hasOwnProperty('conceptId' + toChange)){
if(!final['conceptId' + toChange].includes(nextObj)){
final['conceptId' + toChange].push(nextObj)
}
}
else{
final['conceptId' + toChange] = [nextObj]
}
}
else{
if(final.hasOwnProperty('conceptId' + key)){
if(!final['conceptId' + key].includes(nextObj)){
final['conceptId' + key].push(nextObj)
}
}
else{
final['conceptId' + key] = [nextObj]
}
}
}
for(let j = 0; j < keys.length; j++){
let nextObj = curr[keys[j]]
if(Array.isArray(nextObj)){
let arr = []
for(let k = 0; k <nextObj.length; k++){
if(typeof nextObj[k] != 'string'){
if(!conceptSeen.includes(nextObj[k]['conceptId'])){
conceptSeen.push(nextObj['conceptId'])
if(!key.includes('Source')){
let returned = recurseRead(nextObj[k], final, keys[j], conceptSeen, 1)
arr.push(returned)
}
}
}
else{
//console.log(JSON.stringify(nextObj))
}
}
//console.log(keys[j])
//console.log(arr)
}
else if(typeof nextObj == 'string'){
if(keys[j] != 'conceptId' && key != 'conceptId'){
if(key == '' || key == 'subcollection'){
if(final.hasOwnProperty(keys[j])){
if(!final[keys[j]].includes(nextObj)){
final[keys[j]].push(nextObj)
}
}
else{
final[keys[j]] = [nextObj]
}
//toPrint.push(keys[j] + ':' + nextObj)
//console.log(keys[j] + ': ' + nextObj)
}
else{
if(final.hasOwnProperty(key)){
if(!final[key].includes(nextObj)){
final[key].push(nextObj)
}
}
else{
final[key] = [nextObj]
}
//toPrint.push(key + ':' + nextObj)
//console.log(key + ': ' + nextObj)
}
}
else if(keys[j] == 'conceptId' && key == ''){
if(final.hasOwnProperty('conceptId' + key)){
if(!final['conceptId' + key].includes(nextObj)){
final['conceptId' + key].push(nextObj)
}
}
else{
final['conceptId' + key] = [nextObj]
}
}
}
else{
if(!nextObj.hasOwnProperty('conceptId') || !conceptSeen.includes(nextObj['conceptId'])){
if(nextObj.hasOwnProperty('conceptId')){
conceptSeen.push(nextObj['conceptId'])
recurseRead(nextObj, final, keys[j], conceptSeen, 2)
}
else{
let kList = Object.keys(nextObj);
for(let k = 0; k < kList.length; k++){
if(nextObj[kList[k]].hasOwnProperty('Variable Name') && !nextObj[kList[k]]['Variable Name'].includes('=')){
nextObj[kList[k]]['Variable Name'] = kList[k] + '=' + nextObj[kList[k]]['Variable Name']
}
if(nextObj[kList[k]].hasOwnProperty('conceptId') && !conceptSeen.includes(nextObj[kList[k]]['conceptId'])){
conceptSeen.push(nextObj[kList[k]]['conceptId'])
console.log('FOUND')
recurseRead(nextObj[kList[k]], final, keys[j], conceptSeen, depth + 1)
}
}
}
}
}
}
//console.log(toPrint)
}
module.exports = {
reverseRead: reverseRead
}