From d59f02f622fbe757375577720186017a5611a0e4 Mon Sep 17 00:00:00 2001 From: Matt Sacks Date: Sat, 6 Oct 2012 06:28:41 -0700 Subject: [PATCH] Don't evaluate lambdas that return falsy values as an empty string. Issue: If I create a Mustache lambda to return numbers and I expect 0 to be of those numbers, I'll instead only receive an empty string for 0 because it evaluates to falsy. Fix: Check the result of the lambda to be undefined or null. Only then should it return the empty string. --- mustache.js | 3 ++- test/_files/check_falsy.js | 7 +++++++ test/_files/check_falsy.mustache | 1 + test/_files/check_falsy.txt | 1 + 4 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 test/_files/check_falsy.js create mode 100644 test/_files/check_falsy.mustache create mode 100644 test/_files/check_falsy.txt diff --git a/mustache.js b/mustache.js index 7165a8a..582a29c 100644 --- a/mustache.js +++ b/mustache.js @@ -254,7 +254,8 @@ var Mustache; return self.render(template, context); }; - return value.call(context.view, text, scopedRender) || ""; + var result = value.call(context.view, text, scopedRender); + return result != null ? result : ""; default: if (value) { return callback(this, context); diff --git a/test/_files/check_falsy.js b/test/_files/check_falsy.js new file mode 100644 index 0000000..5a599ca --- /dev/null +++ b/test/_files/check_falsy.js @@ -0,0 +1,7 @@ +({ + number: function(text, render) { + return function(text, render) { + return +render(text); + } + } +}) diff --git a/test/_files/check_falsy.mustache b/test/_files/check_falsy.mustache new file mode 100644 index 0000000..30e2547 --- /dev/null +++ b/test/_files/check_falsy.mustache @@ -0,0 +1 @@ +

{{#number}}0{{/number}}

diff --git a/test/_files/check_falsy.txt b/test/_files/check_falsy.txt new file mode 100644 index 0000000..3bb2f51 --- /dev/null +++ b/test/_files/check_falsy.txt @@ -0,0 +1 @@ +

0