Last active
January 31, 2018 15:46
-
-
Save seawolf/ecf025a7ec9a0a630b577e34231eb69c to your computer and use it in GitHub Desktop.
List Join
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
function joinListOfStrings(list = [], separator = ',', conjunction = 'and', oxfordComma = false) { | |
switch (list.length) { | |
case 0: | |
return ''; | |
case 1: | |
return list[0]; | |
case 2: | |
return list.join( (oxfordComma ? ', ' : ' ') + conjunction + ' '); | |
default: | |
var lastTwoParts = list.splice(-2); | |
var easyBit = list.join(separator + ' ') + separator + ' '; | |
return easyBit + lastTwoParts.join( (oxfordComma ? ', ' : ' ') + conjunction + ' '); | |
} | |
}; |
Author
seawolf
commented
Jan 31, 2018
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment