Commit 31832e2

Anton Medvedev <anton@medv.io>
2023-10-12 13:57:27
Refactor stars
1 parent de828e5
Changed files (1)
src
all-badges
src/all-badges/stars/stars.ts
@@ -1,4 +1,5 @@
 import { BadgePresenter, Present } from '../../badges.js'
+import { Repo } from '../../collect/collect.js'
 
 export default new (class implements BadgePresenter {
   url = new URL(import.meta.url)
@@ -13,47 +14,79 @@ export default new (class implements BadgePresenter {
     'stars-20000',
   ] as const
   present: Present = (data, grant) => {
-    let totalStars = 0
-
-    const sorted = data.repos.sort(
-      ({ stargazers_count: a = 0 }, { stargazers_count: b = 0 }) => b - a,
-    )
-    const reasonable = sorted
-      .map((repo) => {
-        if (!repo.stargazers_count) {
-          return
-        }
-        totalStars += repo.stargazers_count
+    const buckets: Record<(typeof this.badges)[number], Repo[]> = {
+      'stars-100': [],
+      'stars-500': [],
+      'stars-1000': [],
+      'stars-2000': [],
+      'stars-5000': [],
+      'stars-10000': [],
+      'stars-20000': [],
+    }
 
-        return `* <a href="https://github.com/${repo.owner.login}/${repo.name}     <i>${repo.stargazers_count}</i></a>`
-      })
-      .filter(Boolean)
+    const repos = data.repos.sort(asc).filter(withStars)
+    let totalStars = 0
 
-    const text = `Repos:
-${reasonable.join('\n')}
+    for (const repo of repos) {
+      totalStars += repo.stargazers_count || 0
+    }
 
-<sup>I have push, maintainer or admin permissions, so I'm definitely an author.<sup>
-`
     if (totalStars >= 100) {
-      grant('stars-100', `I collected 100 stars.`).evidence(text).tier(1)
+      grant('stars-100', `I collected 100 stars.`)
+        .evidence(text(repos, 100))
+        .tier(1)
     }
     if (totalStars >= 500) {
-      grant('stars-500', 'I collected 500 stars.').evidence(text).tier(2)
+      grant('stars-500', 'I collected 500 stars.')
+        .evidence(text(repos, 500))
+        .tier(2)
     }
     if (totalStars >= 1000) {
-      grant('stars-1000', 'I collected 1000 stars.').evidence(text).tier(3)
+      grant('stars-1000', 'I collected 1000 stars.')
+        .evidence(text(repos, 1000))
+        .tier(3)
     }
     if (totalStars >= 2000) {
-      grant('stars-2000', 'I collected 2000 stars.').evidence(text).tier(4)
+      grant('stars-2000', 'I collected 2000 stars.')
+        .evidence(text(repos, 2000))
+        .tier(4)
     }
     if (totalStars >= 5000) {
-      grant('stars-5000', 'I collected 5000 stars.').evidence(text).tier(5)
+      grant('stars-5000', 'I collected 5000 stars.')
+        .evidence(text(repos, 5000))
+        .tier(5)
     }
-    if (totalStars >= 10000) {
-      grant('stars-10000', 'I collected 10000 stars.').evidence(text).tier(6)
+    if (totalStars >= 10_000) {
+      grant('stars-10000', 'I collected 10000 stars.')
+        .evidence(text(repos, 10_000))
+        .tier(6)
     }
-    if (totalStars >= 20000) {
-      grant('stars-20000', 'I collected 20000 stars.').evidence(text).tier(7)
+    if (totalStars >= 20_000) {
+      grant('stars-20000', 'I collected 20000 stars.')
+        .evidence(text(repos, 20_000))
+        .tier(7)
     }
   }
 })()
+
+function asc(a: Repo, b: Repo) {
+  return (a.stargazers_count || 0) - (b.stargazers_count || 0)
+}
+
+function withStars(repo: Repo) {
+  return (repo.stargazers_count || 0) > 0
+}
+
+function text(repos: Repo[], max: number): string {
+  let text = 'Repos:\n\n'
+  let totalStars = 0
+  for (const repo of repos) {
+    totalStars += repo.stargazers_count || 0
+    text += `* <a href="https://github.com/${repo.owner.login}/${repo.name}>${repo.stargazers_count}</a>\n`
+    if (totalStars >= max) {
+      break
+    }
+  }
+  text += `\n<sup>I have push, maintainer or admin permissions, so I'm definitely an author.<sup>\n`
+  return text
+}