Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: match template helper #40

Merged
merged 3 commits into from
May 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
})