coffeecup
coffeecup copied to clipboard
Any equivalence to Dust's @sep-arator concept?
Dust.js (at least LinkedIn's version) has a useful {@sep} capability to denote what "separator" should be used when generating list entries. It's akin to the "join" operator for an array.
Dust: {#links}<a href='/nodes/{href}'>{name}</a>{@sep}, {/sep}{/links}
I can't quite figure out the best way to do this in coffeecup. It's easy to generate a list of links:
a href:link.href, link.name for link in links
but producing them as a comma-separated list is more problematic.
Since coffeecup is "generating" results to a special buffer rather than "returning" results to coffeescript, I can't use a normal "join" operator.
I'm hoping there's a nifty mechanism to do this without resorting to index counting and emitting a text ', '
for all-but-the-last list item.
Any solutions to this? Can I encapsulate this sort of functionality in a helper somehow? Am I overlooking something trivial?
Well, it was easy enough to write a helper function
forEachWithSep: (list, itemFn, sep) ->
for idx, item of list
itemFn item
if idx < list.length - 1
switch typeof sep
when 'string'
text sep
when 'function'
sep()
that I could then call generically
forEachWithSep links,
(link) -> a href:"/#{link.path}", link.name ## Note: arbitrary item handler function
", "
Any other tactics come to mind?