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

Function invert

ark/schema/shared/codecs.ts:160–232  ·  view source on GitHub ↗
(
	inner: Morph.Inner & { meta?: TypeMeta },
	headTransforms: boolean,
	$: BaseScope,
	mapper: DeepNodeTransformation,
	ctx: DeepNodeTransformContext
)

Source from the content-addressed store, hash-verified

158 .map(([branch]) => branch)
159
160const invert = (
161 inner: Morph.Inner & { meta?: TypeMeta },
162 headTransforms: boolean,
163 $: BaseScope,
164 mapper: DeepNodeTransformation,
165 ctx: DeepNodeTransformContext
166): BaseRoot => {
167 let encoder = inner.declaredOut ?? $ark.kw.unknown.internal
168 let encoderTfs = false
169 // an inverse inverts the one function among its node's morphs
170 let inverse = inner.inverse
171 // a node first is the input, which the encoder ends by piping to
172 const head = hasArkKind(inner.morphs[0], "root") ? inner.morphs[0] : undefined
173 for (let i = inner.morphs.length - 1; i >= (head ? 1 : 0); i--) {
174 const morph = inner.morphs[i]
175 if (hasArkKind(morph, "root")) {
176 const morphTfs = transformsOnEncode(morph)
177 // encoded by the same transform, so an alias it reaches that's
178 // already being encoded, like one cycling back through this pipe,
179 // resolves to that encoder rather than encoding it again
180 encoder = pipeEncoder(
181 encoder,
182 morph.transformWithin(mapper, ctx) ?? $ark.kw.unknown.internal,
183 encoderTfs,
184 morphTfs,
185 $
186 )
187 encoderTfs ||= morphTfs
188 continue
189 }
190 const inverseIn = inverseIns.get(morph)?.()
191 if (inverseIn) {
192 encoder = pipeEncoder(encoder, inverseIn, encoderTfs, false, $)
193 encoderTfs ||= encoder.is("morph")
194 }
195 encoder = $.node("morph", {
196 morphs: [
197 encoder,
198 inverse ??
199 inverses.get(morph) ??
200 throwParseError(
201 writeMissingInverseMessage($.node("morph", inner).expression)
202 )
203 ],
204 inverse: morph
205 })
206 encoderTfs = true
207 inverse = undefined
208 }
209 const decodedIn = inner.declaredIn ?? head
210 const result =
211 decodedIn ?
212 pipeEncoder(
213 encoder,
214 decodedIn,
215 encoderTfs,
216 inner.declaredIn ? false : headTransforms,
217 $

Callers 1

mapperFunction · 0.70

Calls 8

hasArkKindFunction · 0.90
throwParseErrorFunction · 0.90
transformsOnEncodeFunction · 0.85
pipeEncoderFunction · 0.85
isMethod · 0.80
nodeMethod · 0.80
getMethod · 0.65

Tested by

no test coverage detected