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

Function intersectConstraints

ark/sets/constraint.ts:51–163  ·  view source on GitHub ↗
(
	s: ConstraintIntersectionState<kind>
)

Source from the content-addressed store, hash-verified

49 new Disjoint(...disjoint.map(entry => ({ ...entry, optional: true })))
50
51export const intersectConstraints = <kind extends ConstraintGroupKind>(
52 s: ConstraintIntersectionState<kind>
53): nodeOfKind<RootKind | Extract<kind, "structure">> | Disjoint => {
54 const head = s.r.shift()
55 if (!head) {
56 // a structure with no constraints still checks its undeclared keys
57 let result: BaseNode | Disjoint =
58 (
59 s.l.length === 0 &&
60 s.kind === "structure" &&
61 (!s.baseInner.undeclared || s.baseInner.undeclared === "ignore")
62 ) ?
63 $ark.kw.unknown.internal
64 // an intersection of a basis alone is the basis
65 : s.l.length === 0 && s.kind === "intersection" && basisOf(s.baseInner) ?
66 basisOf(s.baseInner)!
67 : s.ctx.$.node(
68 s.kind,
69 Object.assign(s.baseInner, unflattenConstraints(s.l)) as never,
70 { reduced: true }
71 )
72 // a root, e.g. an empty array, can only meet the object a structure constrains
73 if (s.roots.length && result.is("structure"))
74 result = s.ctx.$.node("intersection", { structure: result })
75
76 for (const root of s.roots) {
77 result = intersectOrPipeNodes(root, result, s.ctx)!
78 if (result instanceof Disjoint) return asOptionalDisjoint(result)
79 }
80
81 return result as never
82 }
83 let matched = false
84 const headKey = (head as BaseProp).key
85 for (let i = 0; i < s.l.length; i++) {
86 // props meet only at the same key
87 const key = (s.l[i] as BaseProp).key
88 if (key !== headKey && key !== undefined && headKey !== undefined) continue
89 const result: BaseNode | Disjoint | null = intersectOrPipeNodes(
90 s.l[i],
91 head,
92 s.ctx
93 )
94 if (result === null) continue
95 if (result instanceof Disjoint) {
96 return s.derivedR?.has(head) || s.derivedL?.has(s.l[i]) ?
97 asOptionalDisjoint(result)
98 : result
99 }
100
101 if (result.isRoot()) {
102 s.roots.push(result)
103 // sequences that met as tuples of several lengths are the root's now,
104 // but what's left of a structure rejecting undeclared keys still
105 // declares the indices they did
106 if (
107 s.kind === "structure" &&
108 s.baseInner.undeclared === "reject" &&

Callers 2

intersectIntersectionsFunction · 0.90
structure.tsFile · 0.90

Calls 10

appendUniqueFunction · 0.90
basisOfFunction · 0.85
unflattenConstraintsFunction · 0.85
asOptionalDisjointFunction · 0.85
shiftMethod · 0.80
nodeMethod · 0.80
isMethod · 0.80
hasMethod · 0.45
pushMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected