Skip to content

Commit

Permalink
feat: allows notes pattern to be customized (#586)
Browse files Browse the repository at this point in the history
  • Loading branch information
dnlfrst committed Nov 2, 2020
1 parent cd4c726 commit 9c00f32
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
6 changes: 6 additions & 0 deletions packages/conventional-commits-parser/README.md
Expand Up @@ -205,6 +205,12 @@ Type: `array` of `string` or `string` Default: `['BREAKING CHANGE']`

Keywords for important notes. This value is case **insensitive**. If it's a `string` it will be converted to an `array` separated by a comma.

##### notesPattern

Type: `function` Default: `noteKeywordsSelection => ^[\\s|*]*(' + noteKeywordsSelection + ')[:\\s]+(.*)` where `noteKeywordsSelection` is `join(noteKeywords, '|')`

A function that takes `noteKeywordsSelection` and returns a `RegExp` to be matched against the notes.

##### fieldPattern

Type: `regex` or `string` Default: `/^-(.*?)-$/`
Expand Down
12 changes: 9 additions & 3 deletions packages/conventional-commits-parser/lib/regex.js
Expand Up @@ -13,12 +13,18 @@ function join (array, joiner) {
.join(joiner)
}

function getNotesRegex (noteKeywords) {
function getNotesRegex (noteKeywords, notesPattern) {
if (!noteKeywords) {
return reNomatch
}

return new RegExp('^[\\s|*]*(' + join(noteKeywords, '|') + ')[:\\s]+(.*)', 'i')
const noteKeywordsSelection = join(noteKeywords, '|')

if (!notesPattern) {
return new RegExp('^[\\s|*]*(' + noteKeywordsSelection + ')[:\\s]+(.*)', 'i')
}

return notesPattern(noteKeywordsSelection)
}

function getReferencePartsRegex (issuePrefixes, issuePrefixesCaseSensitive) {
Expand All @@ -42,7 +48,7 @@ function getReferencesRegex (referenceActions) {

module.exports = function (options) {
options = options || {}
var reNotes = getNotesRegex(options.noteKeywords)
var reNotes = getNotesRegex(options.noteKeywords, options.notesPattern)
var reReferenceParts = getReferencePartsRegex(options.issuePrefixes, options.issuePrefixesCaseSensitive)
var reReferences = getReferencesRegex(options.referenceActions)

Expand Down
12 changes: 12 additions & 0 deletions packages/conventional-commits-parser/test/regex.spec.js
Expand Up @@ -18,6 +18,18 @@ describe('regex', function () {
expect(match[2]).to.equal('This is so important.')
})

it('should match notes with customized pattern', function () {
var reNotes = regex({
noteKeywords: ['BREAKING CHANGE'],
notesPattern: (noteKeywords) => new RegExp('^[\\s|*]*(' + noteKeywords + ')[:\\s]+(?:\\[.*\\] )(.*)', 'i')
}).notes
var notes = 'BREAKING CHANGE: [Do not match this prefix.] This is so important.'
var match = notes.match(reNotes)
expect(match[0]).to.equal(notes)
expect(match[1]).to.equal('BREAKING CHANGE')
expect(match[2]).to.equal('This is so important.')
})

it('should be case insensitive', function () {
var reNotes = regex({
noteKeywords: ['Breaking News', 'Breaking Change']
Expand Down

0 comments on commit 9c00f32

Please sign in to comment.