@@ -8,18 +8,20 @@ const encode = (obj, opt = {}) => {
88 opt . newline = opt . newline === true
99 /* istanbul ignore next */
1010 opt . platform = opt . platform || process ?. platform
11+ opt . bracketedArray = opt . bracketedArray !== false
1112
1213 /* istanbul ignore next */
1314 const eol = opt . platform === 'win32' ? '\r\n' : '\n'
1415 const separator = opt . whitespace ? ' = ' : '='
1516 const children = [ ]
1617 let out = ''
18+ const arraySuffix = opt . bracketedArray ? '[]' : ''
1719
1820 for ( const k of Object . keys ( obj ) ) {
1921 const val = obj [ k ]
2022 if ( val && Array . isArray ( val ) ) {
2123 for ( const item of val ) {
22- out += safe ( k + '[]' ) + separator + safe ( item ) + eol
24+ out += safe ( ` ${ k } ${ arraySuffix } ` ) + separator + safe ( item ) + eol
2325 }
2426 } else if ( val && typeof val === 'object' ) {
2527 children . push ( k )
@@ -75,13 +77,15 @@ function splitSections (str, separator) {
7577 return sections
7678}
7779
78- const decode = str => {
80+ const decode = ( str , opt = { } ) => {
81+ opt . bracketedArray = opt . bracketedArray !== false
7982 const out = Object . create ( null )
8083 let p = out
8184 let section = null
8285 // section |key = value
8386 const re = / ^ \[ ( [ ^ \] ] * ) \] \s * $ | ^ ( [ ^ = ] + ) ( = ( .* ) ) ? $ / i
8487 const lines = str . split ( / [ \r \n ] + / g)
88+ const duplicates = { }
8589
8690 for ( const line of lines ) {
8791 if ( ! line || line . match ( / ^ \s * [ ; # ] / ) || line . match ( / ^ \s * $ / ) ) {
@@ -103,7 +107,13 @@ const decode = str => {
103107 continue
104108 }
105109 const keyRaw = unsafe ( match [ 2 ] )
106- const isArray = keyRaw . length > 2 && keyRaw . slice ( - 2 ) === '[]'
110+ let isArray
111+ if ( opt . bracketedArray ) {
112+ isArray = keyRaw . length > 2 && keyRaw . slice ( - 2 ) === '[]'
113+ } else {
114+ duplicates [ keyRaw ] = ( duplicates ?. [ keyRaw ] || 0 ) + 1
115+ isArray = duplicates [ keyRaw ] > 1
116+ }
107117 const key = isArray ? keyRaw . slice ( 0 , - 2 ) : keyRaw
108118 if ( key === '__proto__' ) {
109119 continue
0 commit comments