Skip to content

Commit

Permalink
fix(template): fix replacement of a multiple occurence of the same va…
Browse files Browse the repository at this point in the history
…riable (#46)
  • Loading branch information
arnaudbesnier committed May 7, 2020
1 parent 37e0df5 commit 73c2238
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function template(input, variables) {
return Object.keys(variables).reduce(
(output, variable) =>
variables[variable]
? output.replace(`$${variable}`, variables[variable])
? output.split(`$${variable}`).join(variables[variable])
: output,
input
)
Expand Down
7 changes: 7 additions & 0 deletions test/test_template.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ describe('test template', () => {
assert.equal(expected, actual)
})

it('should replace multiple identical variables in string', () => {
const world = 'underworld'
const expected = `hello ${world}, hello ${world}`
const actual = template('hello $world, hello $world', { world })
assert.equal(expected, actual)
})

it('should replace variable in list', () => {
const expected = 'underworld'
const actual = template(['$world'], { world: expected })
Expand Down

0 comments on commit 73c2238

Please sign in to comment.