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

Function isSimulated

ark/schema/node.ts:1687–1755  ·  view source on GitHub ↗
(l: BaseNode, r: BaseNode, s: SimulationState)

Source from the content-addressed store, hash-verified

1685
1686// a pair of aliases is assumed related until their unfoldings are compared
1687const isSimulated = (l: BaseNode, r: BaseNode, s: SimulationState): boolean => {
1688 if (innerHashesMatch(l, r)) return true
1689 if (l.is("alias") || r.is("alias")) {
1690 if (!s.unfoldsAliases) return true
1691 if (s.subsumes) {
1692 if (l.is("alias") && l.operandsBesides(r)) return true
1693 const others = r.is("alias") && r.operandsBesides(l)
1694 if (others) return others.every(other => isAssumed(l, other, s, "within"))
1695 }
1696 return isAssumed(l, r, s, "simulated")
1697 }
1698 // branches of an unreduced union may subsume each other
1699 if (l.is("union") && !l.inner.ordered) {
1700 return l.branches.every(
1701 branch =>
1702 isSimulated(branch, r, s) ||
1703 (s.subsumes &&
1704 l.branches.some(
1705 other =>
1706 other !== branch &&
1707 isSimulated(other, r, s) &&
1708 isSubsumed(branch, other, s)
1709 ))
1710 )
1711 }
1712 if (r.is("union") && !r.inner.ordered)
1713 return r.branches.some(branch => isSimulated(l, branch, s))
1714 if (l.is("sequence") && r.is("sequence") && !l.defLen && !r.defLen)
1715 return isSequenceSimulated(l, r, s)
1716 const lEntries = simulatedEntriesOf(l)
1717 if (l.kind !== r.kind || lEntries.length !== simulatedEntriesOf(r).length)
1718 return false
1719 for (const [k, v] of lEntries) {
1720 if (!(k in r.inner)) return false
1721 if (k === "morphs") {
1722 const lMorphs = v as Morph.Inner["morphs"]
1723 const rMorphs = (r as Morph.Node).inner.morphs
1724 if (
1725 lMorphs.length !== rMorphs.length ||
1726 !lMorphs.every((morph, i) =>
1727 isNode(morph) && isNode(rMorphs[i]) ?
1728 isSimulated(morph, rMorphs[i] as BaseNode, s)
1729 : morph === rMorphs[i]
1730 )
1731 )
1732 return false
1733 continue
1734 }
1735 if (l.impl.keys[k].child !== true) {
1736 if (!isJsonEqual((l.innerJson as Dict)[k], (r.innerJson as Dict)[k]))
1737 return false
1738 continue
1739 }
1740 // a broader index signature constrains more keys, so signatures must match exactly
1741 if (k === "signature") {
1742 if (!innerHashesMatch(v as BaseNode, (r.inner as Dict)[k] as BaseNode))
1743 return false
1744 continue

Callers 5

isSequenceSimulatedFunction · 0.85
isSubsumedFunction · 0.85
isWithinFunction · 0.85
simulatesFunction · 0.85
_isMutuallySimulatedFunction · 0.85

Calls 10

isNodeFunction · 0.90
liftArrayFunction · 0.90
innerHashesMatchFunction · 0.85
isAssumedFunction · 0.85
isSubsumedFunction · 0.85
isSequenceSimulatedFunction · 0.85
simulatedEntriesOfFunction · 0.85
isJsonEqualFunction · 0.85
isMethod · 0.80
operandsBesidesMethod · 0.80

Tested by

no test coverage detected