Created
May 19, 2016 00:48
-
-
Save michaelrhodes/c782060b9e61b65fa3ba3f7e21a81bfe to your computer and use it in GitHub Desktop.
Join an array with proper English
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 oxford (arr, last) { | |
last = last || ', and ' | |
return arr.reduce(function (a, b, i, arr) { | |
return ( | |
!arr[i + 1] ? a + b : | |
!arr[i + 2] ? a + b + last : | |
a + b + ', ' | |
) | |
}, '') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment