Skip to content

Commit

Permalink
Don't explode when foreach'ing undefined, just act like a zero length…
Browse files Browse the repository at this point in the history
… array.
  • Loading branch information
Danny Brain committed Sep 17, 2010
1 parent a86d5ba commit 8310442
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/jazz/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,10 @@ Compiler_JS.prototype._compileIfStmt = function(stmt) {
Compiler_JS.prototype._compileForEach = function(stmt) {
var exprVar = this._newVar();
var indexVar = this._newVar();
var lengthVar = this._newVar();
var code = "var " + exprVar + " = " + this._compileExpr(stmt.expr) + ";\n";
code += "for (var " + indexVar + " = 0; " + indexVar + " < " + exprVar + ".length; " + indexVar + "++) ";
code += "var " + lengthVar + " = " + exprVar + " ? " + exprVar + ".length : 0;\n";
code += "for (var " + indexVar + " = 0; " + indexVar + " < " + lengthVar + "; " + indexVar + "++) ";
code += "(function() {\n";
code += " var " + stmt.ident.name + " = " + exprVar + "[" + indexVar + "];\n";
code += " " + this._compileSuite(stmt.suite);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "jazz",
"description": "A simple template engine built specifically for nodejs.",
"version": "0.0.5",
"version": "0.0.6",
"author": "Thomas Lee <thomas.lee@shinetech.com>",
"keywords": ["template", "engine"],
"main": "./lib/jazz.js",
Expand Down
4 changes: 3 additions & 1 deletion test/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ var testCases = [
["{megaMuffin({'size':pewpew.maximum,'legs':pewpew.numberOfLegs,'ears':pewpew.numberOfEars})}",
[{pewpew: {maximum: 5, numberOfLegs: 2, numberOfEars: 2}, megaMuffin: function(hash, cb) { cb(hash['size'] + hash['legs']); }}, "7"]],
["{captainAmerica({'wow': true, 'notwow': false})}",
[{captainAmerica: function(hash, cb) { if (hash['wow'] && !hash['notwow']) { cb("yay"); } else { cb("daww"); }}}, "yay" ]]
[{captainAmerica: function(hash, cb) { if (hash['wow'] && !hash['notwow']) { cb("yay"); } else { cb("daww"); }}}, "yay" ]],
["{foreach a in sambuca}{a}{end}",
[{}, ""]]
];

testCases.forEach(function(testCase) {
Expand Down

0 comments on commit 8310442

Please sign in to comment.