Skip to content

Instantly share code, notes, and snippets.

@cgcostume
Last active April 8, 2022 08:23
Show Gist options
  • Save cgcostume/abfb3a9fbfa08a49b8d0db6f02721650 to your computer and use it in GitHub Desktop.
Save cgcostume/abfb3a9fbfa08a49b8d0db6f02721650 to your computer and use it in GitHub Desktop.
join elements of an array of strings using a ', ' and for the last one use an ' and ', e.g., for a readable author list
const happyAnd = (strings) =>
strings.length === 0 ? '' : strings.reduce((text, value, i, array) =>
text + (i < array.length - 1 ? ', ' : array.length > 2 ? ', and ' : ' and ') + value);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment