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

Commit

Permalink
Merge pull request #408 from lukaszb/null-should-yield-empty-string
Browse files Browse the repository at this point in the history
Null must yield empty string when printing variables === `null`. Fixes gh-408, Fixes gh-236
  • Loading branch information
paularmstrong committed Mar 6, 2014
2 parents e758c0c + 91394b5 commit 4b61275
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
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

0 comments on commit 4b61275

Please sign in to comment.