MCPcopy
hub / github.com/arktypeio/arktype / _transform

Function _transform

ark/schema/node.ts:516–610  ·  view source on GitHub ↗
(
		mapper: DeepNodeTransformation,
		ctx: DeepNodeTransformContext
	)

Source from the content-addressed store, hash-verified

514 }
515
516 protected _transform(
517 mapper: DeepNodeTransformation,
518 ctx: DeepNodeTransformContext
519 ): BaseNode | null {
520 const $ = ctx.bindScope ?? this.$
521 if (ctx.seen[this.id])
522 // Cyclic handling needs to be made more robust
523 // https://github.com/arktypeio/arktype/issues/944
524 return this.$.lazilyResolve(ctx.seen[this.id]! as never)
525 if (ctx.shouldTransform?.(this as never, ctx) === false) return this
526
527 let transformedNode: BaseRoot | undefined
528
529 ctx.seen[this.id] = () => transformedNode
530
531 if (
532 this.hasKind("structure") &&
533 this.undeclared !== ctx.undeclaredKeyHandling
534 ) {
535 ctx = {
536 ...ctx,
537 undeclaredKeyHandling: this.undeclared
538 }
539 }
540
541 const innerWithTransformedChildren = flatMorph(
542 this.inner as Dict,
543 (k, v) => {
544 if (!this.impl.keys[k].child) return [k, v]
545 const children = v as listable<BaseNode>
546 if (!isArray(children)) {
547 const transformed = children._transform(mapper, ctx)
548 return transformed ? [k, transformed] : []
549 }
550 // if the value was previously explicitly set to an empty list,
551 // (e.g. branches for `never`), ensure it is not pruned
552 if (children.length === 0) return [k, v]
553 const transformed = children.flatMap(n => {
554 const transformedChild = n._transform(mapper, ctx)
555 return transformedChild ?? []
556 })
557 return transformed.length ? [k, transformed] : []
558 }
559 )
560
561 delete ctx.seen[this.id]
562
563 const innerWithMeta = Object.assign(innerWithTransformedChildren, {
564 meta: this.meta
565 })
566
567 const transformedInner =
568 ctx.selected && !ctx.selected.includes(this) ?
569 innerWithMeta
570 : mapper(this.kind, innerWithMeta, ctx)
571
572 if (transformedInner === null) return null
573

Callers

nothing calls this directly

Calls 5

flatMorphFunction · 0.90
isNodeFunction · 0.90
isEmptyObjectFunction · 0.90
nodeMethod · 0.80
_transformMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…