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

Function scanTerm

ark/type/deferred.ts:424–478  ·  view source on GitHub ↗
(def: string)

Source from the content-addressed store, hash-verified

422}
423
424const scanTerm = (def: string): Facts | undefined => {
425 if (stringLiteral.test(def)) {
426 return {
427 key: def,
428 domains: stringBits,
429 pure: true,
430 units: true,
431 values: [def.slice(1, -1)]
432 }
433 }
434 // at runtime a brand is what it's of
435 const brand = branded.exec(def)
436 if (brand) return keywordDomains[brand[1]] && scanTerm(brand[1])
437 // one that's what it writes, which a number too long to be isn't
438 if (numberLiteral.test(def) && `${+def}` === def) {
439 return {
440 key: def,
441 domains: numberBits,
442 pure: true,
443 units: true,
444 values: [+def]
445 }
446 }
447 if (def[0] === "(" && def[def.length - 1] === ")")
448 return scanUnion(def.slice(1, -1))
449 if (def.endsWith("[]")) {
450 const element = scanTerm(def.slice(0, -2))
451 return element?.pure ? arrayFacts(element) : undefined
452 }
453 const bounded = scanRange(def)
454 if (bounded) return bounded
455 const domain = keywordDomains[def]
456 if (!domain) return
457 const base =
458 def === "string" ? "string"
459 : def === "number" ? "number"
460 : def === "number.integer" ? "integer"
461 : undefined
462 const values =
463 def === "true" ? [true]
464 : def === "false" ? [false]
465 : def === "null" ? [null]
466 : def === "undefined" ? [undefined]
467 : def === "boolean" ? [true, false]
468 : undefined
469 return {
470 key: def,
471 domains: unitBitsOf(domain),
472 pure: true,
473 base,
474 of: base && def,
475 values,
476 units: !!values
477 }
478}
479
480/** adds a term in order, since a union's say nothing, or false if it's a repeat, whose
481 * union could reduce to what isn't scanned */

Callers 3

scanRangeFunction · 0.85
scanUnionFunction · 0.85
scanAndFunction · 0.85

Calls 6

scanUnionFunction · 0.85
arrayFactsFunction · 0.85
scanRangeFunction · 0.85
unitBitsOfFunction · 0.85
testMethod · 0.80
execMethod · 0.80

Tested by

no test coverage detected