diff --git a/index.js b/index.js index b1102a9..f98e7b3 100644 --- a/index.js +++ b/index.js @@ -96,6 +96,10 @@ const parseString = (() => { value = parameter.defaultValue; } + if (typeof value === 'function') { + return value(); + } + if (typeof value === 'object') { return value; } diff --git a/test.js b/test.js index b5da400..dbfdf92 100644 --- a/test.js +++ b/test.js @@ -305,6 +305,16 @@ describe('json-template', () => { }); }); + // This section tests that the parse function applies the templating + // on string with function + describe('function', () => { + it('should compute template with function', () => { + const template = parse(['{{userCard}}']); + assert.deepEqual(template.parameters, [{ key: 'userCard' }]); + assert.deepEqual(template({ userCard: () => ({ id: 1, user: "John" }) }), [{ id: 1, user: "John" }]); + }); + }); + // This section tests that arbitrary types may be present // as leaf nodes of the object tree, and they are handled correctly. describe('unknown types', () => {