-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
151 lines (124 loc) · 2.7 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
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
const fs = require("fs");
const yieldableJson = require("yieldable-json");
console.log("Running Demo . . .");
runAsyncJsonDemo();
// Main function.
function runAsyncJsonDemo()
{
coordinateDemo(function (overallErr)
{
if (overallErr !== null)
{
displayResult(overallErr.message);
}
else
{
displayResult("Complete");
}
});
}
// Read input JSON file.
function coordinateDemo(coordCallback)
{
console.log("Reading");
fs.readFile("./input.json", "utf8", function (readErr, rawInputText)
{
if (readErr !== null)
{
// Error reading file.
return coordCallback(readErr);
}
else
{
// Successful - Parse raw text.
callJsonParse(rawInputText, coordCallback);
}
});
}
// Parse input JSON.
function callJsonParse(rawTxt, parseCallback)
{
var parseMsg = "";
yieldableJson.parseAsync(rawTxt, function (parseErr, parsedJsonObject)
{
if (parseErr !== null)
{
// JSON parse error.
parseMsg = writeJsonErrorText("parsing", parseErr.message);
return parseCallback(new Error(parseMsg));
}
else
{
// Successful - Process object.
handleProcessing(parsedJsonObject, parseCallback);
}
});
}
function handleProcessing(parsedObject, processCallback)
{
console.log("Processing");
// JSON processing goes here.
var waitMs = chooseDelayLength();
// Random delay.
setTimeout(function()
{
// Processing complete.
callJsonStringify(parsedObject, processCallback);
}, waitMs);
}
// Stringify JSON object.
function callJsonStringify(parsedObj, stringCallback)
{
var stringifyMsg = "";
console.log("Saving");
yieldableJson.stringifyAsync(parsedObj, null, 4, 1,
function (stringErr, definitionText)
{
if (stringErr !== null)
{
// JSON stringify error.
stringifyMsg = writeJsonErrorText("stringifying", stringErr.message);
return stringCallback(new Error(stringifyMsg));
}
else
{
// Successful - Save output file.
callOutputSave(definitionText, stringCallback);
}
});
}
// Write output JSON file.
function callOutputSave(defTxt, saveCallback)
{
fs.writeFile("./output.json", defTxt, function (saveErr)
{
// Finished.
return saveCallback(saveErr);
});
}
// Displays result message.
function displayResult(resMsg)
{
console.log("");
console.log(resMsg);
}
// Random processing delay length in milliseconds.
function chooseDelayLength()
{
var minDelay = 1000;
var maxDelay = 5000;
var diffAmount = maxDelay - minDelay;
var randSeed = Math.random() * diffAmount;
var delayRes = Math.round(minDelay + diffAmount);
return delayRes;
}
// Yieldable JSON error text.
function writeJsonErrorText(vAction, vMsg)
{
var writeRes = "";
writeRes += "Error ";
writeRes += vAction;
writeRes += " JSON - ";
writeRes += vMsg;
return writeRes;
}