joinValues
joinValues
(separator: string) => (collection: Collection) => string
Joins collection values into a string
const toCommaList = joinValues(', ') toCommaList(['A', 'B']) // is A, B toCommaList({ a: 'A', b: 'B' }) // is A, B
const toCommaList = joinValues(', ') toCommaList(['A', 'B']) // is A, B toCommaList({ a: 'A', b: 'B' }) // is A, B
const toCommaList = joinValues(', ') toCommaList(['A', 'B']) // is A, B toCommaList({ a: 'A', b: 'B' }) // is A, B
const toCommaList = joinValues(', ') toCommaList(['A', 'B']) // is A, B toCommaList({ a: 'A', b: 'B' }) // is A, B
This is a generic version of
join
. We can use it to join strings of any collection. Below, Sam has a group chat going with Jen and Mike. Let's join their names to have an identifier for Sam's group chat selection.const groupChat = { 'jen@example.com': 'Jen', 'mike@example.com': 'Mike', } const toCommaList = joinValues(', ') const groupDisplay = toCommaList(groupChat) console.log(groupDisplay) // prints Jen, Mike
type GroupChat = Record<string, string> const groupChat: GroupChat = { 'jen@example.com': 'Jen', 'mike@example.com': 'Mike', } const toCommaList = joinValues(', ')<GroupChat> const groupDisplay = toCommaList(groupChat) console.log(groupDisplay) // prints Jen, Mike
const groupChat = { 'jen@example.com': 'Jen', 'mike@example.com': 'Mike', } const toCommaList = joinValues(', ') const groupDisplay = toCommaList(groupChat) console.log(groupDisplay) // prints Jen, Mike
type GroupChat = Record<string, string> const groupChat: GroupChat = { 'jen@example.com': 'Jen', 'mike@example.com': 'Mike', } const toCommaList = joinValues(', ')<GroupChat> const groupDisplay = toCommaList(groupChat) console.log(groupDisplay) // prints Jen, Mike