MCPcopy Create free account
hub / github.com/arktypeio/arktype / encoderOf

Function encoderOf

ark/schema/shared/codecs.ts:57–86  ·  view source on GitHub ↗
(node: BaseRoot)

Source from the content-addressed store, hash-verified

55
56// the decoding graph with each morph inverted
57const encoderOf = (node: BaseRoot): BaseRoot => {
58 // whether each branch of a union or head of a morph transforms, read from
59 // the original before its children transform, as a branch reaching an alias
60 // that's still being encoded can't resolve yet
61 const branchTfs: boolean[][] = []
62 const headTfs: boolean[] = []
63 const mapper: DeepNodeTransformation = (kind, inner, ctx) =>
64 kind === "morph" ?
65 invert(inner as never, headTfs.pop()!, node.$, mapper, ctx)
66 : kind === "optional" && "default" in inner ?
67 node.$.node("required", {
68 ...omit(inner as Optional.Inner, { default: 1 }),
69 value: decodedValueOf((inner as Optional.Inner).value, node.$)
70 })
71 : kind === "structure" ? requireDefaulted(inner as never)
72 : kind === "sequence" ? requireDefaultables(inner as never, node.$)
73 : kind === "union" ?
74 encodeUnion(inner as Union.Inner, branchTfs.pop()!, node.$)
75 : (inner as never)
76 return node.transform(mapper, {
77 shouldTf: n => {
78 const tfs = n.tfs || (n.is("optional") && n.hasDef())
79 if (tfs && n.is("union"))
80 branchTfs.push(n.branches.map(transformsOnEncode))
81 if (tfs && n.is("morph"))
82 headTfs.push(n.head ? transformsOnEncode(n.head) : false)
83 return tfs
84 }
85 }) as never
86}
87
88// whether a branch reaches a transform that survives encoding (morphs and
89// undeclared: "delete"), excluding defaults stripped to required/prefix

Callers 1

encodeFunction · 0.85

Calls 4

transformsOnEncodeFunction · 0.85
isMethod · 0.80
mapMethod · 0.65
pushMethod · 0.45

Tested by

no test coverage detected