Skip to content

Instantly share code, notes, and snippets.

@yifeiyin
Created November 6, 2022 19:13
Show Gist options
  • Save yifeiyin/f33f3324ca0b2d1ede3011b8ae51b391 to your computer and use it in GitHub Desktop.
Save yifeiyin/f33f3324ca0b2d1ede3011b8ae51b391 to your computer and use it in GitHub Desktop.
commit message parsing using magic-regexp https://regexp.dev
import assert from 'node:assert';
import {
createRegExp,
exactly,
oneOrMore,
char,
wordChar,
whitespace,
maybe,
} from 'magic-regexp';
// type(scope): [ticket] desc
const a = createRegExp(
oneOrMore(wordChar)
.groupedAs('type')
.and(
exactly('(')
.and(maybe(oneOrMore(char)).groupedAs('scope'))
.and(exactly(')'))
.optionally()
)
.and(oneOrMore(whitespace).optionally())
.and(exactly(':'))
.and(oneOrMore(whitespace).optionally())
.and(
exactly('[')
.and(oneOrMore(char).groupedAs('ticket'))
.and(exactly(']'))
.optionally()
)
.and(maybe(oneOrMore(char)).groupedAs('desc'))
);
const tests = [
'invalid',
'test',
'chore: update',
'fix(careX): tmp',
'fix: [SD-1234] ok',
'fix: [aa]',
'fix(all): [sxxx] aa',
'fix(all): [1234] (with paren)',
];
for (const entry of tests) {
console.log(entry, entry.match(a)?.groups);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment