Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/dust-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
14 changes: 14 additions & 0 deletions test/jasmine-test/spec/helpersTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1595,13 +1595,27 @@
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}",
context: { guests: function() { return ["Alice", "Bob", "Charlie"]; } },
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}",
Expand Down