Format names as Ana, Ben and 3 others with limit options
Shared folders, comment threads, and reaction tooltips all show who is involved in a short sentence: “Ana, Ben and 3 others”. Implement formatNameList(names, options), which turns an array of names into that kind of readable string.
formatNameList(names, options?) // => string
limit: a positive integer, the maximum number of names to show. Default: show every name.conjunction: the word before the last part. Default: 'and'.oxfordComma: when true, a list of three or more parts gets a comma before the conjunction. Default: false.Any option may be missing, and options itself may be omitted.
limit, keep the first limit names and add one final part for the rest: "1 other" when one name is hidden, "N others" otherwise.''."A and B" (never a comma, even with oxfordComma).", ", then " and " and the last part. With oxfordComma, use ", and " before the last part.Replace and with the conjunction option in every case above.
formatNameList(['Ana']); // => 'Ana'
formatNameList(['Ana', 'Ben']); // => 'Ana and Ben'
formatNameList(['Ana', 'Ben', 'Cleo']); // => 'Ana, Ben and Cleo'
formatNameList(['Ana', 'Ben', 'Cleo'], { oxfordComma: true });
// => 'Ana, Ben, and Cleo'
formatNameList(['Ana', 'Ben', 'Cleo', 'Dev', 'Eli'], { limit: 2 });
// => 'Ana, Ben and 3 others'
formatNameList(['Ana', 'Ben'], { limit: 1 });
// => 'Ana and 1 other'
formatNameList(['Tea', 'Coffee'], { conjunction: 'or' });
// => 'Tea or Coffee'
formatNameList([' Ana ', '', 'Ana', ' ', 'ana']);
// => 'Ana and ana'
formatNameList([]); // => ''
names is a string.Intl.ListFormat, and do not mutate names.