-
Notifications
You must be signed in to change notification settings - Fork 0
/
getTop.js
71 lines (57 loc) · 1.64 KB
/
getTop.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
const fetch = require("node-fetch");
const { format, parseISO } = require("date-fns");
function customReverseSort(b, a) {
if (a[1] > b[1]) return 1;
if (a[1] < b[1]) return -1;
return 0;
}
function addToMap(map, item) {
if (map.has(item)) {
map.set(item, map.get(item) + 1);
} else {
map.set(item, 1);
}
return map;
}
function sortSliceMap(map, n) {
return new Map([...map.entries()].sort(customReverseSort).slice(0, n));
}
async function getReport() {
const result = await fetch(
`https://dev.to/api/articles?top=1000&per_page=1000`
);
const articles = await result.json();
const formattedArticles = articles.map(({ published_at, tag_list }) => {
return {
published_at,
tag_list,
};
});
const topTags = formattedArticles.reduce((acc, { tag_list }) => {
const tags = tag_list;
for (tag of tags) {
acc = addToMap(acc, tag);
}
return acc;
}, new Map());
const topDays = formattedArticles.reduce((acc, { published_at }) => {
const day = format(parseISO(published_at), "eeee");
acc = addToMap(acc, day);
return acc;
}, new Map());
const topHour = formattedArticles.reduce((acc, { published_at }) => {
const hour = format(parseISO(published_at), "H");
acc = addToMap(acc, hour);
return acc;
}, new Map());
const topFiveTags = sortSliceMap(topTags, 5);
const topThreeDays = sortSliceMap(topDays, 3);
const topFiveHours = sortSliceMap(topHour, 5);
console.log("Top Five Tags");
console.log(topFiveTags);
console.log("\nTop Three Days");
console.log(topThreeDays);
console.log("\nTop Five Hours");
console.log(topFiveHours);
}
getReport();