Skip to content

Commit

Permalink
dynamic title is displayed as json string instead of Object object
Browse files Browse the repository at this point in the history
  • Loading branch information
Barabasbalazs committed Dec 8, 2024
1 parent 8a0df5f commit 7348130
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/interpolate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ function parseProp(data: any, key: string) {
* @example
* interpolate('hello { username }', { username: 'virk' })
*/

export function interpolate(input: string, data: any, index: number) {
return input.replace(/(\\)?{(.*?)}/g, (_, escapeChar, key) => {
if (escapeChar) {
Expand All @@ -40,7 +41,7 @@ export function interpolate(input: string, data: any, index: number) {
}

if (key === '$self') {
return data
return typeof data === 'string' ? data : JSON.stringify(data)
}

return parseProp(data, key)
Expand Down
5 changes: 4 additions & 1 deletion tests/interpolate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ test.describe('Interpolate', () => {
})

test('interpolate when literval value is not a string', () => {
assert.equal(interpolate('hello {$self}', { foo: 'bar' }, 0), 'hello [object Object]')
assert.equal(
interpolate('hello {$self}', { foo: 'bar' }, 0),
`hello ${JSON.stringify({ foo: 'bar' })}`
)
})

test('interpolate key value is not a string', () => {
Expand Down

0 comments on commit 7348130

Please sign in to comment.