From 642cbbf6d31d5801500b454c1c84686f60d3769f Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Fri, 6 Jan 2023 18:50:42 +0400 Subject: [PATCH] Fix star count is displaying NaN --- src/lib/github.ts | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/lib/github.ts b/src/lib/github.ts index 5cb614458..4a3a2e818 100644 --- a/src/lib/github.ts +++ b/src/lib/github.ts @@ -2,6 +2,7 @@ const formatter = Intl.NumberFormat('en-US', { notation: 'compact', }); +const defaultStarCount = 224000; let starCount: number | undefined = undefined; export async function countStars( @@ -15,10 +16,10 @@ export async function countStars( const repoData = await fetch(`https://api.github.com/repos/${repo}`); const json = await repoData.json(); - starCount = json.stargazers_count * 1; + starCount = json.stargazers_count * 1 || defaultStarCount; } catch (e) { console.log('Failed to fetch stars', e); - starCount = 224000; + starCount = defaultStarCount; } return starCount; @@ -27,11 +28,7 @@ export async function countStars( export async function getFormattedStars( repo = 'kamranahmedse/developer-roadmap' ): Promise { - if (import.meta.env.DEV) { - return '224k'; - } - - const stars = await countStars(repo); + const stars = import.meta.env.DEV ? defaultStarCount : await countStars(repo); return formatter.format(stars); }