refactor: internal function renamings

This commit is contained in:
Johann Schopplich
2025-10-29 08:04:58 +01:00
parent ab95699c30
commit bb00d76765
4 changed files with 22 additions and 22 deletions

View File

@@ -17,11 +17,11 @@ import {
import { import {
isArrayHeaderAfterHyphen, isArrayHeaderAfterHyphen,
isObjectFirstFieldAfterHyphen, isObjectFirstFieldAfterHyphen,
mapRowValuesToPrimitives,
parseArrayHeaderLine, parseArrayHeaderLine,
parseDelimitedValues,
parseKeyToken, parseKeyToken,
parsePrimitiveToken, parsePrimitiveToken,
parseRowValuesToPrimitives,
splitDelimitedValues,
} from './parser' } from './parser'
// #region Entry decoding // #region Entry decoding
@@ -192,8 +192,8 @@ function decodeInlinePrimitiveArray(
return [] return []
} }
const values = splitDelimitedValues(inlineValues, header.delimiter) const values = parseDelimitedValues(inlineValues, header.delimiter)
const primitives = parseRowValuesToPrimitives(values) const primitives = mapRowValuesToPrimitives(values)
assertExpectedCount(primitives.length, header.length, 'inline array items', options) assertExpectedCount(primitives.length, header.length, 'inline array items', options)
@@ -254,10 +254,10 @@ function decodeTabularArray(
if (line.depth === rowDepth) { if (line.depth === rowDepth) {
cursor.advance() cursor.advance()
const values = splitDelimitedValues(line.content, header.delimiter) const values = parseDelimitedValues(line.content, header.delimiter)
assertExpectedCount(values.length, header.fields!.length, 'tabular row values', options) assertExpectedCount(values.length, header.fields!.length, 'tabular row values', options)
const primitives = parseRowValuesToPrimitives(values) const primitives = mapRowValuesToPrimitives(values)
const obj: JsonObject = {} const obj: JsonObject = {}
for (let i = 0; i < header.fields!.length; i++) { for (let i = 0; i < header.fields!.length; i++) {

View File

@@ -16,10 +16,10 @@ import {
isJsonPrimitive, isJsonPrimitive,
} from './normalize' } from './normalize'
import { import {
encodeAndJoinPrimitives,
encodeKey, encodeKey,
encodePrimitive, encodePrimitive,
formatHeader, formatHeader,
joinEncodedValues,
} from './primitives' } from './primitives'
import { LineWriter } from './writer' import { LineWriter } from './writer'
@@ -110,7 +110,7 @@ export function encodeArray(
// Array of objects // Array of objects
if (isArrayOfObjects(value)) { if (isArrayOfObjects(value)) {
const header = detectTabularHeader(value) const header = extractTabularHeader(value)
if (header) { if (header) {
encodeArrayOfObjectsAsTabular(key, value, header, writer, depth, options) encodeArrayOfObjectsAsTabular(key, value, header, writer, depth, options)
} }
@@ -135,7 +135,7 @@ export function encodeInlinePrimitiveArray(
depth: Depth, depth: Depth,
options: ResolvedEncodeOptions, options: ResolvedEncodeOptions,
): void { ): void {
const formatted = formatInlineArray(values, options.delimiter, prefix, options.lengthMarker) const formatted = encodeInlineArrayLine(values, options.delimiter, prefix, options.lengthMarker)
writer.push(depth, formatted) writer.push(depth, formatted)
} }
@@ -155,15 +155,15 @@ export function encodeArrayOfArraysAsListItems(
for (const arr of values) { for (const arr of values) {
if (isArrayOfPrimitives(arr)) { if (isArrayOfPrimitives(arr)) {
const inline = formatInlineArray(arr, options.delimiter, undefined, options.lengthMarker) const inline = encodeInlineArrayLine(arr, options.delimiter, undefined, options.lengthMarker)
writer.push(depth + 1, `${LIST_ITEM_PREFIX}${inline}`) writer.push(depth + 1, `${LIST_ITEM_PREFIX}${inline}`)
} }
} }
} }
export function formatInlineArray(values: readonly JsonPrimitive[], delimiter: string, prefix?: string, lengthMarker?: '#' | false): string { export function encodeInlineArrayLine(values: readonly JsonPrimitive[], delimiter: string, prefix?: string, lengthMarker?: '#' | false): string {
const header = formatHeader(values.length, { key: prefix, delimiter, lengthMarker }) const header = formatHeader(values.length, { key: prefix, delimiter, lengthMarker })
const joinedValue = joinEncodedValues(values, delimiter) const joinedValue = encodeAndJoinPrimitives(values, delimiter)
// Only add space if there are values // Only add space if there are values
if (values.length === 0) { if (values.length === 0) {
return header return header
@@ -189,7 +189,7 @@ export function encodeArrayOfObjectsAsTabular(
writeTabularRows(rows, header, writer, depth + 1, options) writeTabularRows(rows, header, writer, depth + 1, options)
} }
export function detectTabularHeader(rows: readonly JsonObject[]): string[] | undefined { export function extractTabularHeader(rows: readonly JsonObject[]): string[] | undefined {
if (rows.length === 0) if (rows.length === 0)
return return
@@ -238,7 +238,7 @@ function writeTabularRows(
): void { ): void {
for (const row of rows) { for (const row of rows) {
const values = header.map(key => row[key]) const values = header.map(key => row[key])
const joinedValue = joinEncodedValues(values as JsonPrimitive[], options.delimiter) const joinedValue = encodeAndJoinPrimitives(values as JsonPrimitive[], options.delimiter)
writer.push(depth, joinedValue) writer.push(depth, joinedValue)
} }
} }
@@ -265,7 +265,7 @@ export function encodeMixedArrayAsListItems(
else if (isJsonArray(item)) { else if (isJsonArray(item)) {
// Direct array as list item // Direct array as list item
if (isArrayOfPrimitives(item)) { if (isArrayOfPrimitives(item)) {
const inline = formatInlineArray(item, options.delimiter, undefined, options.lengthMarker) const inline = encodeInlineArrayLine(item, options.delimiter, undefined, options.lengthMarker)
writer.push(depth + 1, `${LIST_ITEM_PREFIX}${inline}`) writer.push(depth + 1, `${LIST_ITEM_PREFIX}${inline}`)
} }
} }
@@ -294,12 +294,12 @@ export function encodeObjectAsListItem(obj: JsonObject, writer: LineWriter, dept
else if (isJsonArray(firstValue)) { else if (isJsonArray(firstValue)) {
if (isArrayOfPrimitives(firstValue)) { if (isArrayOfPrimitives(firstValue)) {
// Inline format for primitive arrays // Inline format for primitive arrays
const formatted = formatInlineArray(firstValue, options.delimiter, firstKey, options.lengthMarker) const formatted = encodeInlineArrayLine(firstValue, options.delimiter, firstKey, options.lengthMarker)
writer.push(depth, `${LIST_ITEM_PREFIX}${formatted}`) writer.push(depth, `${LIST_ITEM_PREFIX}${formatted}`)
} }
else if (isArrayOfObjects(firstValue)) { else if (isArrayOfObjects(firstValue)) {
// Check if array of objects can use tabular format // Check if array of objects can use tabular format
const header = detectTabularHeader(firstValue) const header = extractTabularHeader(firstValue)
if (header) { if (header) {
// Tabular format for uniform arrays of objects // Tabular format for uniform arrays of objects
const headerStr = formatHeader(firstValue.length, { key: firstKey, fields: header, delimiter: options.delimiter, lengthMarker: options.lengthMarker }) const headerStr = formatHeader(firstValue.length, { key: firstKey, fields: header, delimiter: options.delimiter, lengthMarker: options.lengthMarker })
@@ -324,7 +324,7 @@ export function encodeObjectAsListItem(obj: JsonObject, writer: LineWriter, dept
writer.push(depth + 1, `${LIST_ITEM_PREFIX}${encodePrimitive(item, options.delimiter)}`) writer.push(depth + 1, `${LIST_ITEM_PREFIX}${encodePrimitive(item, options.delimiter)}`)
} }
else if (isJsonArray(item) && isArrayOfPrimitives(item)) { else if (isJsonArray(item) && isArrayOfPrimitives(item)) {
const inline = formatInlineArray(item, options.delimiter, undefined, options.lengthMarker) const inline = encodeInlineArrayLine(item, options.delimiter, undefined, options.lengthMarker)
writer.push(depth + 1, `${LIST_ITEM_PREFIX}${inline}`) writer.push(depth + 1, `${LIST_ITEM_PREFIX}${inline}`)
} }
else if (isJsonObject(item)) { else if (isJsonObject(item)) {

View File

@@ -134,14 +134,14 @@ export function parseBracketSegment(
} }
export function parseFieldsSegment(seg: string, delimiter: Delimiter): string[] { export function parseFieldsSegment(seg: string, delimiter: Delimiter): string[] {
return splitDelimitedValues(seg, delimiter).map(field => parseStringLiteral(field.trim())) return parseDelimitedValues(seg, delimiter).map(field => parseStringLiteral(field.trim()))
} }
// #endregion // #endregion
// #region Delimited value parsing // #region Delimited value parsing
export function splitDelimitedValues(input: string, delimiter: Delimiter): string[] { export function parseDelimitedValues(input: string, delimiter: Delimiter): string[] {
const values: string[] = [] const values: string[] = []
let current = '' let current = ''
let inQuotes = false let inQuotes = false
@@ -183,7 +183,7 @@ export function splitDelimitedValues(input: string, delimiter: Delimiter): strin
return values return values
} }
export function parseRowValuesToPrimitives(values: string[]): JsonPrimitive[] { export function mapRowValuesToPrimitives(values: string[]): JsonPrimitive[] {
return values.map(v => parsePrimitiveToken(v)) return values.map(v => parsePrimitiveToken(v))
} }

View File

@@ -124,7 +124,7 @@ function isValidUnquotedKey(key: string): boolean {
// #region Value joining // #region Value joining
export function joinEncodedValues(values: readonly JsonPrimitive[], delimiter: string = COMMA): string { export function encodeAndJoinPrimitives(values: readonly JsonPrimitive[], delimiter: string = COMMA): string {
return values.map(v => encodePrimitive(v, delimiter)).join(delimiter) return values.map(v => encodePrimitive(v, delimiter)).join(delimiter)
} }