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

Function narrowToKeysOf

ark/sets/structure/structure.ts:149–221  ·  view source on GitHub ↗
(
	node: Structure.Node,
	other: Structure.Node,
	otherInner: {
		optional?: readonly OptionalNode[]
		index?: readonly nodeOfKind<"index">[]
		sequence?: nodeOfKind<"sequence">
	},
	derived: OptionalNode[],
	roots: BaseRoot[],
	$: BaseScope
)

Source from the content-addressed store, hash-verified

147// structure with undeclared keys allows, with node on the left of a disjoint
148// for each required key or element it doesn't
149const narrowToKeysOf = (
150 node: Structure.Node,
151 other: Structure.Node,
152 otherInner: {
153 optional?: readonly OptionalNode[]
154 index?: readonly nodeOfKind<"index">[]
155 sequence?: nodeOfKind<"sequence">
156 },
157 derived: OptionalNode[],
158 roots: BaseRoot[],
159 $: BaseScope
160): Disjoint => {
161 const disjoint = new Disjoint()
162 const keys = allowedKeysOf(node)
163 const count =
164 node.undeclared === "reject" && other.sequence ?
165 allowedIndexCountOf(keys, other.sequence)
166 : null
167 if (count !== null) {
168 const sequence = other.sequence!
169 if (sequence.minLength > count) {
170 const value = requiredElementAt(sequence, count)
171 disjoint.push(
172 ...Disjoint.init(
173 "presence",
174 $ark.kw.never.internal,
175 value
176 ).withPrefixKey(count, "required")
177 )
178 }
179 // as a length bound, 0 would be an exact length, which a union could
180 // mistake for a discriminant of node, which needn't be an array
181 else if (count === 0) {
182 otherInner.sequence = $.node("sequence", {
183 variadic: $ark.kw.never.internal
184 })
185 } else {
186 roots.push(
187 $.node("intersection", {
188 proto: $.node("proto", { proto: Array }),
189 // at least 1, so it doesn't reduce to an exact length
190 maxLength: $.node("maxLength", { rule: count }) as never
191 })
192 )
193 }
194 }
195 for (const k of other.requiredKeys) {
196 if (keys.allowsUnused(k)) continue
197 disjoint.add("presence", $ark.kw.never.internal, other.byKey[k]!.value, {
198 path: [k],
199 undeclared: node.handlesUndeclared
200 })
201 }
202 if (otherInner.optional) {
203 otherInner.optional = otherInner.optional.filter(n =>
204 keys.allowsUnused(n.key)
205 )
206 }

Callers 1

structure.tsFile · 0.85

Calls 12

addMethod · 0.95
intersectNodesRootFunction · 0.90
normalizeIndexFunction · 0.90
allowedKeysOfFunction · 0.85
allowedIndexCountOfFunction · 0.85
requiredElementAtFunction · 0.85
withPrefixKeyMethod · 0.80
initMethod · 0.80
nodeMethod · 0.80
filterMethod · 0.80
extendsMethod · 0.80
pushMethod · 0.45

Tested by

no test coverage detected