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

Function digestOf

ark/schema/node.ts:379–402  ·  view source on GitHub ↗
(json: object)

Source from the content-addressed store, hash-verified

377
378// a nested object past maxInlinedLength is written as "#" and its digest
379const digestOf = (json: object): string => {
380 let digest = digests.get(json)
381 if (digest === undefined) {
382 const array = isArray(json)
383 const entries: string[] = []
384 for (const k of Object.keys(json)) {
385 const v = (json as Dict)[k]
386 const written =
387 (
388 typeof v === "object" &&
389 v !== null &&
390 jsonLengthOf(v) > maxInlinedLength
391 ) ?
392 `#${digestOf(v)}`
393 : (JSON.stringify(v) as string | undefined)
394 if (array) entries.push(written ?? "null")
395 else if (written !== undefined)
396 entries.push(`${JSON.stringify(k)}:${written}`)
397 }
398 digest = hashOf(array ? `[${entries.join()}]` : `{${entries.join()}}`)
399 digests.set(json, digest)
400 }
401 return digest
402}
403
404// digests can collide, so hashes ending in one are confirmed by their JSON
405export const hashesMatch = (l: BaseNode, r: BaseNode): boolean =>

Callers 1

hashJsonOfFunction · 0.70

Calls 5

hashOfFunction · 0.90
jsonLengthOfFunction · 0.85
getMethod · 0.65
pushMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected