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

Function jsonPrefixOf

ark/schema/node.ts:359–374  ·  view source on GitHub ↗
(json: unknown, max: number)

Source from the content-addressed store, hash-verified

357 v === undefined || typeof v === "function" || typeof v === "symbol"
358
359const jsonPrefixOf = (json: unknown, max: number): string => {
360 if (max <= 0) return ""
361 if (typeof json !== "object" || json === null)
362 return JSON.stringify(json).slice(0, max)
363 const array = isArray(json)
364 let prefix = array ? "[" : "{"
365 for (const k of Object.keys(json)) {
366 if (prefix.length >= max) return prefix.slice(0, max)
367 const v = (json as Dict)[k]
368 if (!array && isUnwritten(v)) continue
369 if (prefix.length > 1) prefix += ","
370 if (!array) prefix += `${JSON.stringify(k)}:`
371 prefix += isUnwritten(v) ? "null" : jsonPrefixOf(v, max - prefix.length)
372 }
373 return `${prefix}${array ? "]" : "}"}`.slice(0, max)
374}
375
376const digests = new WeakMap<object, string>()
377

Callers 1

hashJsonOfFunction · 0.85

Calls 1

isUnwrittenFunction · 0.85

Tested by

no test coverage detected