Is there any coffeescript specific trickery that would make this look neater:
index = (->
if segment == 'index'
return 0
else if segment == 'inbox'
return 2
1
)()
You could use an inline if ... then ... else statement broken up into multiple lines (for readability) by using the \ character.
index = if segment == 'index' then 0 \
else if segment == 'inbox' then 2 \
else 1
This is useful if your conditional logic is too complex for a simple switch block.
Yes, the CoffeeScript-specific existential operator:
index = {'index': 0, 'inbox': 2}[segment] ? 1
You could also use an inline if statement to get rid of the function call:
index = if segment == 'index' then 0 else if segment == 'inbox' then 2 else 1
But an inline if wouldn't be any harder in straight Javascript:
index = segment == 'index' ? 0 : segment == 'inbox' ? 2 : 1