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

Function isJsonEqual

ark/schema/node.ts:415–443  ·  view source on GitHub ↗
(l: unknown, r: unknown)

Source from the content-addressed store, hash-verified

413const equalJson = new WeakMap<object, WeakSet<object>>()
414
415const isJsonEqual = (l: unknown, r: unknown): boolean => {
416 if (l === r) return true
417 const isObject = typeof l === "object" && l !== null
418 if (isObject !== (typeof r === "object" && r !== null)) return false
419 if (!isObject) return JSON.stringify(l) === JSON.stringify(r)
420 if (equalJson.get(l)?.has(r as object)) return true
421 const array = isArray(l)
422 if (array !== isArray(r)) return false
423 const lKeys = writtenKeysOf(l, array)
424 const rKeys = writtenKeysOf(r as object, array)
425 if (lKeys.length !== rKeys.length) return false
426 for (let i = 0; i < lKeys.length; i++) {
427 const k = lKeys[i]
428 if (k !== rKeys[i]) return false
429 const lValue = (l as Dict)[k]
430 const rValue = (r as Dict)[k]
431 // an array writes these as null
432 if (
433 !(array && isUnwritten(lValue) ? isUnwritten(rValue) || rValue === null
434 : array && isUnwritten(rValue) ? lValue === null
435 : isJsonEqual(lValue, rValue))
436 )
437 return false
438 }
439 const equal = equalJson.get(l)
440 if (equal) equal.add(r as object)
441 else equalJson.set(l, new WeakSet([r as object]))
442 return true
443}
444
445const writtenKeysOf = (json: object, array: boolean): string[] =>
446 array ?

Callers 3

hashesMatchFunction · 0.85
innerHashesMatchFunction · 0.85
isSimulatedFunction · 0.85

Calls 6

writtenKeysOfFunction · 0.85
isUnwrittenFunction · 0.85
getMethod · 0.65
hasMethod · 0.45
addMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected