diff --git a/lib/dust-helpers.js b/lib/dust-helpers.js index 18d2a4f..ca72dc2 100644 --- a/lib/dust-helpers.js +++ b/lib/dust-helpers.js @@ -188,18 +188,28 @@ var helpers = { }, "first": function(chunk, context, bodies) { + var body = bodies.block, + skip = bodies['else']; + if (context.stack.index === 0) { - return bodies.block(chunk, context); + chunk = chunk.render(body, context); + } else if (skip) { + chunk = chunk.render(skip, context); } return chunk; }, "last": function(chunk, context, bodies) { + var body = bodies.block, + skip = bodies['else']; + if (context.stack.index === context.stack.of - 1) { - return bodies.block(chunk, context); + chunk = chunk.render(body, context); + } else if (skip) { + chunk = chunk.render(skip, context); } return chunk; - }, + }, /** * {@contextDump} diff --git a/test/jasmine-test/spec/helpersTests.js b/test/jasmine-test/spec/helpersTests.js index ba8f98a..ce6a93c 100644 --- a/test/jasmine-test/spec/helpersTests.js +++ b/test/jasmine-test/spec/helpersTests.js @@ -1595,6 +1595,13 @@ expected: "Hello Alice Bob Charlie ", message: "first helper should output on the first iteration only" }, + { + name: "first helper with else", + source: "{#guests}{@first}Hello {:else}and {/first}{.} {/guests}", + context: { guests: function() { return ["Alice", "Bob", "Charlie"]; } }, + expected: "Hello Alice and Bob and Charlie ", + message: "first helper should support else" + }, { name: "last helper", source: "Hello {#guests}{@last}and {/last}{.} {/guests}", @@ -1602,6 +1609,13 @@ expected: "Hello Alice Bob and Charlie ", message: "last helper should output on the last iteration only" }, + { + name: "last helper with else", + source: "{#guests}{.}{@last}.{:else} and {/last}{/guests}", + context: { guests: function() { return ["Alice", "Bob", "Charlie"]; } }, + expected: "Alice and Bob and Charlie.", + message: "last helper should support else" + }, { name: "first / last / sep combo", source: "{#guests}{@first}Hello {/first}{@last}and {/last}{.}{@last}!{/last}{@sep}, {/sep}{/guests}",