1- import { flatMap , map , split } from 'lodash-es'
1+ import { map } from 'lodash-es'
2+ import split from 'split-string'
23import type { InputChoiceProperties } from '../input/choice/types'
34import type { InputStringProperties } from '../input/string/types'
45import {
@@ -7,15 +8,54 @@ import {
78 type GenericVariable ,
89 InputType ,
910 type NormalizedStringValue ,
11+ type Settings ,
1012} from '../types'
13+ import { trimWhitespace , unquote } from './unquote'
14+
15+ function trimArrayWhitespace ( array : string [ ] ) : string [ ] {
16+ // Remove excess whitespace and newline strings from the beginning
17+ while ( array . length > 0 && / ^ \p{ Z} * $ / u. test ( array [ 0 ] ) ) {
18+ array . shift ( )
19+ }
20+
21+ // Remove excess whitespace and newline strings from the end
22+ while ( array . length > 0 && / ^ \p{ Z} * $ / u. test ( array [ array . length - 1 ] ) ) {
23+ array . pop ( )
24+ }
25+
26+ return array
27+ }
28+
29+ const normalizeVariableString = (
30+ value : string ,
31+ options : {
32+ repeat : boolean
33+ } & Pick < Settings , 'quotes' | 'separator' > ,
34+ ) : string [ ] => {
35+ const lines = trimArrayWhitespace ( value . split ( / \r ? \n / ) )
36+
37+ if ( lines . length > 1 ) {
38+ return [ lines . join ( '\n' ) ]
39+ }
40+
41+ const string = lines [ 0 ] ?? ''
42+
43+ if ( ! options . repeat ) {
44+ return [ unquote ( trimWhitespace ( string ) , options . quotes ) ]
45+ }
46+
47+ return split ( string , { quotes : options . quotes , separator : options . separator } ) . map ( ( value ) =>
48+ unquote ( value , options . quotes ) ,
49+ )
50+ }
1151
1252export function normalizeString (
1353 previousValues : DeNormalizedStringValue [ ] ,
1454 properties : InputChoiceProperties | InputStringProperties ,
1555) : NormalizedStringValue [ ] {
1656 const { repeat } = properties . model . state
1757
18- return flatMap ( previousValues , ( previousValue ) => {
58+ return map ( previousValues , ( previousValue ) => {
1959 if ( previousValue . type === InputType . Option ) {
2060 return repeat
2161 ? map (
@@ -27,15 +67,18 @@ export function normalizeString(
2767 )
2868 : ( previousValue as GenericOption < string > )
2969 } /* (previousValue.type === InputType.Variable) */ else {
30- return repeat
31- ? map (
32- split ( previousValue . value , properties . settings . split ) ,
33- ( value ) : GenericVariable < string > => ( {
34- ...previousValue ,
35- value,
36- } ) ,
37- )
38- : previousValue
70+ const asd : Array < GenericVariable < string > > = normalizeVariableString ( previousValue . value , {
71+ quotes : properties . settings . quotes ,
72+ repeat,
73+ separator : properties . settings . separator ,
74+ } ) . map (
75+ ( value ) : GenericVariable < string > => ( {
76+ ...previousValue ,
77+ value,
78+ } ) ,
79+ )
80+
81+ return asd
3982 }
40- } )
83+ } ) . flat ( )
4184}
0 commit comments