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

Method applyStructMorph

ark/schema/structure/structure.ts:892–963  ·  view source on GitHub ↗

applies its defaults and structural morphs to data, writing into `out`

(
		data: object,
		out: object,
		ctx: InternalTraversal | undefined
	)

Source from the content-addressed store, hash-verified

890
891 /** applies its defaults and structural morphs to data, writing into `out` */
892 applyStructMorph(
893 data: object,
894 out: object,
895 ctx: InternalTraversal | undefined
896 ): object | TransformErrors {
897 const sliced = out !== data && this.slicesSeq
898 let failed: TransformErrors | undefined
899 for (let i = 0; i < this.defaultable.length; i++) {
900 if (!this.defaultable[i].holdsValue(data)) {
901 if (out === data && !this.writesInto(ctx)) out = this.copy(data)
902 const defaulted = this.defaultable[i].defMorph(
903 out as never,
904 ctx as never
905 )
906 if (defaulted instanceof TransformErrors)
907 failed = defaulted.addTo(failed)
908 }
909 }
910 const sequence = this.sequence
911 if (sequence?.defaultables) {
912 for (let i = 0; i < sequence.defLen; i++) {
913 if (sequence.holdsElement(data as array, sequence.preLen + i)) continue
914 if (out === data && !this.writesInto(ctx)) out = this.copy(data)
915 const defaulted = sequence.defMorphs[i](out as never, ctx as never)
916 if (defaulted instanceof TransformErrors)
917 failed = defaulted.addTo(failed)
918 }
919 }
920 if (failed) {
921 // a transform without ctx returns its errors
922 if (!ctx) return failed
923 addMorphErrs(ctx, failed)
924 }
925 // an object's copy drops a declared key data holds as a non-enumerable own prop
926 const keepDeclared = () => {
927 if (sequence) return
928 for (const prop of this.inOrder) {
929 if (!hasOwn(out, prop.key) && hasOwn(data, prop.key))
930 setOwnValue(out, prop.key, data[prop.key as never])
931 }
932 }
933 if (out !== data) keepDeclared()
934 if (this.undeclared !== "delete" || sliced) return out
935 // a result is built of own values, so data's own "__proto__" is copied
936 // only where it's declared, as compiled
937 if (
938 Object.getPrototypeOf(out) !== Object.prototype ||
939 (hasOwn(out, "__proto__") && this.declaresKey("__proto__"))
940 ) {
941 const undeclaredKeys = ownKeysOf(data).filter(k => !this.declaresKey(k))
942 if (!undeclaredKeys.length) return out
943 const copiedOnDelete = out === data
944 if (copiedOnDelete) out = this.copy(data)
945 for (const k of undeclaredKeys) delete out[k as never]
946 if (copiedOnDelete) keepDeclared()
947 return out
948 }
949 const result: Record<Key, unknown> = {}

Callers 2

tTransformMethod · 0.95
$arkStructuralMorphFunction · 0.80

Calls 12

writesIntoMethod · 0.95
copyMethod · 0.95
tfsOfMethod · 0.95
addMorphErrsFunction · 0.90
hasOwnFunction · 0.90
setOwnValueFunction · 0.90
ownKeysOfFunction · 0.85
holdsValueMethod · 0.80
holdsElementMethod · 0.80
filterMethod · 0.80
keepDeclaredFunction · 0.50
addToMethod · 0.45

Tested by

no test coverage detected