Skip to content

Instantly share code, notes, and snippets.

@bradleybuda
Created April 22, 2013 00:02
Show Gist options
  • Save bradleybuda/5431638 to your computer and use it in GitHub Desktop.
Save bradleybuda/5431638 to your computer and use it in GitHub Desktop.
Rails' Array#to_sentence for AngularJS
%span
%p {{ [] | toSentence }}
%p {{ ['my parents'] | toSentence }}
%p {{ ['my parents', 'Ayn Rand'] | toSentence }}
%p {{ ['my parents', 'Ayn Rand', 'God'] | toSentence }}
app.filter 'toSentence', ->
(items) ->
l = items.length
if l == 0
''
else if l == 1
items[0]
else if l == 2
"#{items[0]} and #{items[1]}"
else
items.slice(0, -1).join(', ') + ', and ' + items[l - 1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment