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

Function intersectProps

ark/sets/structure/prop.ts:17–63  ·  view source on GitHub ↗
(
	l: nodeOfKind<Prop.Kind>,
	r: nodeOfKind<Prop.Kind>,
	ctx: IntersectionContext
)

Source from the content-addressed store, hash-verified

15import { domainsOf, unitBitsOf } from "../roots/union.ts"
16
17export const intersectProps = (
18 l: nodeOfKind<Prop.Kind>,
19 r: nodeOfKind<Prop.Kind>,
20 ctx: IntersectionContext
21): nodeOfKind<Prop.Kind> | Disjoint | null => {
22 if (l.key !== r.key) return null
23
24 const key = l.key
25 let value = intersectOrPipeNodes(l.value, r.value, ctx)
26 const kind: Prop.Kind = l.required || r.required ? "required" : "optional"
27 if (value instanceof Disjoint) {
28 if (kind === "optional") value = $ark.kw.never.internal
29 else {
30 // if either operand was optional, the Disjoint has to be treated as optional
31 return value.withPrefixKey(
32 l.key,
33 l.required && r.required ? "required" : "optional"
34 )
35 }
36 }
37
38 if (kind === "required") {
39 return ctx.$.node("required", {
40 key,
41 value
42 })
43 }
44
45 const defaultIntersection =
46 l.hasDef() ?
47 r.hasDef() ?
48 l.default === r.default ?
49 l.default
50 : throwParseError(writeDefaultIntersectionMessage(l.default, r.default))
51 : l.default
52 : r.hasDef() ? r.default
53 : unset
54
55 // value allows undefined only if both did, whatever ctx.$ configures
56 return ctx.$.node(
57 "optional",
58 defaultIntersection === unset ?
59 { key, value }
60 : { key, value, default: defaultIntersection },
61 { reduced: true }
62 )
63}
64
65export const required: setImplementationOf<Required.Declaration> = {
66 intersections: {

Callers

nothing calls this directly

Calls 4

throwParseErrorFunction · 0.90
withPrefixKeyMethod · 0.80
nodeMethod · 0.80

Tested by

no test coverage detected