Commit 3a250ce

Anton Golub <antongolub@antongolub.com>
2023-10-13 11:45:35
feat: support masks for omit and pick opts
1 parent 4a06480
src/present-badges.ts
@@ -14,6 +14,9 @@ export const mergeBadges = (...badges: (Badge | Badge[])[]): Badge[] =>
       ),
   )
 
+const parseRegexp = (value: string): RegExp =>
+  new RegExp(`^${value}$`.replace('*', '.+'))
+
 export const presentBadges = (
   data: Data,
   userBadges: Badge[],
@@ -57,10 +60,14 @@ export const presentBadges = (
     }
   }
   if (pickBadges.length > 0) {
-    userBadges = userBadges.filter((x) => pickBadges.includes(x.id))
+    userBadges = userBadges.filter((x) =>
+      pickBadges.map(parseRegexp).some((r) => r.test(x.id)),
+    )
   }
   if (omitBadges.length > 0) {
-    userBadges = userBadges.filter((x) => !omitBadges.includes(x.id))
+    userBadges = userBadges.filter((x) =>
+      omitBadges.map(parseRegexp).every((r) => !r.test(x.id)),
+    )
   }
 
   return userBadges
test/present-badges.test.ts
@@ -97,6 +97,45 @@ describe('present-badges', () => {
     ])
   })
 
+  it('presentBadges() supports masks for `omit` && `pick`', () => {
+    const userBadges = presentBadges(
+      data,
+      [],
+      ['stars-*'],
+      ['stars-*000'],
+      false,
+    )
+
+    assert.deepEqual(userBadges, [
+      {
+        id: 'stars-100',
+        tier: 1,
+        desc: 'I collected 100 stars.',
+        body:
+          'Repos:\n' +
+          '\n' +
+          '* <a href="https://github.com/foo/bar">foo/bar: ★1000</a>\n' +
+          '\n' +
+          "<sup>I have push, maintainer or admin permissions, so I'm definitely an author.<sup>\n",
+        image:
+          'https://github.com/my-badges/my-badges/blob/master/src/all-badges/stars/stars-100.png?raw=true',
+      },
+      {
+        id: 'stars-500',
+        tier: 2,
+        desc: 'I collected 500 stars.',
+        body:
+          'Repos:\n' +
+          '\n' +
+          '* <a href="https://github.com/foo/bar">foo/bar: ★1000</a>\n' +
+          '\n' +
+          "<sup>I have push, maintainer or admin permissions, so I'm definitely an author.<sup>\n",
+        image:
+          'https://github.com/my-badges/my-badges/blob/master/src/all-badges/stars/stars-500.png?raw=true',
+      },
+    ])
+  })
+
   it('presentBadges() applies `compact`', () => {
     const userBadges = presentBadges(
       data,
README.md
@@ -68,7 +68,7 @@ You can also perform these steps manually:
 | `size`    |                | Badge size for README.md, px                                                                                                                            | 64            |
 | `dryrun`  |                | Generate badges, but skip pushing them to git                                                                                                           |               |
 | `pick`    |                | List of badges to pick. Pass `--pick="a-commit,ab-commit,revert-revert-commit"` to generate only the specified entries. If empty gets all of them       |               |
-| `omit`    |                | List of badges to exclude. For example, if you're too shy to flex your stars: `--omit:stars-100,stars-500,stars-1000`                                   |               |
+| `omit`    |                | List of badges to exclude. For example, if you're too shy to flex your stars: `--omit:stars-100,stars-500,stars-1000` or even shorter `--omit:stars-*`  |               |
 | `compact` |                | Represent the highest tier badges in README.md. For example, If you have both `stars-100` and `stars-500` achievements, only the last one will be shown |               |
 
 ## License