Skip to content
This repository has been archived by the owner on May 29, 2020. It is now read-only.

Commit

Permalink
Simplify template function.
Browse files Browse the repository at this point in the history
  • Loading branch information
sapegin committed Mar 13, 2016
1 parent 2fbb466 commit f4d0fdd
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/util.js
Expand Up @@ -105,8 +105,7 @@ export function makeUrl(url, context) {
export function template(tmpl, context, filter) {
return tmpl.replace(/\{([^}]+)}/g, function(m, key) {
let value = filter ? filter(context[key]) : context[key];
// If key doesn't exists in the context we should keep template tag as is
return key in context ? value : m;
return value || '';
});
}

Expand Down
5 changes: 5 additions & 0 deletions test/util.spec.js
Expand Up @@ -43,6 +43,11 @@ test('template should render a template', t => {
t.is(r, 'Foo foo baz 42.');
});

test('template should render empty string for unknown tags', t => {
let r = template('Foo {bar} baz {boo}.', { bar: 'foo' });
t.is(r, 'Foo foo baz .');
});

test('template should accept a converted function', t => {
let r = template('Foo {bar} baz {boo}.', { bar: 'foo', boo: 'baa' }, s => s.toUpperCase());
t.is(r, 'Foo FOO baz BAA.');
Expand Down

0 comments on commit f4d0fdd

Please sign in to comment.