Skip to content

Commit 297f752

Browse files
committed
refactor: reformat and added internal docs
1 parent 297096d commit 297f752

File tree

1 file changed

+52
-31
lines changed

1 file changed

+52
-31
lines changed

src/properties.ts

+52-31
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ export const toMap = (config: Properties): Map<string, string> => {
140140
return result
141141
}
142142

143+
/**
144+
* Format key-value pair as a single line.
145+
*
146+
* @param key Key, can be empty string.
147+
* @param value Value, can be empty string.
148+
* @param sep Separator, cannot be empty. Valid chars are ` :=`.
149+
*/
143150
const formatLine = (key: string, value: string, sep: string) =>
144151
`${escapeKey(key)}${sep}${escapeValue(value)}`
145152

@@ -199,16 +206,21 @@ export const remove = (config: Properties, key: string): void =>
199206
*
200207
* @param lines Lines to iterate over.
201208
*/
202-
function* chars(lines: string[]): Generator<{char: string; line: number}> {
209+
function* chars(
210+
lines: string[]
211+
): Generator<{char: string; lineNumber: number}> {
203212
for (let i = 0; i < lines.length; i++) {
204213
const line = lines[i]
205214
for (const char of line) {
206-
yield {char, line: i}
215+
yield {char, lineNumber: i}
207216
}
208-
yield {char: 'EOL', line: i}
217+
yield {char: 'EOL', lineNumber: i}
209218
}
210219
}
211220

221+
/**
222+
* State for a simple state-machine inside listPairs.
223+
*/
212224
enum State {
213225
START,
214226
COMMENT,
@@ -217,35 +229,44 @@ enum State {
217229
VALUE
218230
}
219231

232+
/**
233+
* Create empty START state.
234+
*/
235+
const newState = (): {
236+
state: State
237+
start: number
238+
key: string
239+
sep: string
240+
value: string
241+
skipSpace: boolean
242+
escapedNext: boolean
243+
unicode?: string
244+
} => ({
245+
state: State.START,
246+
start: -1,
247+
key: '',
248+
sep: '',
249+
value: '',
250+
skipSpace: true,
251+
escapedNext: false
252+
})
253+
254+
/**
255+
* Parse and iterate over key-pairs.
256+
*
257+
* @param lines Lines to parse.
258+
* @return Parsed and unescaped key-value pairs.
259+
*/
220260
function* listPairs(lines: string[]): Generator<{
221261
start: number
222262
len: number
223263
sep: string
224264
key: string
225265
value: string
226266
}> {
227-
const newState = (): {
228-
state: State
229-
start: number
230-
key: string
231-
sep: string
232-
value: string
233-
skipSpace: boolean
234-
escapedNext: boolean
235-
unicode?: string
236-
} => ({
237-
state: State.START,
238-
start: -1,
239-
key: '',
240-
sep: '',
241-
value: '',
242-
skipSpace: true,
243-
escapedNext: false
244-
})
245-
246267
let state = newState()
247268

248-
for (const {char, line} of chars(lines)) {
269+
for (const {char, lineNumber} of chars(lines)) {
249270
// Simply ignore spaces
250271
if (state.skipSpace && char === ' ') {
251272
continue
@@ -256,7 +277,7 @@ function* listPairs(lines: string[]): Generator<{
256277
if (state.unicode) {
257278
// Handle incomplete sequence
258279
if (char === 'EOL') {
259-
throw new Error(`Invalid unicode sequence at line ${line}`)
280+
throw new Error(`Invalid unicode sequence at line ${lineNumber}`)
260281
}
261282

262283
// Append and consume until it has correct length
@@ -274,11 +295,11 @@ function* listPairs(lines: string[]): Generator<{
274295
case '#':
275296
case '!':
276297
state.state = State.COMMENT
277-
state.start = line
298+
state.start = lineNumber
278299
break
279300
default:
280301
state.state = State.KEY
281-
state.start = line
302+
state.start = lineNumber
282303
break
283304
}
284305
}
@@ -295,7 +316,7 @@ function* listPairs(lines: string[]): Generator<{
295316
if (state.state === State.KEY) {
296317
// Special unicode handling
297318
if (state.unicode) {
298-
state.key += parseUnicode(state.unicode, line)
319+
state.key += parseUnicode(state.unicode, lineNumber)
299320
state.unicode = undefined
300321
continue
301322
}
@@ -308,7 +329,7 @@ function* listPairs(lines: string[]): Generator<{
308329
state.skipSpace = true
309330
} else {
310331
// Value-less key
311-
yield {...state, len: line - state.start + 1}
332+
yield {...state, len: lineNumber - state.start + 1}
312333
state = newState()
313334
}
314335
break
@@ -358,7 +379,7 @@ function* listPairs(lines: string[]): Generator<{
358379
switch (char) {
359380
case 'EOL':
360381
// Value-less key
361-
yield {...state, len: line - state.start + 1}
382+
yield {...state, len: lineNumber - state.start + 1}
362383
state = newState()
363384
break
364385
case ' ':
@@ -386,7 +407,7 @@ function* listPairs(lines: string[]): Generator<{
386407
if (state.state === State.VALUE) {
387408
// Special unicode handling
388409
if (state.unicode) {
389-
state.value += parseUnicode(state.unicode, line)
410+
state.value += parseUnicode(state.unicode, lineNumber)
390411
state.unicode = undefined
391412
continue
392413
}
@@ -399,7 +420,7 @@ function* listPairs(lines: string[]): Generator<{
399420
state.skipSpace = true
400421
} else {
401422
// Value end
402-
yield {...state, len: line - state.start + 1}
423+
yield {...state, len: lineNumber - state.start + 1}
403424
state = newState()
404425
}
405426
break

0 commit comments

Comments
 (0)