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

Function possiblyCollapse

ark/schema/parse.ts:340–362  ·  view source on GitHub ↗
(
	json: dict,
	toKey: string | undefined,
	allowPrimitive: allowPrimitive
)

Source from the content-addressed store, hash-verified

338}
339
340const possiblyCollapse = <allowPrimitive extends boolean>(
341 json: dict,
342 toKey: string | undefined,
343 allowPrimitive: allowPrimitive
344): [allowPrimitive] extends [false] ? dict : unknown => {
345 const collapsibleKeys = Object.keys(json)
346 if (collapsibleKeys.length === 1 && collapsibleKeys[0] === toKey) {
347 const collapsed = json[toKey]
348
349 if (allowPrimitive) return collapsed as never
350
351 if (
352 // if the collapsed value is still an object
353 hasDomain(collapsed, "object") &&
354 // and the JSON did not include any implied keys
355 (Object.keys(collapsed).length === 1 || Array.isArray(collapsed))
356 ) {
357 // we can replace it with its collapsed value
358 return collapsed as never
359 }
360 }
361 return json as never
362}

Callers 1

createNodeFunction · 0.85

Calls 1

hasDomainFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…