UNPKG

word-join

Version:

Tiny module to join arrays with commas, "and", and Oxford comma support

1 lines 1.41 kB
{"version":3,"file":"index.cjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["export default function wordJoin(\n words: unknown[] = [],\n options: {\n conjunction?: string;\n oxford?: boolean;\n separator?: string;\n } = {},\n): string {\n const wordCount = words.length;\n\n if (wordCount === 0) {\n return \"\";\n } else if (wordCount === 1) {\n return `${words[0]}`;\n }\n\n const conjunction = options.conjunction || \"and\";\n\n if (wordCount === 2) {\n return `${words[0]} ${conjunction} ${words[1]}`;\n }\n\n const lastIndex = wordCount - 1;\n const separator = options.separator || \",\";\n\n let start = words[0];\n\n for (let index = 1; index < lastIndex; index++) {\n /* skip first (1) and last (lastIndex) words */\n start += `${separator} ${words[index]}`;\n }\n\n const end = ` ${conjunction} ${words[lastIndex]}`;\n\n return options.oxford ? `${start}${separator}${end}` : `${start}${end}`;\n}\n"],"mappings":"+FAAA,SAAwB,EACtB,EAAmB,EAAE,CACrB,EAII,EAAE,CACE,CACR,IAAM,EAAY,EAAM,OAExB,GAAI,IAAc,EAChB,MAAO,MACE,IAAc,EACvB,MAAO,GAAG,EAAM,KAGlB,IAAM,EAAc,EAAQ,aAAe,MAE3C,GAAI,IAAc,EAChB,MAAO,GAAG,EAAM,GAAG,GAAG,EAAY,GAAG,EAAM,KAG7C,IAAM,EAAY,EAAY,EACxB,EAAY,EAAQ,WAAa,IAEnC,EAAQ,EAAM,GAElB,IAAK,IAAI,EAAQ,EAAG,EAAQ,EAAW,IAErC,GAAS,GAAG,EAAU,GAAG,EAAM,KAGjC,IAAM,EAAM,IAAI,EAAY,GAAG,EAAM,KAErC,OAAO,EAAQ,OAAS,GAAG,IAAQ,IAAY,IAAQ,GAAG,IAAQ"}