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

Function writeUnionCombinator

ark/type/standalone/combinators.ts:1310–1444  ·  view source on GitHub ↗
(
	js: CombinatorCompiler,
	node: UnionNode
)

Source from the content-addressed store, hash-verified

1308
1309// a list of units, cases by domain, a discriminated union or a list of checkers
1310const writeUnionCombinator = (
1311 js: CombinatorCompiler,
1312 node: UnionNode
1313): string => {
1314 const ref = (branch: BaseRoot, k?: string) =>
1315 readCase(js, branch, k === undefined ? undefined : caseMember(node, k))
1316 const fails =
1317 node.tfs && (node.hasMorph || node.hasAlias) ?
1318 "runtime.failingBranches"
1319 : undefined
1320 const units = unitListOf(node)
1321 if (units) {
1322 if (
1323 units.site.code === "discriminant" &&
1324 units.site.description === "boolean" &&
1325 units.values.length === 2 &&
1326 units.values.every(value => typeof value === "boolean")
1327 )
1328 return "runtime.booleanChecker"
1329 return writeCall(
1330 "units",
1331 `[${units.values.map(value => writeLiteral(js, value)).join(", ")}]`,
1332 units.site.code === "discriminant" ? undefined : '"union"',
1333 unlessDerived(runtime.describeValues(units.values), units.site)
1334 )
1335 }
1336 const domains = domainCasesOf(node)
1337 if (domains) {
1338 const cases = Object.entries(domains).map(
1339 ([domain, branch]): [string, string] => [
1340 domain,
1341 // null and undefined are their domain's only value
1342 (
1343 branch === true ||
1344 (branch.is("unit") && (domain === "null" || domain === "undefined"))
1345 ) ?
1346 "1"
1347 : ref(branch, JSON.stringify(domain))
1348 ]
1349 )
1350 const { description } = discriminantSiteOf(node)!
1351 const [domain, checker] = cases.find(([k]) => k !== "null")!
1352 if (
1353 !fails &&
1354 cases.length === 2 &&
1355 cases.some(([k, branch]) => k === "null" && branch === "1") &&
1356 description === runtime.nullableDescriptionOf(domain)
1357 ) {
1358 if (domain === "object") return writeCall("nullableObject", checker)
1359 if (
1360 !Object.values(domains).some(branch => branch !== true && branch.tfs)
1361 ) {
1362 return writeCall(
1363 "nullable",
1364 checker,
1365 domain === "string" ? undefined : JSON.stringify(domain)
1366 )
1367 }

Callers 1

writeCheckerFunction · 0.85

Calls 15

listsBranchesFunction · 0.90
listedIssueOfFunction · 0.90
unitListOfFunction · 0.85
writeCallFunction · 0.85
writeLiteralFunction · 0.85
unlessDerivedFunction · 0.85
domainCasesOfFunction · 0.85
discriminantSiteOfFunction · 0.85
checkerDescriptionOfFunction · 0.85
writeObjectFunction · 0.85
discriminatedOfFunction · 0.85
writeKeyFunction · 0.85

Tested by

no test coverage detected