Created
August 15, 2011 19:47
-
-
Save shiftb/1147615 to your computer and use it in GitHub Desktop.
Rails to_sentence implemented in JavaScrip
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
function toSentence(arr) { | |
arr = arr.slice(0); | |
switch (arr.length) { | |
case 0: | |
return ""; | |
case 1: | |
return arr.pop().name; | |
default: | |
var lastItem = arr.pop(); | |
return _.pluck(arr, 'name').join(', ') + " and " + lastItem.name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment