Skip to content

Commit

Permalink
Use JavaScript's definition of falsy
Browse files Browse the repository at this point in the history
Fixes #186
  • Loading branch information
mjackson committed Aug 31, 2012
1 parent a6420e4 commit 5ab345e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
7 changes: 3 additions & 4 deletions mustache.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,9 @@ var Mustache;
Renderer.prototype._inverted = function (name, context, callback) {
var value = context.lookup(name);

// From the spec: inverted sections may render text once based on the
// inverse value of the key. That is, they will be rendered if the key
// doesn't exist, is false, or is an empty list.
if (value == null || value === false || (isArray(value) && value.length === 0)) {
// Use JavaScript's definition of falsy. Include empty arrays.
// See https://github.com/janl/mustache.js/issues/186
if (!value || (isArray(value) && value.length === 0)) {
return callback(context, this);
}

Expand Down
8 changes: 8 additions & 0 deletions test/_files/falsy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
({
"emptyString": "",
"emptyArray": [],
"zero": 0,
"null": null,
"undefined": undefined,
"NaN": 0/0
})
12 changes: 12 additions & 0 deletions test/_files/falsy.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{{#emptyString}}empty string{{/emptyString}}
{{^emptyString}}inverted empty string{{/emptyString}}
{{#emptyArray}}empty array{{/emptyArray}}
{{^emptyArray}}inverted empty array{{/emptyArray}}
{{#zero}}zero{{/zero}}
{{^zero}}inverted zero{{/zero}}
{{#null}}null{{/null}}
{{^null}}inverted null{{/null}}
{{#undefined}}undefined{{/undefined}}
{{^undefined}}inverted undefined{{/undefined}}
{{#NaN}}NaN{{/NaN}}
{{^NaN}}inverted NaN{{/NaN}}
12 changes: 12 additions & 0 deletions test/_files/falsy.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

inverted empty string

inverted empty array

inverted zero

inverted null

inverted undefined

inverted NaN

0 comments on commit 5ab345e

Please sign in to comment.