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

Function keyRegisteredNames

ark/schema/units.ts:492–529  ·  view source on GitHub ↗
(root: BaseRoot)

Source from the content-addressed store, hash-verified

490
491// registered names and minified function names vary by program
492const keyRegisteredNames = (root: BaseRoot): string => {
493 constantNames ??= new Map(
494 Object.entries(unitConstants).map(([name, value]) => [value, name])
495 )
496 const kindsByName: Record<string, string> = Object.create(null)
497 for (const id in root.byId) {
498 const node = root.byId[id]
499 for (const k in node.inner) {
500 // a unit value's function name would shift later registrations
501 forEachElement((node.inner as Dict)[k], k === "unit" ? 0 : 2, value => {
502 const constant = constantNames!.get(value)
503 if (constant) kindsByName[nameOf(value as object)] = `$.${constant}`
504 else if (typeof value === "function" && !isNode(value)) {
505 // only predicate and morph arity is compiled, and whether a build
506 // proved a predicate only returns what it reports
507 kindsByName[nameOf(value)] =
508 node.is("predicate") || node.is("morph") ?
509 `fn:${arityOf(value)}${returnsReports.has(value) ? "r" : ""}`
510 : "fn"
511 }
512 })
513 }
514 }
515 const keysByName: Record<string, string> = {}
516 let nameCount = 0
517 const keyOfName = (name: string) => {
518 const kind = kindsByName[name] ?? name.replace(/\$[1-9]\d*$/, "")
519 return `${kind}#${nameCount++}`
520 }
521 // a quote after a backslash is text in a string the hash holds, not a
522 // reference. Matched by the character before and not a lookbehind, since a
523 // lookbehind in a literal fails the whole module to parse before Safari 16.4
524 return root.hash.replace(
525 /(^|[^\\])"\$ark\.([\w$]+)"/g,
526 (_, before: string, name: string) =>
527 `${before}"${(keysByName[name] ??= keyOfName(name))}"`
528 )
529}
530
531const precompileReferences = (
532 references: readonly BaseNode[],

Callers 1

precompiledKeyOfFunction · 0.85

Calls 10

nameOfFunction · 0.90
isNodeFunction · 0.90
arityOfFunction · 0.90
forEachElementFunction · 0.85
keyOfNameFunction · 0.85
isMethod · 0.80
replaceMethod · 0.80
mapMethod · 0.65
getMethod · 0.65
hasMethod · 0.45

Tested by

no test coverage detected