@@ -93,8 +93,16 @@ export function* list(config: Properties): Generator<KeyValuePair> {
93
93
* @return Found value, or undefined. Value is properly unescaped.
94
94
*/
95
95
export const get = ( config : Properties , key : string ) : string | undefined => {
96
- // Find existing
97
- const { value} = findValue ( config . lines , key )
96
+ let value : string | undefined = undefined
97
+
98
+ // Find last value
99
+ for ( const entry of listPairs ( config . lines ) ) {
100
+ // If found, remember it
101
+ if ( key === entry . key ) {
102
+ value = entry . value
103
+ }
104
+ }
105
+
98
106
return value
99
107
}
100
108
@@ -132,6 +140,9 @@ export const toMap = (config: Properties): Map<string, string> => {
132
140
return result
133
141
}
134
142
143
+ const formatLine = ( key : string , value : string , sep : string ) =>
144
+ `${ escapeKey ( key ) } ${ sep } ${ escapeValue ( value ) } `
145
+
135
146
/**
136
147
* Set or remove value for the given key.
137
148
*
@@ -146,22 +157,29 @@ export const set = (
146
157
value : string | undefined | null ,
147
158
options ?: { separator ?: string }
148
159
) : void => {
149
- // Find existing
150
- const { start, len, sep} = findValue ( config . lines , key )
151
-
152
- // Prepare value
153
- const items =
154
- typeof value === 'string'
155
- ? [ `${ escapeKey ( key ) } ${ options ?. separator || sep || '=' } ${ escapeValue ( value ) } ` ]
156
- : [ ]
157
-
158
- // If found
159
- if ( start >= 0 && len > 0 ) {
160
- // Replace
161
- config . lines . splice ( start , len , ...items )
162
- } else {
163
- // Not found, append
164
- config . lines . push ( ...items )
160
+ let sep = '='
161
+ let found = false
162
+
163
+ // Find all entries
164
+ for ( const entry of listPairs ( config . lines ) ) {
165
+ // Remember separator
166
+ if ( entry . sep ) sep = entry . sep
167
+
168
+ // If found, either replace or remove
169
+ if ( key === entry . key ) {
170
+ const items =
171
+ ! found && typeof value === 'string'
172
+ ? [ formatLine ( key , value , options ?. separator || sep ) ]
173
+ : [ ]
174
+
175
+ config . lines . splice ( entry . start , entry . len , ...items )
176
+ found = true
177
+ }
178
+ }
179
+
180
+ // Not found, append
181
+ if ( ! found && typeof value === 'string' ) {
182
+ config . lines . push ( formatLine ( key , value , options ?. separator || sep ) )
165
183
}
166
184
}
167
185
@@ -177,29 +195,10 @@ export const remove = (config: Properties, key: string): void =>
177
195
set ( config , key , undefined )
178
196
179
197
/**
180
- * Find value indices .
198
+ * Character iterator over lines of chars .
181
199
*
182
- * @param lines Lines array.
183
- * @param key Key to be found.
200
+ * @param lines Lines to iterate over.
184
201
*/
185
- const findValue = (
186
- lines : string [ ] ,
187
- key : string
188
- ) : { start : number ; len : number ; sep : string ; value ?: string } => {
189
- let sep = '='
190
- for ( const entry of listPairs ( lines ) ) {
191
- // Remember separator
192
- if ( entry . sep ) sep = entry . sep
193
- // Return found value
194
- if ( key === entry . key ) {
195
- return entry
196
- }
197
- }
198
-
199
- // Not found
200
- return { start : - 1 , len : 0 , sep}
201
- }
202
-
203
202
function * chars ( lines : string [ ] ) : Generator < { char : string , line : number } > {
204
203
for ( let i = 0 ; i < lines . length ; i ++ ) {
205
204
const line = lines [ i ]
@@ -337,7 +336,7 @@ function* listPairs(lines: string[]): Generator<{
337
336
state . unicode = '0x'
338
337
} else {
339
338
// Special char
340
- state . key += unescapeChar ( char )
339
+ state . key += unescapeControlChar ( char )
341
340
}
342
341
} else {
343
342
// Normal char
@@ -415,7 +414,7 @@ function* listPairs(lines: string[]): Generator<{
415
414
state . unicode = '0x'
416
415
} else {
417
416
// Special char
418
- state . value += unescapeChar ( char )
417
+ state . value += unescapeControlChar ( char )
419
418
}
420
419
} else {
421
420
// Normal char
@@ -427,8 +426,7 @@ function* listPairs(lines: string[]): Generator<{
427
426
}
428
427
}
429
428
430
- // Very simple implementation
431
- const unescapeChar = ( c : string ) : string => {
429
+ const unescapeControlChar = ( c : string ) : string => {
432
430
switch ( c ) {
433
431
case 'r' :
434
432
return '\r'
0 commit comments