Skip to content

Commit 58a00db

Browse files
committed
Filter top contries using ?top=
1 parent f9a0ec7 commit 58a00db

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

app.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ app.use(morgan(':remote-addr :remote-user :method :url :status :res[content-leng
1919

2020
app.get('/', (req, res) => {
2121
const format = req.query.format ? req.query.format : '';
22+
const top = req.query.top ? Number(req.query.top) : 300;
2223

2324
if (format.toLowerCase() === 'json') {
2425
return getJSONData().then(result => {
@@ -27,7 +28,7 @@ app.get('/', (req, res) => {
2728
}).catch(error => errorHandler(error, res));
2829
}
2930

30-
return getCompleteTable().then(result => {
31+
return getCompleteTable(max=top).then(result => {
3132
res.setHeader('Cache-Control', 's-maxage=900');
3233
return res.send(result);
3334
}).catch(error => errorHandler(error, res));

lib/corona.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ function extraStats(dataArr) {
7979
);
8080
}
8181

82-
exports.getCompleteTable = async (emojis = false) => {
82+
exports.getCompleteTable = async (max = false, emojis = false) => {
8383
const head = [
8484
'',
8585
'Country',
@@ -121,7 +121,7 @@ exports.getCompleteTable = async (emojis = false) => {
121121
]
122122
})
123123
let rank = 1;
124-
countryData.forEach(cd => {
124+
countryData.some(cd => {
125125
const countryEmoji = getEmoji(cd.countryCode);
126126
const values = [
127127
getCountry(`${cd.country} (${cd.countryCode})`),
@@ -137,6 +137,7 @@ exports.getCompleteTable = async (emojis = false) => {
137137
...(emojis ? [countryEmoji] : [])
138138
]
139139
table.push({ [rank++]: values })
140+
return rank == max+1
140141
});
141142
const lastUpdated = countryData[0].lastUpdated;
142143
return table.toString() + footer(lastUpdated);

0 commit comments

Comments
 (0)