Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix default value for null #45

Merged
merged 7 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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') {
mytharcher marked this conversation as resolved.
Show resolved Hide resolved
value = parameter.defaultValue;
}

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

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

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

describe('date', () => {
mytharcher marked this conversation as resolved.
Show resolved Hide resolved
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', () => {
curran marked this conversation as resolved.
Show resolved Hide resolved
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', () => {
curran marked this conversation as resolved.
Show resolved Hide resolved
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 tempalte', () => {
curran marked this conversation as resolved.
Show resolved Hide resolved
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' });
});
})
});