From d43c572311cc85726f452016413665def87d279e Mon Sep 17 00:00:00 2001 From: pheralb Date: Thu, 22 Aug 2024 16:49:34 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9A=99=EF=B8=8F=20Fix=20show=20categories=20?= =?UTF-8?q?data?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api-routes/src/index.ts | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/api-routes/src/index.ts b/api-routes/src/index.ts index f4ceccc..cddb391 100644 --- a/api-routes/src/index.ts +++ b/api-routes/src/index.ts @@ -107,21 +107,23 @@ app.get('/categories', async (c) => { return c.json({ error: 'Too many request' }, 429); } - const categories = fullRouteSvgsData.reduce((acc, svg) => { + const categoryTotals: Record = {}; + + fullRouteSvgsData.forEach((svg) => { if (typeof svg.category === 'string') { - if (!acc.includes(svg.category)) { - acc.push(svg.category); - } - } - if (Array.isArray(svg.category)) { + categoryTotals[svg.category] = (categoryTotals[svg.category] || 0) + 1; + } else if (Array.isArray(svg.category)) { svg.category.forEach((category) => { - if (!acc.includes(category)) { - acc.push(category); - } + categoryTotals[category] = (categoryTotals[category] || 0) + 1; }); } - return acc; - }, [] as string[]); + }); + + const categories = Object.entries(categoryTotals).map(([category, total]) => ({ + category, + total + })); + return c.json(categories); });