-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.js
35 lines (28 loc) · 1.03 KB
/
convert.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
const fs = require('fs');
const dir = require('node-dir');
const dirRawFiles = 'raw';
const combinedRaw = 'result/_combined-raw.json';
let tempArr = [];
dir.readFiles(dirRawFiles,{
match: /.json$/, // match only filenames with a .json extension
exclude: /^\./ // exclude filenames that starts with a '.' (dot)
},
function(err, content, next) { // read per instance/file
if (err) throw err;
tempArr = tempArr.concat(JSON.parse(content));
next();
},
function(err, files){ // on finish reading all
if (err) throw err;
fs.writeFileSync(combinedRaw, JSON.stringify(tempArr,null,2));
const final = tempArr.reduce((ret, o) => {
const {stock, date, ...props} = o
const items = ret[stock] || {}
items[date] = props
ret[stock] = items
return ret
}, {});
const outFileName = process.argv[2];
const finalArr = [final];
fs.writeFileSync(outFileName, JSON.stringify(finalArr,null,2));
});