forked from coracle/flotilla
14 lines
445 B
TypeScript
14 lines
445 B
TypeScript
export const displayList = <T>(xs: T[], conj = "and", n = 6, locale = "en-US") => {
|
|
const stringItems = xs.map(String)
|
|
|
|
if (xs.length > n + 2) {
|
|
const formattedList = new Intl.ListFormat(locale, {style: "long", type: "unit"}).format(
|
|
stringItems.slice(0, n),
|
|
)
|
|
|
|
return `${formattedList}, ${conj} ${xs.length - n} others`
|
|
}
|
|
|
|
return new Intl.ListFormat(locale, {style: "long", type: "conjunction"}).format(stringItems)
|
|
}
|