refactor: use set instead of array

This commit is contained in:
Namekuji 2023-08-22 09:13:49 -04:00
parent 5b6f8bebf5
commit 64aaff1f3d
No known key found for this signature in database
GPG key ID: 1D62332C07FBA532

View file

@ -82,7 +82,7 @@ export default define(meta, paramDef, async () => {
const tags: { const tags: {
name: string; name: string;
users: Note["userId"][]; users: Set<Note["userId"]>;
}[] = []; }[] = [];
for (const [_, fields] of tagNotes) { for (const [_, fields] of tagNotes) {
@ -91,16 +91,16 @@ export default define(meta, paramDef, async () => {
if (hiddenTags.includes(name)) continue; if (hiddenTags.includes(name)) continue;
const index = tags.findIndex((tag) => tag.name === name); const index = tags.findIndex((tag) => tag.name === name);
if (index >= 0 && !tags[index].users.includes(userId)) { if (index >= 0) {
tags[index].users.push(userId); tags[index].users.add(userId);
} else if (index < 0) { } else {
tags.push({ name, users: [userId] }); tags.push({ name, users: new Set([userId]) });
} }
} }
// Sort tags by their popularity // Sort tags by their popularity
const hots = tags const hots = tags
.sort((a, b) => b.users.length - a.users.length) .sort((a, b) => b.users.size - a.users.size)
.map((tag) => tag.name) .map((tag) => tag.name)
.slice(0, max); .slice(0, max);
@ -108,7 +108,7 @@ export default define(meta, paramDef, async () => {
const stats = hots.map((tag, i) => ({ const stats = hots.map((tag, i) => ({
tag, tag,
chart: [], // FIXME: generate chart chart: [], // FIXME: generate chart
usersCount: tags[i].users.length, usersCount: tags[i].users.size,
})); }));
return stats; return stats;