Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions packages/toon/src/decode/decoders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,10 @@ function* decodeKeyValueSync(
}

// Regular key-value pair
const { key, isQuoted } = withLine(line, () => parseKeyToken(content, 0))
const colonIndex = content.indexOf(COLON, key.length)
const rest = colonIndex >= 0 ? content.slice(colonIndex + 1).trim() : ''
// Use `end` from parseKeyToken directly — it already points to the character
// after the colon separator, avoiding a fragile secondary indexOf search.
const { key, end, isQuoted } = withLine(line, () => parseKeyToken(content, 0))
const rest = content.slice(end).trim()

yield isQuoted ? { type: 'key', key, wasQuoted: true } : { type: 'key', key }

Expand Down Expand Up @@ -616,9 +617,10 @@ async function* decodeKeyValueAsync(
}

// Regular key-value pair
const { key, isQuoted } = withLine(line, () => parseKeyToken(content, 0))
const colonIndex = content.indexOf(COLON, key.length)
const rest = colonIndex >= 0 ? content.slice(colonIndex + 1).trim() : ''
// Use `end` from parseKeyToken directly — it already points to the character
// after the colon separator, avoiding a fragile secondary indexOf search.
const { key, end, isQuoted } = withLine(line, () => parseKeyToken(content, 0))
const rest = content.slice(end).trim()

yield isQuoted ? { type: 'key', key, wasQuoted: true } : { type: 'key', key }

Expand Down
14 changes: 11 additions & 3 deletions packages/toon/src/decode/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,14 @@ export function parseBracketSegment(
content = content.slice(0, -1)
}

// Reject non-decimal-integer strings: hex (0x…), floats (3.7), leading
// junk, etc. parseInt is too permissive — it accepts all of these.
if (!/^-?\d+$/.test(content)) {
throw new TypeError(`Invalid array length: ${seg}`)
}

const length = Number.parseInt(content, 10)
if (Number.isNaN(length)) {
if (Number.isNaN(length) || length < 0) {
throw new TypeError(`Invalid array length: ${seg}`)
}

Expand Down Expand Up @@ -191,8 +197,10 @@ export function parseDelimitedValues(input: string, delimiter: Delimiter): strin
i++
}

// Add last value
if (valueBuffer || values.length > 0) {
// Add last value only when there is actual content or a non-trailing
// delimiter was seen. A trailing delimiter (e.g. "a,b,") must not produce
// a spurious empty final element.
if (valueBuffer.trim() || values.length === 0) {
values.push(valueBuffer.trim())
}

Expand Down
5 changes: 5 additions & 0 deletions packages/toon/src/encode/encoders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ export function* encodeArrayOfArraysAsListItemsLines(
const arrayLine = encodeInlineArrayLine(arr, options.delimiter)
yield indentedListItem(depth + 1, arrayLine, options.indent)
}
else {
// Non-primitive sub-arrays fall back to the mixed/expanded path so no
// rows are silently dropped from the declared header count.
yield* encodeMixedArrayAsListItemsLines(undefined, arr, depth + 1, options)
}
}
}

Expand Down