Skip to content
Merged
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ console.log(template.parameters); // Prints [{ key: "foo" }]
console.log(template({ foo: "bar" })); // Prints "bar"
```

Parameters can have default values, specified using a colon. These come into play when the parameter is either `undefined` or `null`.
Parameters can have default values, specified using a colon. These come into play only when the parameter is `undefined`.

```js
const template = parse("{{foo:bar}}");
Expand Down
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,16 @@ const parseString = (() => {
return matches.reduce((result, match, i) => {
const parameter = parameters[i];
let value = objectPath.get(context, parameter.key);
if (value == null) {

if (typeof value === 'undefined') {
value = parameter.defaultValue;
}

if (typeof value === 'function') {
value = value();
}

if (typeof value === 'object') {
if (typeof value === 'object' && value !== null) {
return value;
}

Expand Down
45 changes: 34 additions & 11 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,14 @@ describe('json-template', () => {
});
});

describe('date', () => {
it('should compute template with Date', () => {
const template = parse('{{now}}');
const now = new Date();
assert.strictEqual(template({ now }), now);
});
});

// This section tests that arbitrary types may be present
// as leaf nodes of the object tree, and they are handled correctly.
describe('unknown types', () => {
Expand Down Expand Up @@ -595,26 +603,41 @@ describe('json-template', () => {
});
});

// This section tests that if the match is not found the template should be replaced by null
// This section tests that if the match is not found the template should remains undefined
describe('no match on the given context', () => {
it('should replace the given template by null if no match found for an string', () => {
it('should replace the given template by undefined if no match found for an string', () => {
const template = parse('{{foo}}');
assert.equal(template({}), null);
assert.strictEqual(template({}), undefined);
});

it('should replace the given template by null if no match found for an object', () => {
it('should replace the given template by undefined if no match found for an object', () => {
const template = parse({ boo: '{{foo}}' });
assert.deepEqual(template({}), { boo: null });
assert.deepStrictEqual(template({}), { boo: undefined });
});

it('should replace the given template by null if the found value is null', () => {
const template = parse({ boo: '{{foo}}' });
assert.deepEqual(template({ foo: null }), { boo: null });
});

it('should handle multi-value expressions where the first value is null, but has a defaultValue', () => {
const template = parse({ boo: '{{foo.isNull:defaultValue}} {{foo.isNonNull}}' });
assert.deepEqual(template({ foo: { isNull: null, isNonNull: 'value' } }), { boo: 'defaultValue value' });
assert.deepStrictEqual(template({ foo: null }), { boo: null });
});
});

describe('string template', () => {
it('should be string type when there are more than one slots', () => {
const template = parse('{{foo}}{{bar}}');
assert.equal(template({ foo: 1, bar: 'a' }), '1a');
assert.equal(template({ bar: 'a' }), 'a');
assert.equal(template({ foo: 1 }), '1');
assert.equal(template({ foo: true, bar: false }), 'truefalse');
assert.equal(template({ foo: undefined }), '');
assert.equal(template({ foo: null }), '');
assert.equal(template({}), '');
assert.equal(template(), '');
assert.equal(template({ foo: Number.NaN }), 'NaN');
});

it('default value', () => {
const template = parse({ boo: '{{foo.isNull:null}} {{foo.isUndefined:undefined}} {{foo.isNonNull}}' });
assert.deepStrictEqual(template({ foo: { isNull: null, isNonNull: 'value' } }), { boo: ' undefined value' });
});
})
});