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

Class UnitNode

ark/schema/roots/unit.ts:86–139  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

84 })
85
86export class UnitNode extends InternalBasis<Unit.Declaration> {
87 /** its unit as JSON: a Date by its ISO string, an object or symbol by its registered reference */
88 unitJson: JsonPrimitive = serializeUnit(this.unit)
89 /** its unit written as a JavaScript literal, as compiled code compares data to it */
90 jsUnit: string =
91 dateTimeOf(this.unit) !== undefined ?
92 JSON.stringify(this.unitJson)
93 : compileSerializedValue(this.unit)
94
95 override sameInner(r: UnitNode): boolean {
96 return this.unitJson === r.unitJson
97 }
98
99 jsCond(js: NodeCompiler, data: string): string {
100 const time = dateTimeOf(this.unit)
101 return (
102 time !== undefined ?
103 Number.isNaN(time) ?
104 `${data} instanceof Date && Number.isNaN(${js.ref(timeOf)}(${data}))`
105 : `${data} instanceof Date && ${js.ref(timeOf)}(${data}) === ${time}`
106 : Number.isNaN(this.unit) ? `Number.isNaN(${data})`
107 : hasDomain(this.unit, "object") || typeof this.unit === "symbol" ?
108 `${data} === ${js.ref(this.unit)}`
109 : `${data} === ${this.jsUnit}`
110 )
111 }
112
113 expression: string =
114 dateTimeOf(this.unit) !== undefined ?
115 this.unitJson === "Invalid Date" ?
116 "Invalid Date"
117 : `d'${this.unitJson}'`
118 : typeof this.unit === "number" ? printDecimal(this.unit)
119 : printUnit(this.unit)
120 domain: Domain = domainOf(this.unit)
121
122 get brief(): string {
123 return this.domain === "object" ?
124 domainDescriptions.object
125 : this.description
126 }
127
128 tAllows: TraverseAllows = (() => {
129 const time = dateTimeOf(this.unit)
130 return (
131 time !== undefined ?
132 Number.isNaN(time) ?
133 data => data instanceof Date && Number.isNaN(timeOf(data))
134 : data => data instanceof Date && timeOf(data) === time
135 : Number.isNaN(this.unit) ? data => Number.isNaN(data)
136 : data => data === this.unit
137 )
138 })()
139}
140
141export const Unit = {
142 impl: implementation,

Callers

nothing calls this directly

Calls 7

compileSerializedValueFunction · 0.90
printDecimalFunction · 0.90
domainOfFunction · 0.90
timeOfFunction · 0.90
serializeUnitFunction · 0.85
dateTimeOfFunction · 0.85
printUnitFunction · 0.70

Tested by

no test coverage detected