Last active
April 1, 2021 14:51
-
-
Save crazy4groovy/10084d298cfc5083117606bf7f1b17b4 to your computer and use it in GitHub Desktop.
Human readable list concat from an Array (JavaScript)
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
// 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