MCPcopy Index your code
hub / github.com/arktypeio/arktype / createNode

Function createNode

ark/schema/parse.ts:229–307  ·  view source on GitHub ↗
({
	id,
	kind,
	inner,
	meta,
	$,
	ignoreCache
}: CreateNodeInput)

Source from the content-addressed store, hash-verified

227}
228
229export const createNode = ({
230 id,
231 kind,
232 inner,
233 meta,
234 $,
235 ignoreCache
236}: CreateNodeInput): BaseNode => {
237 const impl = nodeImplementationsByKind[kind]
238 const innerEntries = entriesOf(inner)
239 const children: BaseNode[] = []
240 let innerJson: dict = {}
241
242 for (const [k, v] of innerEntries) {
243 const keyImpl = impl.keys[k]
244 const serialize =
245 keyImpl.serialize ??
246 (keyImpl.child ? serializeListableChild : defaultValueSerializer)
247
248 innerJson[k] = serialize(v as never)
249
250 if (keyImpl.child === true) {
251 const listableNode = v as listable<BaseNode>
252 if (isArray(listableNode)) children.push(...listableNode)
253 else children.push(listableNode)
254 } else if (typeof keyImpl.child === "function")
255 children.push(...keyImpl.child(v as never))
256 }
257
258 if (impl.finalizeInnerJson)
259 innerJson = impl.finalizeInnerJson(innerJson) as never
260
261 let json = { ...innerJson }
262 let metaJson: ArkEnv.meta & dict = {}
263
264 if (!isEmptyObject(meta)) {
265 metaJson = flatMorph(meta, (k, v) => [
266 k,
267 k === "examples" ? v : defaultValueSerializer(v)
268 ]) as never
269 json.meta = possiblyCollapse(metaJson, "description", true)
270 }
271
272 innerJson = possiblyCollapse(innerJson, impl.collapsibleKey, false)
273 const innerHash = JSON.stringify({ kind, ...innerJson })
274
275 json = possiblyCollapse(json, impl.collapsibleKey, false)
276 const collapsibleJson = possiblyCollapse(json, impl.collapsibleKey, true)
277 const hash = JSON.stringify({ kind, ...json })
278
279 // we have to wait until after reduction to return a cached entry,
280 // since reduction can add impliedSiblings
281 if ($.nodesByHash[hash] && !ignoreCache) return $.nodesByHash[hash]
282
283 const attachments: UnknownAttachments & dict = {
284 id,
285 kind,
286 impl,

Callers 3

parseNodeFunction · 0.85
withIdFunction · 0.85
withMetaFunction · 0.85

Calls 5

isEmptyObjectFunction · 0.90
flatMorphFunction · 0.90
defaultValueSerializerFunction · 0.90
possiblyCollapseFunction · 0.85
stringifyMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…