Implement listFormat(items, options). It turns an array of strings into a readable English list, the kind you see in "Ada, Linus and 2 others liked this".
Basic format:
- no items:
"" - one item: the item itself,
"Ada" - two items:
"Ada and Linus" - three or more: commas between items and
andbefore the last, with no comma beforeand:"Ada, Linus and Grace"
Options are all optional. First drop empty strings, then apply the options in this order:
unique: trueremoves repeats, keeping each item's first occurrence.sorted: truesorts the items in ascending order with the defaultsort(), which compares UTF-16 code units.length: nshows only the firstnitems, thenand K others, whereKis how many were left out. UseotherwhenKis 1. Ifnis not a positive number smaller than the item count, ignore it.
Don't modify items, and don't use Intl.ListFormat.
Input: listFormat(['Ada', 'Linus', 'Grace'])
Output: 'Ada, Linus and Grace'
Input: listFormat(['Ada', 'Linus', 'Grace', 'Alan'], { length: 2 })
Output: 'Ada, Linus and 2 others'
Input: listFormat(['b', 'a', 'b', 'c'], { unique: true, sorted: true, length: 2 })
Output: 'a, b and 1 other'