-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
36 lines (32 loc) · 1018 Bytes
/
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
const lineByLine = require('n-readlines')
const program = require('commander')
function sortScores(filename, n) {
const scoreLines = new lineByLine(filename);
let output = [];
let scoreLine;
while (scoreLine = scoreLines.next()) {
if (scoreLine) {
let [ score, ...blob ] = String(scoreLine).split(': ');
blob = blob.join(': ');
try {
blob = JSON.parse(blob);
if (blob && blob.id) {
output.push({ id: blob.id, score: parseInt(score) })
}
} catch (err) {
return 2;
}
}
}
output = output.sort((a, b) => { return a.score - b.score }).slice(0, n).reverse();
return JSON.stringify(output);
}
module.exports = { sortScores: sortScores };
program
.version('0.1.0')
.arguments('[path] [n]')
.action((path, n) => {
const output = sortScores(path, n);
console.log(output);
});
program.parse(process.argv);