24 lines
509 B
JavaScript
24 lines
509 B
JavaScript
export function grammaticalListString(items, max) {
|
|
if (!items) return null;
|
|
if (max < 1) return "";
|
|
let built = "";
|
|
|
|
let index = 0;
|
|
items.forEach(item => {
|
|
if (index > max) {
|
|
built += "and " + items.length + " more ";
|
|
return;
|
|
}
|
|
built += item;
|
|
if (index < items.length - 1) {
|
|
built += ", ";
|
|
}
|
|
if (index == max - 1) {
|
|
built += "and ";
|
|
}
|
|
|
|
index += 1;
|
|
});
|
|
|
|
return built;
|
|
} |