Implement a function listFormat(items, options) that joins an array of strings into a single formatted string.
The function should support an options object with the following properties:
sorted: boolean — if true, sort items alphabetically.length: number — show only the first length items, and append "and X other(s)" for remaining items.unique: boolean — remove duplicates.Examples
listFormat([]); // '' listFormat(['Bob']); // 'Bob' listFormat(['Bob', 'Alice']); // 'Bob and Alice' listFormat(['Bob', 'Ben', 'Tim', 'Jane', 'John']); // 'Bob, Ben, Tim, Jane and John' listFormat(['Bob', 'Ben', 'Tim', 'Jane', 'John'], { length: 3 }); // 'Bob, Ben, Tim and 2 others' listFormat(['Bob', 'Ben', 'Tim', 'Jane', 'John'], { length: 4 }); // 'Bob, Ben, Tim, Jane and 1 other' listFormat(['Bob', 'Ben', 'Tim', 'Jane', 'John'], { length: 3, sorted: true }); // 'Ben, Bob, Jane and 2 others' listFormat(['Bob', 'Ben', 'Tim', 'Jane', 'John', 'Bob'], { length: 3, unique: true }); // 'Bob, Ben, Tim and 2 others' listFormat(['Bob', 'Ben', '', '', 'John']); // 'Bob, Ben and John'
JavaScript Function
No test results yet
Click "Run" to execute tests