forked from wesbos/favicon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.ts
More file actions
28 lines (25 loc) · 895 Bytes
/
db.ts
File metadata and controls
28 lines (25 loc) · 895 Bytes
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
export const db = await Deno.openKv();
export function incrementCount(emoji: string) {
const VIEW_KEY = [`favicon`, `${emoji}`];
// also Update + query via Deno KV
return db.atomic().sum(VIEW_KEY, 1n).commit(); // Increment KV by 1
}
export async function getEmojiCounts() {
const counts = db.list<bigint>({ prefix: ["favicon"] });
const emojis = [];
for await (const count of counts) {
emojis.push([count.key[1], Number(count.value)]);
}
emojis.sort((a, b) => b[1] - a[1]);
const totalCount = emojis.reduce((acc, [_, count]) => acc + count, 0);
// Filter out country flags
const [topEmojis, countryEmojis] = emojis.reduce((acc, [emoji, count]) => {
if (emoji.match(/[🇦-🇿]{2}/u)) {
acc[1].push([emoji, count]);
} else {
acc[0].push([emoji, count]);
}
return acc;
}, [[], []]);
return { topEmojis, countryEmojis, totalCount };
}