-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
69 lines (65 loc) · 2.62 KB
/
main.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
const express = require('express');
const stock = require("./stock");
const operation = process.argv.length == 3? process.argv[2] : 'cmd';
if(operation == 'web'){
const app = express();
const port = 3000;
app.get('/', (req, res) => {
let tableBody = `
<thead>
<th>Company Name</td>
<th>High</td>
<th>Low</td>
<th>Latest Price</td>
<th>Prev Close</td>
<th>Change</td>
<th>% Gain/Loss</td>
<th>Gain/lost since last run</td>
</thead>
`;
stock.invoke().then((data)=>{
tableBody += "<tbody>"
Object.keys(data).map((companyName)=>{
tableBody += `
<tr style="background-color:${
typeof data[companyName]['Gain/lost since last run'] === 'string' ? 'white' :
data[companyName]['Gain/lost since last run'] < 0 ? "red":
data[companyName]['Gain/lost since last run'] == 0 ? "white": "green"
}">
<td>${companyName}</td>
<td style="text-align:right;">${data[companyName]['High']}</td>
<td style="text-align:right;">${data[companyName]['Low']}</td>
<td style="text-align:right;">${data[companyName]['Latest Price']}</td>
<td style="text-align:right;">${data[companyName]['Prev Close']}</td>
<td style="text-align:right;">${data[companyName]['Change']}</td>
<td style="text-align:right;">${data[companyName]['% Gain/Loss']}</td>
<td style="text-align:right;">${data[companyName]['Gain/lost since last run']}</td>
</tr>
`;
});
tableBody += "</tbody>";
res.send(`
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
padding: 3px;
}
</style>
<table>
${tableBody}
</table>
`);
})
})
app.listen(port, () => {
console.log(`Please visit http://localhost:${port}`);
})
}else{
stock.invoke().then((data)=>{
const finalResponse = Object.keys(data).map((companyName)=>{
return { "Company Name": companyName, ...data[companyName] }
});
console.table(finalResponse, ["Company Name","High","Low","Latest Price","Prev Close","Change","% Gain/Loss","Gain/lost since last run"]);
})
}