word-join
Version:
Tiny module to join arrays with commas, "and", and Oxford comma support
2 lines (1 loc) • 538 B
JavaScript
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(e="undefined"!=typeof globalThis?globalThis:e||self).wordJoin=n()}(this,(function(){"use strict";return function(e=[],n={}){const t=e.length;if(0===t)return"";if(1===t)return`${e[0]}`;const o=n.conjunction||"and";if(2===t)return`${e[0]} ${o} ${e[1]}`;const f=t-1,i=n.separator||",";let r=e[0];for(let n=1;n<f;n++)r+=`${i} ${e[n]}`;const u=` ${o} ${e[f]}`;return n.oxford?`${r}${i}${u}`:`${r}${u}`}}));