Skip to content

Commit 4cf1af7

Browse files
committed
refactor: prepare for optional unicode encoding on set
1 parent 5f0254a commit 4cf1af7

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/properties.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,10 @@ export const toMap = (config: Properties): Map<string, string> => {
156156
* @param key Key, can be empty string.
157157
* @param value Value, can be empty string.
158158
* @param sep Separator, cannot be empty. Valid chars are ` :=`.
159+
* @param escapeUnicode Enable/disable unicode escaping.
159160
*/
160-
const formatLine = (key: string, value: string, sep: string) =>
161-
`${escapeKey(key)}${sep}${escapeValue(value)}`
161+
const formatLine = (key: string, value: string, sep: string, escapeUnicode: boolean) =>
162+
`${escapeKey(key, escapeUnicode)}${sep}${escapeValue(value, escapeUnicode)}`
162163

163164
/**
164165
* Set or remove value for the given key.
@@ -174,19 +175,20 @@ export const set = (
174175
value: string | undefined | null,
175176
options?: {separator?: string}
176177
): void => {
177-
let sep = '='
178+
const escapeUnicode = true
179+
let lastSep = '='
178180
let found = false
179181

180182
// Find all entries
181183
for (const entry of listPairs(config.lines)) {
182184
// Remember separator
183-
if (entry.sep) sep = entry.sep
185+
if (entry.sep) lastSep = entry.sep
184186

185187
// If found, either replace or remove
186188
if (key === entry.key) {
187189
const items =
188190
!found && typeof value === 'string'
189-
? [formatLine(key, value, options?.separator || sep)]
191+
? [formatLine(key, value, options?.separator || lastSep, escapeUnicode)]
190192
: []
191193

192194
config.lines.splice(entry.start, entry.len, ...items)
@@ -196,7 +198,7 @@ export const set = (
196198

197199
// Not found, append
198200
if (!found && typeof value === 'string') {
199-
config.lines.push(formatLine(key, value, options?.separator || sep))
201+
config.lines.push(formatLine(key, value, options?.separator || lastSep, escapeUnicode))
200202
}
201203
}
202204

0 commit comments

Comments
 (0)