Created
April 22, 2013 00:02
-
-
Save bradleybuda/5431638 to your computer and use it in GitHub Desktop.
Rails' Array#to_sentence for AngularJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%span | |
%p {{ [] | toSentence }} | |
%p {{ ['my parents'] | toSentence }} | |
%p {{ ['my parents', 'Ayn Rand'] | toSentence }} | |
%p {{ ['my parents', 'Ayn Rand', 'God'] | toSentence }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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