3
0
mirror of https://github.com/larvit/adf-codec.git synced 2026-11-19 06:35:44 +01:00

Answer principles round 1: 1 🔴, 5 🟡 (0 misread); the document carry's line feeds checked, element spellings shared with the writer, carried JSON bounded by type, an edit to a carry refused, one test per case, a start tag read one attribute at a time

This commit is contained in:
2026-10-28 01:27:40 +01:00
parent d2b8f2c95f
commit d12d031784
14 changed files with 430 additions and 302 deletions
+5 -1
View File
@@ -20,6 +20,10 @@ export function isNumberList(value: unknown): value is NumberList {
return Array.isArray(value) && value.length > 0 && value.every((item) => typeof item === 'number')
}
export function holdsKind(value: JsonValue, kind: AttributeKind): boolean {
return vocabularyPair('', value, kind) !== undefined
}
export function vocabularyPairs(attrs: AdfAttributes, vocabulary: AttributeVocabulary, spelledElsewhere: readonly string[]): VocabularyPair[] | undefined {
const pairs: VocabularyPair[] = []
for (const [key, value] of Object.entries(attrs)) {
@@ -33,7 +37,7 @@ export function vocabularyPairs(attrs: AdfAttributes, vocabulary: AttributeVocab
return pairs
}
export function vocabularyPair(key: string, value: JsonValue, kind: AttributeKind): VocabularyPair | undefined {
function vocabularyPair(key: string, value: JsonValue, kind: AttributeKind): VocabularyPair | undefined {
if (kind === 'boolean') return typeof value === 'boolean' ? { key, kind, value } : undefined
if (kind === 'number') return typeof value === 'number' ? { key, kind, value } : undefined
if (kind === 'numbers') return isNumberList(value) ? { key, kind, value } : undefined
+7 -4
View File
@@ -8,6 +8,9 @@ import { isJsonValue, nestingDepth, overNested } from '../json-value.ts'
import { joinedWithin } from '../length-bound.ts'
import { parseJsonText } from '../json-text.ts'
// JSON `carriedNesting` found within the levels its position leaves, the one value `carriedNode` reads.
export type BoundedJson = { readonly bounded: JsonValue }
// `spelled` is the node as the carry holds it, `type` dropped where the format names it apart; `levels` the nesting its position leaves.
export function carriedJson(node: AdfNode, spelled: object, spelling: JsonSpelling, path: ConvertErrorPath, levels: number): Result<{ headroom: number; json: string }> {
const headroom = levels - nestingDepth(node)
@@ -24,12 +27,12 @@ export function carriedValue(json: string, holder: string, invalid: ConvertFault
return { fault: parsed.refusal === 'syntax' ? invalid : { code: 'unsupported-node-shape', message: `${holder} holds a number JSON cannot spell` } }
}
export function carriedNesting(held: JsonValue, levels: number): ConvertFault | undefined {
return overNested(held, levels) ? nestedPast(levels) : undefined
export function carriedNesting(held: JsonValue, levels: number): Read<BoundedJson> {
return overNested(held, levels) ? { fault: nestedPast(levels) } : { value: { bounded: held } }
}
export function carriedNode(held: JsonValue, holder: string): Read<AdfNode> {
return isAdfNode(held) ? { value: held } : { fault: { code: 'unsupported-node-shape', message: `${holder} holds one ADF node's JSON: this JSON is no ADF node` } }
export function carriedNode({ bounded }: BoundedJson, holder: string): Read<AdfNode> {
return isAdfNode(bounded) ? { value: bounded } : { fault: { code: 'unsupported-node-shape', message: `${holder} holds one ADF node's JSON: this JSON is no ADF node` } }
}
function nestedPast(levels: number): ConvertFault {