Commit e53454a

Anton Medvedev <anton@medv.io>
2024-07-05 23:03:38
Add plural helper
1 parent f210375
Changed files (7)
badges
cafe-commit
dead-commit
favorite-word
github-anniversary
old-issue
src
badges/cafe-commit/cafe-commit.ts
@@ -1,4 +1,4 @@
-import { Commit, define, Repo } from '#src'
+import { Commit, define, plural, Repo } from '#src'
 
 export default define({
   url: import.meta.url,
@@ -24,7 +24,11 @@ export default define({
     if (commits.length >= 1) {
       grant(
         'cafe-commit',
-        `I pushed a commit with "cafe" ${commits.length} times.`,
+        `I pushed a commit with "cafe" ${plural(
+          commits.length,
+          'once',
+          '%d times',
+        )}.`,
       ).evidence(text)
     }
   },
badges/dead-commit/dead-commit.ts
@@ -1,4 +1,4 @@
-import { Commit, define, Repo } from '#src'
+import { Commit, define, Repo, plural } from '#src'
 
 export default define({
   url: import.meta.url,
@@ -24,7 +24,11 @@ export default define({
     if (commits.length >= 1) {
       grant(
         'dead-commit',
-        `I pushed a commit with "dead" ${commits.length} times.`,
+        `I pushed a commit with "dead" ${plural(
+          commits.length,
+          'once',
+          '%d times',
+        )}.`,
       ).evidence(text)
     }
   },
badges/favorite-word/favorite-word.ts
@@ -1,4 +1,4 @@
-import { define } from '#src'
+import { define, plural } from '#src'
 
 export default define({
   url: import.meta.url,
@@ -25,7 +25,10 @@ export default define({
     grant('favorite-word', `My favorite word is "${topWords[0][0]}".`).evidence(
       `My favorite commit message words are:\n\n` +
         topWords
-          .map((p, i) => `${i + 1}. ${p[0]} (used ${p[1]} times)`)
+          .map(
+            (p, i) =>
+              `${i + 1}. ${p[0]} (used ${plural(p[1], 'once', '%d times')})`,
+          )
           .join('\n'),
     )
   },
badges/github-anniversary/github-anniversary.ts
@@ -1,4 +1,4 @@
-import { define } from '#src'
+import { define, plural } from '#src'
 
 export default define({
   url: import.meta.url,
@@ -23,7 +23,10 @@ export default define({
           createdAt.getDay(),
         ).valueOf()
       ) {
-        grant(badge, `I joined GitHub ${years} years ago.`)
+        grant(
+          badge,
+          `I joined GitHub ${plural(years, 'a year', '%d years')} ago.`,
+        )
       }
     })
   },
badges/old-issue/old-issue.ts
@@ -1,4 +1,4 @@
-import { define, Issue } from '#src'
+import { define, Issue, plural } from '#src'
 
 export default define({
   url: import.meta.url,
@@ -34,7 +34,11 @@ export default define({
       if (!buckets[years]) continue
       grant(
         `old-issue-${years}` as (typeof this.badges)[number],
-        `I closed an issue that was open for ${years} years`,
+        `I closed an issue that was open for ${plural(
+          years,
+          'a year',
+          '%d years',
+        )}`,
       )
         .evidenceIssuesWithTitles(...buckets[years])
         .tier(years)
src/index.ts
@@ -1,3 +1,3 @@
 export { define } from './badges.js'
 export { Repo, User, Issue, Pull, Commit } from './collect/types.js'
-export { linkCommit, linkIssue, linkPull, latest } from './utils.js'
+export { linkCommit, linkIssue, linkPull, latest, plural } from './utils.js'
src/utils.ts
@@ -50,3 +50,7 @@ export function latest(a: Commit, b: Commit) {
     new Date(b.committedDate).getTime() - new Date(a.committedDate).getTime()
   )
 }
+
+export function plural(count: number, singular: string, plural: string) {
+  return (count === 1 ? singular : plural).replace('%d', count.toString())
+}