vike
Version:
The Framework *You* Control - Next.js & Nuxt alternative for unprecedented flexibility and dependability.
13 lines (12 loc) • 545 B
JavaScript
export { joinEnglish };
import { assert } from './assert.js';
// https://stackoverflow.com/questions/53879088/join-an-array-by-commas-and-and/53879103#53879103
function joinEnglish(arr, conjunction, colorizer = (s) => s) {
assert(arr.length > 0);
if (arr.length === 1)
return colorizer(arr[0]);
const firsts = arr.slice(0, arr.length - 1);
const last = arr[arr.length - 1];
const lastComma = arr.length > 2 ? ',' : '';
return firsts.map(colorizer).join(', ') + `${lastComma} ${conjunction} ` + colorizer(last);
}