Skip to content

Instantly share code, notes, and snippets.

@crazy4groovy
Last active April 1, 2021 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crazy4groovy/10084d298cfc5083117606bf7f1b17b4 to your computer and use it in GitHub Desktop.
Save crazy4groovy/10084d298cfc5083117606bf7f1b17b4 to your computer and use it in GitHub Desktop.
Human readable list concat from an Array (JavaScript)
// see: https://kentcdodds.com/blog/listify-a-java-script-array
function listify(
array,
{
type = 'conjunction',
style = 'long',
stringify = JSON.stringify,
lang = 'en'
} = {}
) {
const stringified = array.map(item => stringify(item))
const formatter = new Intl.ListFormat(lang, {style, type})
return formatter.format(stringified)
}
const to = `To: ${listify(mentionedMembersNicknames)}`
const didYouMean = `Did you mean ${listify(closeMatches, { type: 'disjunction' })}?`
console.log(to, didYouMean)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment