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

Function parseDefault

ark/type/parser/shift/operator/default.ts:80–109  ·  view source on GitHub ↗
(
	s: RootedRuntimeState
)

Source from the content-addressed store, hash-verified

78export type ParsedDefaultableProperty = readonly [BaseRoot, "=", unknown]
79
80export const parseDefault = (
81 s: RootedRuntimeState
82): ParsedDefaultableProperty => {
83 // store the node that will be bounded
84 const baseNode = s.unsetRoot()
85 // collection literals are not unit literals, so they must be short-circuited
86 // here and represented as thunks to keep each traversal's default distinct
87 s.sc.skipSpace()
88 if (s.sc.unscanned.startsWith("{}")) {
89 s.sc.jumpForward(2)
90 return [baseNode, "=", emptyCollectionDefaults["{}"]]
91 }
92 if (s.sc.peek === "[") return [baseNode, "=", parseDefaultArray(s)]
93 const start = s.sc.location
94 s.parseOperand()
95 const defaultNode = s.unsetRoot()
96 // ( and keyof are operands that don't set a root
97 if (!defaultNode?.is("unit")) {
98 return s.error(
99 writeNonLiteralDefaultMessage(
100 defaultNode?.expression ?? s.sc.sliceChars(start).trim()
101 )
102 )
103 }
104 const defaultValue =
105 defaultNode.unit instanceof Date ?
106 () => new Date(defaultNode.unit as Date)
107 : defaultNode.unit
108 return [baseNode, "=", defaultValue]
109}
110
111const parseDefaultArray = (s: RootedRuntimeState): (() => unknown[]) => {
112 const arrayStart = s.sc.location

Callers 1

fullStringParseFunction · 0.90

Calls 9

parseDefaultArrayFunction · 0.85
unsetRootMethod · 0.80
skipSpaceMethod · 0.80
jumpForwardMethod · 0.80
parseOperandMethod · 0.80
isMethod · 0.80
sliceCharsMethod · 0.80
errorMethod · 0.45

Tested by

no test coverage detected