Skip to content

Commit

Permalink
Feat: match template helper (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
w33ble authored and cookpete committed May 16, 2018
1 parent 5ca7c61 commit ce4f9be
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, options.hash.flags || '')
return r.test(val) ? options.fn(this) : options.inverse(this)
})

async function getTemplate (template) {
if (await pathExists(template)) {
return readFile(template, 'utf-8')
Expand Down
67 changes: 67 additions & 0 deletions test/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})

0 comments on commit ce4f9be

Please sign in to comment.