1+ import { prefixLines , stripLastNewLine } from '../utils' ;
2+
13/**
24 * Converts an array substitution to a string containing a list
3- * @param {String } [opts.separator = ''] - the character that separates each item
4- * @param {String } [opts.conjunction = ''] - replace the last separator with this
5- * @param {Boolean } [opts.serial = false] - include the separator before the conjunction? (Oxford comma use-case)
5+ * @param {String } [opts.separator = ''] - The character that separates each item
6+ * @param {String } [opts.conjunction = ''] - Replace the last separator with this
7+ * @param {Boolean } [opts.serial = false] - Include the separator before the conjunction? (Oxford comma use-case)
68 *
7- * @return {Object } - a TemplateTag transformer
9+ * @return {Object } - A transformer
810 */
911const inlineArrayTransformer = ( {
1012 conjunction = '' ,
@@ -17,20 +19,30 @@ const inlineArrayTransformer = ({
1719 return substitution ;
1820 }
1921
20- // be sure to maintain indentation
21- const indent = resultSoFar . match ( / ( \n ? [ ^ \S \n ] + ) $ / ) ;
22- const fullSeparator = separator . concat ( indent ? indent [ 1 ] : ' ' ) ;
23- const fullConjunction = '' . concat ( conjunction , ' ' ) ;
24- const conjunctionIndex = conjunction ? substitution . length - 1 : - 1 ;
22+ const { length } = substitution ;
23+ const lastSeparatorIndex = conjunction && ! serial ? length - 2 : length - 1 ;
24+ const indentation = resultSoFar . match ( / (?: \n ) ( [ ^ \S \n ] + ) $ / ) ;
25+
26+ if ( conjunction && length > 1 ) {
27+ substitution [ length - 1 ] = '' . concat (
28+ conjunction ,
29+ ' ' ,
30+ substitution [ length - 1 ] ,
31+ ) ;
32+ }
2533
26- return substitution . reduce ( ( result , part , index ) =>
27- '' . concat (
34+ return substitution . reduce ( ( result , part , index ) => {
35+ const isFirstPart = index === 0 ;
36+ const strippedPart = stripLastNewLine ( part ) ;
37+ return '' . concat (
2838 result ,
29- index !== conjunctionIndex || serial ? fullSeparator : ' ' ,
30- index === conjunctionIndex ? fullConjunction : '' ,
31- part ,
32- ) ,
33- ) ;
39+ isFirstPart ? '' : indentation ? '\n' : ' ' ,
40+ indentation
41+ ? prefixLines ( indentation [ 1 ] , strippedPart , isFirstPart )
42+ : strippedPart ,
43+ index < lastSeparatorIndex ? separator : '' ,
44+ ) ;
45+ } , '' ) ;
3446 } ,
3547} ) ;
3648
0 commit comments