Skip to content
Open
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
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ export function renderString(

const variableValue = props[variable]

// If the variable doesn't exist, do not process the template
if (variableValue === undefined) {
expressions[i] = `${delimiters[0]}${expressions[i]}${delimiters[1]}`
continue
}

expressions[i] = filters.reduce((variableValue, filter) => {
const splitPattern = /:(?![^{}]*})/g
const [filterMethod, args] = filter.split(splitPattern)
Expand Down
20 changes: 19 additions & 1 deletion tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,22 @@ describe('renderString', () => {
expect(renderString('Test {{ value }}', { value: 123 })).toBe('Test 123')
})

test('should return ignore invalid filters', () => {
test('should ignore invalid filters', () => {
expect(renderString('{{ value | invalidFilter }}', { value: 123 })).toBe(
123
)
})

test('should ignore undefined props', () => {
expect(renderString(
'{{ prefix }}/{{ suffix }}',
{ prefix: 'abc' }
)).toBe('abc/{{ suffix }}')
expect(renderString(
'{{ prefix }}/{{ suffix | upcase }}',
{ prefix: 'abc' }
)).toBe('abc/{{ suffix | upcase }}')
})
})
})

Expand All @@ -135,4 +146,11 @@ describe('renderObject', () => {
list: [1, 1234, 3],
})
})

test('should ignore undefined props', () => {
expect(renderObject(
{ key: '{{ nested.prefix }}/{{ nested.suffix }}' },
{ nested: { prefix: 'abc' } }
)).toEqual({ key: 'abc/{{ nested.suffix }}' })
})
})