Commit 850ccd5

Maggy Hofstetter <119439839+Rignchen@users.noreply.github.com>
2025-01-19 09:38:58
feat(favorite-word): ignore conventional commit (#110)
* feat(favorite-word): ignore conventionnal commit conventionnal commit are put in every single commit, because of that they allways end up being way more frequent than most other words and aren't really interresting as a statistic * feat(favorite-word): add breaking changes * test(favorite-word): create test script file * test(favorite-word): add factory to create Data with just commit message & body * style: make test prettier * test(favorite-word): define list of commit * test(favorite-word): test which word is the most frequent * test(favorite-word): test which word are the 5 most frequent words * style: format code properly * refactor(favorite-word): split & filter words in a dedicated function
1 parent b071169
Changed files (2)
badges/favorite-word/favorite-word.test.ts
@@ -0,0 +1,46 @@
+import { describe, it, expect } from 'vitest'
+import { splitWithoutTooFrequentWords } from './favorite-word.js'
+
+describe('favorite-word', () => {
+  // prettier-ignore
+  describe('ignore conventional commit prefixes', () => {
+    const prefixes = [
+      'BREAKING CHANGE',
+      'build',
+      'chore',
+      'ci',
+      'docs',
+      'feat',
+      'fix',
+      'perf',
+      'refactor',
+      'revert',
+      'style',
+      'test',
+    ]
+    for (const prefix of prefixes) {
+	  describe(`"${prefix}"`, () => {
+        it(`ignores "${prefix}" at the beginning of the message`, () => {
+          expect(splitWithoutTooFrequentWords(`${prefix}: hello world`)).toEqual(['hello', 'world'])
+        })
+        it(`ignores "${prefix}" without space after it`, () => {
+          expect(splitWithoutTooFrequentWords(`${prefix}:hello world`)).toEqual(['hello', 'world'])
+        })
+        it(`ignores "${prefix}" with exclamation mark`, () => {
+          expect(splitWithoutTooFrequentWords(`${prefix}!: hello world`)).toEqual(['hello', 'world'])
+        })
+        it(`ignores "${prefix}" with scope`, () => {
+          expect(splitWithoutTooFrequentWords(`${prefix}(world): hello`)).toEqual(['hello'])
+        })
+        it(`don't ignore "${prefix}" if not at the beginning`, () => {
+	      const expectedPrefix = `${prefix}:`.toLowerCase().split(' ')
+	      expectedPrefix.unshift('hello')
+          expect(splitWithoutTooFrequentWords(`hello ${prefix}:`)).toEqual(expectedPrefix)
+        })
+	  })
+    }
+    it(`don't ignore fake prefix`, () => {
+      expect(splitWithoutTooFrequentWords(`fake: hello world`)).toEqual(['fake:', 'hello', 'world'])
+    })
+  })
+})
badges/favorite-word/favorite-word.ts
@@ -9,13 +9,7 @@ export default define({
     for (const repo of data.repos) {
       for (const commit of repo.commits) {
         const msg = commit.message + '\n' + commit.messageBody
-        const words = removeStopwords(
-          msg
-            .toLowerCase()
-            .split(/\s+/)
-            // ignore words not including alphanumeric chars
-            .filter((w) => /\w/.test(w)),
-        )
+        const words = splitWithoutTooFrequentWords(msg)
         for (const word of words) {
           counts[word] = (counts[word] || 0) + 1
         }
@@ -36,3 +30,18 @@ export default define({
     )
   },
 })
+
+export function splitWithoutTooFrequentWords(msg: string) {
+  return removeStopwords(
+    msg
+      .toLowerCase()
+      // remove conventional commit prefixes as they would outweigh other words
+      .replace(
+        /^(breaking change|build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\(.*\))?!?:\s*/,
+        '',
+      )
+      .split(/\s+/)
+      // ignore words not including alphanumeric chars
+      .filter((w) => /\w/.test(w)),
+  )
+}