refactor: remove unused decoders functions

This commit is contained in:
Johann Schopplich
2025-10-29 07:49:35 +01:00
parent 45604b06e8
commit ab95699c30

View File

@@ -37,7 +37,7 @@ export function decodeValueFromLines(cursor: LineCursor, options: ResolvedDecode
const headerInfo = parseArrayHeaderLine(first.content, DEFAULT_DELIMITER) const headerInfo = parseArrayHeaderLine(first.content, DEFAULT_DELIMITER)
if (headerInfo) { if (headerInfo) {
cursor.advance() // Move past the header line cursor.advance() // Move past the header line
return decodeArrayFromHeader(headerInfo.header, first, cursor, 0, options) return decodeArrayFromHeader(headerInfo.header, headerInfo.inlineValues, cursor, 0, options)
} }
} }
@@ -51,9 +51,7 @@ export function decodeValueFromLines(cursor: LineCursor, options: ResolvedDecode
} }
function isRootArrayHeaderLine(line: ParsedLine): boolean { function isRootArrayHeaderLine(line: ParsedLine): boolean {
const content = line.content.trim() return isArrayHeaderAfterHyphen(line.content)
// Root array: starts with [ and has a colon
return content.startsWith('[') && content.includes(COLON)
} }
function isKeyValueLine(line: ParsedLine): boolean { function isKeyValueLine(line: ParsedLine): boolean {
@@ -106,6 +104,44 @@ function decodeObject(cursor: LineCursor, baseDepth: Depth, options: ResolvedDec
return obj return obj
} }
function decodeKeyValue(
content: string,
cursor: LineCursor,
baseDepth: Depth,
options: ResolvedDecodeOptions,
): { key: string, value: JsonValue, followDepth: Depth } {
// Check for array header first (before parsing key)
const arrayHeader = parseArrayHeaderLine(content, DEFAULT_DELIMITER)
if (arrayHeader && arrayHeader.header.key) {
const value = decodeArrayFromHeader(arrayHeader.header, arrayHeader.inlineValues, cursor, baseDepth, options)
// After an array, subsequent fields are at baseDepth + 1 (where array content is)
return {
key: arrayHeader.header.key,
value,
followDepth: baseDepth + 1,
}
}
// Regular key-value pair
const { key, end } = parseKeyToken(content, 0)
const rest = content.slice(end).trim()
// No value after colon - expect nested object or empty
if (!rest) {
const nextLine = cursor.peek()
if (nextLine && nextLine.depth > baseDepth) {
const nested = decodeObject(cursor, baseDepth + 1, options)
return { key, value: nested, followDepth: baseDepth + 1 }
}
// Empty object
return { key, value: {}, followDepth: baseDepth + 1 }
}
// Inline primitive value
const value = parsePrimitiveToken(rest)
return { key, value, followDepth: baseDepth + 1 }
}
function decodeKeyValuePair( function decodeKeyValuePair(
line: ParsedLine, line: ParsedLine,
cursor: LineCursor, cursor: LineCursor,
@@ -113,58 +149,25 @@ function decodeKeyValuePair(
options: ResolvedDecodeOptions, options: ResolvedDecodeOptions,
): [key: string, value: JsonValue] { ): [key: string, value: JsonValue] {
cursor.advance() cursor.advance()
const { key, value } = decodeKeyValue(line.content, cursor, baseDepth, options)
// Check for array header first (before parsing key)
const arrayHeader = parseArrayHeaderLine(line.content, DEFAULT_DELIMITER)
if (arrayHeader && arrayHeader.header.key) {
const value = decodeArrayFromHeader(arrayHeader.header, line, cursor, baseDepth, options)
return [arrayHeader.header.key, value]
}
// Regular key-value pair
const { key, end } = parseKeyToken(line.content, 0)
const rest = line.content.slice(end).trim()
// No value after colon - expect nested object or empty
if (!rest) {
const nextLine = cursor.peek()
if (nextLine && nextLine.depth > baseDepth) {
const nested = expectNestedObject(cursor, baseDepth + 1, options)
return [key, nested]
}
// Empty object
return [key, {}]
}
// Inline primitive value
const value = parsePrimitiveToken(rest)
return [key, value] return [key, value]
} }
function expectNestedObject(cursor: LineCursor, nestedDepth: Depth, options: ResolvedDecodeOptions): JsonObject {
return decodeObject(cursor, nestedDepth, options)
}
// #endregion // #endregion
// #region Array decoding // #region Array decoding
function decodeArrayFromHeader( function decodeArrayFromHeader(
header: ArrayHeaderInfo, header: ArrayHeaderInfo,
line: ParsedLine, inlineValues: string | undefined,
cursor: LineCursor, cursor: LineCursor,
baseDepth: Depth, baseDepth: Depth,
options: ResolvedDecodeOptions, options: ResolvedDecodeOptions,
): JsonArray { ): JsonArray {
const arrayHeader = parseArrayHeaderLine(line.content, DEFAULT_DELIMITER)
if (!arrayHeader) {
throw new Error('Invalid array header')
}
// Inline primitive array // Inline primitive array
if (arrayHeader.inlineValues) { if (inlineValues) {
// For inline arrays, cursor should already be advanced or will be by caller // For inline arrays, cursor should already be advanced or will be by caller
return decodeInlinePrimitiveArray(header, arrayHeader.inlineValues, options) return decodeInlinePrimitiveArray(header, inlineValues, options)
} }
// For multi-line arrays (tabular or list), the cursor should already be positioned // For multi-line arrays (tabular or list), the cursor should already be positioned
@@ -321,7 +324,7 @@ function decodeListItem(
if (isArrayHeaderAfterHyphen(afterHyphen)) { if (isArrayHeaderAfterHyphen(afterHyphen)) {
const arrayHeader = parseArrayHeaderLine(afterHyphen, activeDelimiter as any) const arrayHeader = parseArrayHeaderLine(afterHyphen, activeDelimiter as any)
if (arrayHeader) { if (arrayHeader) {
return decodeArrayFromHeader(arrayHeader.header, line, cursor, baseDepth, options) return decodeArrayFromHeader(arrayHeader.header, arrayHeader.inlineValues, cursor, baseDepth, options)
} }
} }
@@ -370,40 +373,7 @@ function decodeFirstFieldOnHyphen(
baseDepth: Depth, baseDepth: Depth,
options: ResolvedDecodeOptions, options: ResolvedDecodeOptions,
): { key: string, value: JsonValue, followDepth: Depth } { ): { key: string, value: JsonValue, followDepth: Depth } {
// Check for array header as first field return decodeKeyValue(rest, cursor, baseDepth, options)
const arrayHeader = parseArrayHeaderLine(rest, DEFAULT_DELIMITER)
if (arrayHeader) {
// Create a synthetic line for array decoding
const syntheticLine: ParsedLine = {
raw: rest,
content: rest,
indent: baseDepth * options.indent,
depth: baseDepth,
}
const value = decodeArrayFromHeader(arrayHeader.header, syntheticLine, cursor, baseDepth, options)
// After an array, subsequent fields are at baseDepth + 1 (where array content is)
return {
key: arrayHeader.header.key!,
value,
followDepth: baseDepth + 1,
}
}
// Regular key-value pair
const { key, end } = parseKeyToken(rest, 0)
const afterKey = rest.slice(end).trim()
if (!afterKey) {
// Nested object
const nested = expectNestedObject(cursor, baseDepth + 1, options)
return { key, value: nested, followDepth: baseDepth + 1 }
}
// Inline primitive
const value = parsePrimitiveToken(afterKey)
return { key, value, followDepth: baseDepth + 1 }
} }
// #endregion // #endregion