Commit 850ccd5
Changed files (2)
badges
favorite-word
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)),
+ )
+}