Created
October 5, 2011 06:55
-
-
Save mihar/1263820 to your computer and use it in GitHub Desktop.
JavaScript equivalent of Array#to_sentence from Rails
This file contains hidden or 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
Array.prototype.to_sentence = function() { | |
return this.join(", ").replace(/,\s([^,]+)$/, ' and $1') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you like oxford commas (my team at work does), you can do this instead:
I also changed the name to
toSeries
because I disagree with rails about the name.Coffeescript version: