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

Method tfSharedKey

ark/schema/structure/structure.ts:834–889  ·  view source on GitHub ↗

transforms a key's value through each constraint transforming it: its prop or the index signatures it matches

(
		data: object,
		k: Key,
		ctx: InternalTraversal,
		inPlace: boolean | undefined,
		pathKey: PropertyKey = k
	)

Source from the content-addressed store, hash-verified

832
833 /** transforms a key's value through each constraint transforming it: its prop or the index signatures it matches */
834 private tfSharedKey(
835 data: object,
836 k: Key,
837 ctx: InternalTraversal,
838 inPlace: boolean | undefined,
839 pathKey: PropertyKey = k
840 ): void {
841 const prop = this.byKey[k]
842 if (prop && !this.tfsOf(prop).length) return
843 const constraints: BaseNode[] = prop ? [prop.value] : []
844 // a declared key data inherits or hides is no key of an index signature
845 const listed = !prop || isDeletableKeyOf(data, k)
846 if (listed) {
847 for (const index of this.index!) {
848 if (allowsInBranch(ctx, index.signature, k))
849 constraints.push(index.value)
850 }
851 }
852 const steps: TransformStep[] =
853 prop ?
854 this.tfsOf(prop).filter(step => listed || !step.indexed)
855 : constraints.filter(n => n.tfs).map(node => ({ node }))
856 const input: unknown = data[k as never]
857 if (
858 !steps.length ||
859 !travKey(
860 pathKey,
861 () => constraints.every(n => allowsInBranch(ctx, n, input)),
862 ctx
863 )
864 )
865 return
866 const errN = ctx.errN
867 let value = input
868 for (const step of steps) {
869 if (ctx.errN > errN) break
870 if (step.signature && !allowsInBranch(ctx, step.signature, k)) continue
871 const stepInput =
872 value === input || step.node.allows(value) ? value : input
873 const transformed = travKey(
874 pathKey,
875 () =>
876 transform(
877 ctx,
878 inPlace ? writingInPlace(step.node) : step.node,
879 stepInput
880 ),
881 ctx
882 )
883 value =
884 stepInput === value ? transformed : (
885 mergeTransformed(stepInput, value, transformed)
886 )
887 }
888 transformedInApply(ctx, value)
889 }
890
891 /** applies its defaults and structural morphs to data, writing into `out` */

Callers 1

applySharedKeysMethod · 0.95

Calls 12

tfsOfMethod · 0.95
isDeletableKeyOfFunction · 0.90
allowsInBranchFunction · 0.90
travKeyFunction · 0.90
transformFunction · 0.90
mergeTransformedFunction · 0.90
transformedInApplyFunction · 0.90
writingInPlaceFunction · 0.85
filterMethod · 0.80
allowsMethod · 0.80
mapMethod · 0.65
pushMethod · 0.45

Tested by

no test coverage detected