Skip to content

Instantly share code, notes, and snippets.

@nchapman
Forked from mudge/gist:1076046
Created February 19, 2012 08:24
Show Gist options
  • Save nchapman/1862601 to your computer and use it in GitHub Desktop.
Save nchapman/1862601 to your computer and use it in GitHub Desktop.
CoffeeScript version of ActiveSupport's to_sentence method.
toSentence = (array = [], wordsConnector = ", ", twoWordsConnector = " and ", lastWordConnector = ", and ") ->
switch array.length
when 0 then ""
when 1 then array[0]
when 2 then array[0] + twoWordsConnector + array[1]
else array.slice(0, -1).join(wordsConnector) + lastWordConnector + array[array.length - 1]
@billhorsman
Copy link

+1 Thanks Nick.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment