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

Function createNode

ark/schema/parse.ts:296–365  ·  view source on GitHub ↗
({
	id,
	alias,
	kind,
	inner,
	meta,
	$,
	ignoreCache,
	shell,
	unchecked
}: CreateNodeInput)

Source from the content-addressed store, hash-verified

294export let lastCreated: string | undefined
295
296export const createNode = ({
297 id,
298 alias,
299 kind,
300 inner,
301 meta,
302 $,
303 ignoreCache,
304 shell,
305 unchecked
306}: CreateNodeInput): BaseNode => {
307 // serialized as the node is created, so meta that can't be throws here
308 const metaJson = isEmptyObject(meta) ? undefined : metaJsonOf(meta)
309 const impl = nodeImplementationsByKind[kind]
310
311 // nodes are only cached with a set engine, which reduces them first
312 const key =
313 ignoreCache || hasSymbolMeta(meta) || !$ark.sets ?
314 undefined
315 : nodeKeyOf(kind, impl, inner, metaJson, $)
316
317 if (key) {
318 const cached = $.nodesByKey.get(key)
319 // one whose check was thrown, or discarded with its definition, before it
320 // passed is created again, which makes the check again
321 if (cached && !cached.c.unchecked) {
322 $ark.reached?.node(cached)
323 return cached
324 }
325 }
326
327 let children: BaseNode[] = noChildren
328 for (const k in inner) {
329 const keyImpl = impl.keys[k]
330 if (!keyImpl.child) continue
331 const value = inner[k] as listable<BaseNode>
332 const held =
333 keyImpl.child === true ?
334 isArray(value) ? value
335 : [value]
336 : keyImpl.child(value as never)
337 // held by a union or a list alone, it's that list
338 children =
339 children === noChildren ? (held as BaseNode[]) : children.concat(held)
340 }
341
342 const attachments: UnknownAttachmentsInput = {
343 id: id ?? registerNodeId(alias ?? kind),
344 kind,
345 impl,
346 inner,
347 meta,
348 metaJson,
349 children,
350 shell
351 }
352
353 const node = composing(() =>

Callers 4

settleFunction · 0.90
parseNodeFunction · 0.85
withIdFunction · 0.85
withMetaFunction · 0.85

Calls 11

isEmptyObjectFunction · 0.90
metaJsonOfFunction · 0.90
composingFunction · 0.90
markUncheckedFunction · 0.90
hasSymbolMetaFunction · 0.85
nodeKeyOfFunction · 0.85
registerNodeIdFunction · 0.85
resolveDescriptionFunction · 0.85
nodeMethod · 0.80
getMethod · 0.65
setMethod · 0.45

Tested by

no test coverage detected