-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (59 loc) · 1.69 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
//Parse map json and input json. Create output JSON as per activity stream v2.0
let Q = require("q");
let fs = require("fs");
let readline = require("readline");
const mapjsonfilename = "map.json";
const inputjsonfile = "input.json";
function fread(filename) {
var deferred = Q.defer();
fs.readFile(filename, "utf-8", function (error, text) {
if (error) {
deferred.reject(new Error(error));
} else {
deferred.resolve(text);
}
});
return deferred.promise;
}
let mapobj, inputobj, outputobj, outvals = [];
fread(mapjsonfilename).then(function(text) {
mapobj = JSON.parse(text);
console.log("Mapped format:\n");
console.log(mapobj);
readinputjson(inputjsonfile);
});
function readinputjson(filename) {
fread(filename).then(function(text) {
inputobj = JSON.parse(text);
console.log("\nInput JSON:\n")
console.log(inputobj);
getmapvalues(mapobj, inputobj, outputobj, outvals).then(function() {
let obj = createOutputJson(mapobj, outvals);
console.log("\noutput object\n");
console.log(obj);
});
});
}
function getmapvalues(mapobj, inputobj, outputobj, outvals) {
var deferred = Q.defer();
console.log("\nParsed data: \n")
Object.keys(mapobj).forEach(function(key) {
let val = mapobj[key];
let newval = inputobj[val];
outvals.push(newval);
console.log(val + ' : ' + newval);
});
deferred.resolve();
return deferred.promise;
}
function getObjectkeys(obj) {
return Object.keys(obj);
}
function createOutputJson(mapobj, outvals) {
let outarray = getObjectkeys(mapobj);
let newobj = { };
for(let ind=0; ind < outarray.length; ind++) {
newobj[outarray[ind]] = outvals[ind];
}
return newobj;
}