vike
Version:
(Replaces Next.js/Nuxt) 🔨 Composable framework to build advanced applications with flexibility and stability.
13 lines (12 loc) • 577 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, { color = (s) => s, trailingComma = true } = {}) {
assert(arr.length > 0);
if (arr.length === 1)
return color(arr[0]);
const firsts = arr.slice(0, arr.length - 1);
const last = arr[arr.length - 1];
const lastComma = trailingComma && arr.length > 2 ? ',' : '';
return firsts.map(color).join(', ') + `${lastComma} ${conjunction} ` + color(last);
}