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

Function encodeKey

ark/type/standalone/keys.ts:158–290  ·  view source on GitHub ↗
(
	start: unknown,
	defaultsByMorph?: ReadonlyMap<unknown, CollectedDefault>,
	mode: KeyMode = defaultsByMorph ? "value" : "definition"
)

Source from the content-addressed store, hash-verified

156
157// equal keys differ only in which functions they hold
158export const encodeKey = (
159 start: unknown,
160 defaultsByMorph?: ReadonlyMap<unknown, CollectedDefault>,
161 mode: KeyMode = defaultsByMorph ? "value" : "definition"
162): Key => {
163 const values: string[] = []
164 const holes: unknown[] = []
165 const seen = new Map<unknown, number>()
166 const thunks: [...ScopeAlias, path: string][] = []
167 let refusal: Key[2]
168 const refuse = (reason: string) => void (refusal ??= ["", reason])
169 const holeIndexOf = (hole: unknown) => {
170 const index = holes.indexOf(hole)
171 return index === -1 ? holes.push(hole) - 1 : index
172 }
173 const writeKeyOf = (kind: string, value: Foldable, path: string) => {
174 const [id, held, nested, nestedThunks] = keyOf(value)
175 values.push(`${kind} ${id} ${held.map(holeIndexOf)}`)
176 if (nested) refusal ??= [appendPath(path, nested[0]), nested[1]]
177 for (const [scope, alias, at] of nestedThunks)
178 thunks.push([scope, alias, appendPath(path, at)])
179 }
180 const at = (path: string) => (path ? ` at \`${path}\`` : "")
181 const write = (value: unknown, path: string, mode: KeyMode): void => {
182 if (hasDomain(value, "object")) {
183 const index = seen.get(value)
184 if (index !== undefined) return void values.push(`seen ${index}`)
185 seen.set(value, seen.size)
186 }
187 const defaulted = defaultsByMorph?.get(value)
188 const runtimeKey = hasDomain(value, "object") && runtimeKeyOf(value)
189 if (defaulted) {
190 values.push("default")
191 write(defaulted[2], path, "value")
192 } else if (runtimeKey) values.push(`runtime ${runtimeKey}`)
193 else if (isNode(value)) {
194 if (defaultsByMorph || !value.isRoot()) values.push("node")
195 else writeKeyOf("Type", value, path)
196 } else if (hasArkKind(value, "module")) {
197 const entries = Object.entries(value)
198 values.push(`Module ${entries.length}`)
199 for (const [k, entry] of entries) {
200 values.push(JSON.stringify(k))
201 write(entry, appendStringifiedKey(path, k), "definition")
202 }
203 } else if (hasArkKind(value, "scope")) {
204 // a builtin scope holds only arktype's own functions, named stably
205 if (value.isBuiltin) values.push(`Scope ${value.name}`)
206 // e.g. a Type's, which each Type of the Scope shares
207 else if (value !== start) writeKeyOf("Scope", value, path)
208 else {
209 const aliases = Object.entries(value.aliases)
210 values.push(
211 `Scope ${JSON.stringify(value.exportedNames)} ${aliases.length}`
212 )
213 write(value.scopeConfig, "", "config")
214 // the definition, as resolving it would run default checks early
215 for (const [k, def] of aliases) {

Callers 3

writeDefaultChecksMethod · 0.90
keyOfFunction · 0.85
rootKeyOfFunction · 0.85

Calls 2

hashOfFunction · 0.90
writeFunction · 0.70

Tested by

no test coverage detected