From 48ffa1a17af19db28af3fd5479dce70db68ae58b Mon Sep 17 00:00:00 2001 From: Joe Fleming Date: Wed, 18 Apr 2018 11:21:05 -0700 Subject: [PATCH 1/3] feat: add a matches template helper useful for matching on values in the commit --- src/template.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/template.js b/src/template.js index 0ad236b7..874d4d87 100644 --- a/src/template.js +++ b/src/template.js @@ -43,6 +43,11 @@ Handlebars.registerHelper('commit-list', function (context, options) { return `${options.hash.heading}\n${list}` }) +Handlebars.registerHelper('matches', function (val, pattern, options) { + const r = new RegExp(pattern) + return r.test(val) ? options.fn(this) : options.inverse(this) +}) + async function getTemplate (template) { if (await pathExists(template)) { return readFile(template, 'utf-8') From 3dcd4a8de4eb7239160f78d82c2e1f3cd06df966 Mon Sep 17 00:00:00 2001 From: Joe Fleming Date: Wed, 18 Apr 2018 11:23:33 -0700 Subject: [PATCH 2/3] feat: allow flags to be specified --- src/template.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/template.js b/src/template.js index 874d4d87..782de61d 100644 --- a/src/template.js +++ b/src/template.js @@ -44,7 +44,7 @@ Handlebars.registerHelper('commit-list', function (context, options) { }) Handlebars.registerHelper('matches', function (val, pattern, options) { - const r = new RegExp(pattern) + const r = new RegExp(pattern, options.hash.flags || '') return r.test(val) ? options.fn(this) : options.inverse(this) }) From dd97fb4ab7c78f0d155d4457905ddfdaf6bc4e54 Mon Sep 17 00:00:00 2001 From: Joe Fleming Date: Fri, 27 Apr 2018 12:57:03 -0700 Subject: [PATCH 3/3] tests: add matches helper tests --- test/template.js | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/test/template.js b/test/template.js index 426fea76..3b970385 100644 --- a/test/template.js +++ b/test/template.js @@ -114,3 +114,70 @@ describe('commit-list helper', () => { expect(compile({ commits })).to.equal(expected) }) }) + +describe('matches helper', () => { + const compileCommits = (matches) => Handlebars.compile( + '{{#each releases}}\n' + + '{{#each commits}}\n' + + matches + + '{{/each}}\n' + + '{{/each}}' + ) + + it('matches on field value', () => { + const matches = + '{{#matches href "12c0624"}}\n' + + '- {{message}}\n' + + '{{/matches}}\n' + const expected = + '- Commit that fixes nothing\n' + expect(compileCommits(matches)({ releases })).to.equal(expected) + }) + + it('matches with case insensitive flag', () => { + const matches = + '{{#matches author "pete" flags="i"}}\n' + + '- {{shorthash}}\n' + + '{{/matches}}\n' + const expected = + '- b0b3040\n' + + '- 12c0624\n' + + '- e9a43b2\n' + + '- 158fdde\n' + expect(compileCommits(matches)({ releases })).to.equal(expected) + }) + + it('provides non-matching conditional', () => { + const matches = + '{{#matches shorthash "e9a43b2"}}\n' + + '- HIT {{date}}\n' + + '{{else}}\n' + + '- MISS {{date}}\n' + + '{{/matches}}\n' + const expected = + '- MISS 2015-12-29T21:57:19.000Z\n' + + '- MISS 2015-12-29T21:18:19.000Z\n' + + '- HIT 2015-12-29T21:19:19.000Z\n' + + '- MISS 2015-12-14T17:06:12.000Z\n' + expect(compileCommits(matches)({ releases })).to.equal(expected) + }) + + it('matches on multiline content', () => { + const multiReleases = [{ commits: [ + { + shorthash: 'c0f25d7', + message: 'Hello\n\nWorld\n\nBREAKING CHANGE: mock break\n\nsome more text' + }, { + shorthash: '12cd728', + message: 'Nope' + } + ]}] + const matches = + '{{#matches message "BREAKING CHANGE"}}\n' + + '- {{shorthash}}\n' + + '{{/matches}}\n' + const expected = + '- c0f25d7\n' + expect(compileCommits(matches)({ releases: multiReleases })).to.equal(expected) + }) +})