Skip to content

Commit

Permalink
Expand repeated placeholders in commit message (#29)
Browse files Browse the repository at this point in the history
Fixes #26
  • Loading branch information
mislav committed Dec 26, 2021
1 parent 154e0c5 commit 7793600
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
25 changes: 25 additions & 0 deletions src/main-test.ts
Expand Up @@ -16,13 +16,38 @@ test('commitForRelease()', (t) => {
}),
'chore(test formula): version {{version}}'
)
t.is(
commitForRelease('{formulaName} {version}', {
formulaName: 'test formula',
version: 'v1.2.3',
}),
'{formulaName} {version}'
)
t.is(
commitForRelease('chore({{formulaName}}): upgrade to version {{version}}', {
formulaName: 'test formula',
version: 'v1.2.3',
}),
'chore(test formula): upgrade to version v1.2.3'
)
t.is(
commitForRelease(
'{{formulaName}} {{version}}: upgrade {{formulaName}} to version {{version}}',
{
formulaName: 'test formula',
version: 'v1.2.3',
}
),
'test formula v1.2.3: upgrade test formula to version v1.2.3'
)
t.is(
commitForRelease('{{constructor}}{{__proto__}}', {}),
'{{constructor}}{{__proto__}}'
)
t.is(
commitForRelease('{{version}}', { version: 'v{{version}}' }),
'v{{version}}'
)
})

test('prepareEdit()', async (t) => {
Expand Down
11 changes: 8 additions & 3 deletions src/main.ts
Expand Up @@ -18,9 +18,14 @@ export function commitForRelease(
messageTemplate: string,
params: { [key: string]: string } = {}
): string {
return Object.keys(params).reduce(
(currentMessage, tag) => currentMessage.replace(`{{${tag}}}`, params[tag]),
messageTemplate
return messageTemplate.replace(
/\{\{(\w+)\}\}/g,
(m: string, key: string): string => {
if (params.hasOwnProperty(key)) {
return params[key]
}
return m
}
)
}

Expand Down

0 comments on commit 7793600

Please sign in to comment.