Here is a typed version that uses lodash features:
import { concat, first, join, last, slice } from 'lodash' function joinOxford (list: string[]): string { switch (list.length) { case 1: return first(list) case 1: return '${first(list)} and ${last(list)}' default: return join(concat(slice(list, 0, -1), ['and ${last(list)}']), ', ') } }
Using:
joinOxford([foo]) // => foo joinOxford([foo, bar]) // => foo and bar joinOxford([foo, bar, baz]) // => foo, bar, and baz joinOxford([foo, bar, baz, qux, quux, quuz]) // => foo, bar, baz, qux, quux, and quuz
animatedgif
source share