Skip to content
This repository has been archived by the owner on Apr 11, 2018. It is now read-only.

Null should yield empty string when resolving variable #408

Merged
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
6 changes: 3 additions & 3 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ TokenParser.prototype = {
* @private
*/
checkMatch: function (match) {
var temp = match[0];
var temp = match[0], result;

function checkDot(ctx) {
var c = ctx + temp,
Expand All @@ -415,8 +415,8 @@ TokenParser.prototype = {
function buildDot(ctx) {
return '(' + checkDot(ctx) + ' ? ' + ctx + match.join('.') + ' : "")';
}

return '(' + checkDot('_ctx.') + ' ? ' + buildDot('_ctx.') + ' : ' + buildDot('') + ')';
result = '(' + checkDot('_ctx.') + ' ? ' + buildDot('_ctx.') + ' : ' + buildDot('') + ')';
return '(' + result + ' !== null ? ' + result + ' : ' + '"" )';
}
};

Expand Down
8 changes: 7 additions & 1 deletion tests/variables.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ var cases = {
'return empty string if undefined': [
{ c: '"{{ u }}"', e: '""' }
],
'return empty string if null': [
{ c: '"{{ n }}"', e: '""' },
{ c: '"{{ o3.n }}"', e: '""' }
],
'can use operators': [
{ c: '{{ a + 3 }}', e: '4' },
{ c: '{{ a * 3 }}', e: '3' },
Expand Down Expand Up @@ -88,8 +92,10 @@ describe('Variables', function () {
g: { '0': { q: { c: { b: { foo: 'hi!' }}}}},
h: { g: { i: 'q' } },
i: 'foo',
n: null,
o: Object.create({ foo: function () { return 'bar'; } }),
o2: { a: 'bar', foo: function (b) { return b || this.a; } }
o2: { a: 'bar', foo: function (b) { return b || this.a; } },
o3: { n: null }
}};
_.each(cases, function (cases, description) {
describe(description, function () {
Expand Down