forked from DigitasLBi/node-analytics-json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
116 lines (90 loc) · 2.3 KB
/
index.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
#! /usr/bin/env node
var dtas = dtas || {};
dtas.json = (function() {
var fs = require('fs'),
util = require('util'),
hash = require('string-hash');
function init() {
process.argv.forEach(function (val, index, array) {
switch(val) {
case '-c':
clean(array[index + 1]);
break;
case '-v':
convert(array[index + 1]);
break;
case '-h':
help();
break;
default:
}
});
}
function convert(file) {
var Converter = require("csvtojson").core.Converter;
var csvConverter = new Converter();
csvConverter.on("end_parsed", function(jsonObj) {
cleanUpJSON('temp.json', jsonObj, false);
});
csvConverter.from(file);
}
function clean(file) {
var data = readJSON(file, cleanUpJSON);
}
function cleanUpJSON(file, data, parse) {
data = (parse === true) ? removeNulls(JSON.parse(data)) : removeNulls(data);
//TODO Need to remove this and add elsewhere as an option
data = convertToHashMap(data);
writeJSON(file + '.tmp', data);
function removeNulls(obj) {
var isArray = obj instanceof Array;
for (var k in obj) {
if (obj[k] === null || obj[k] === 'null' || obj[k] === '') {
isArray ? obj.splice(k, 1) : delete obj[k];
}
else if (typeof obj[k] === "object") {
removeNulls(obj[k]);
}
}
return obj;
}
}
function convertToHashMap(json) {
var arr = json.csvRows,
len = arr.length,
map = {};
for (var i = 0; i < len; i++) {
obj = arr[i];
if (obj.Description) {
map[hash(obj.Description)] = obj;
}
}
return map;
}
function readJSON(file, callback) {
fs.readFile(file, 'utf8', function (err, data) {
if (err) {
console.log('Error: ' + err);
return;
}
callback(file, data, true);
});
}
function writeJSON(path, data) {
fs.writeFile(path, JSON.stringify(data, null, 2), function(err) {
if (err) {
console.log(err);
}
else {
console.log("The file was saved!");
}
});
}
function help() {
console.log('Sorry, there is no help yet!');
}
return {
init: init
};
}());
dtas.json.init();