Created
November 6, 2022 19:13
-
-
Save yifeiyin/f33f3324ca0b2d1ede3011b8ae51b391 to your computer and use it in GitHub Desktop.
commit message parsing using magic-regexp https://regexp.dev
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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