Skip to content

Instantly share code, notes, and snippets.

@leonardokl
Last active December 27, 2019 19:43
Show Gist options
  • Save leonardokl/6f6bf8012773bce96c4d034a094efdf3 to your computer and use it in GitHub Desktop.
Save leonardokl/6f6bf8012773bce96c4d034a094efdf3 to your computer and use it in GitHub Desktop.
Format Text List
export const formatList = list =>
list.reduce((acc, text, index) => {
if (index === 0) return text;
return acc.concat(`${!list[index + 1] ? ' and' : ','} ${text}`);
}, '');
import { formatList } from './formatList';
formatList(['Me', 'myself', 'I']);
// Me, myself and I
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment