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

Function digestOf

ark/type/runtime.ts:1296–1328  ·  view source on GitHub ↗
(value: object)

Source from the content-addressed store, hash-verified

1294 * of each key and value, which would name them all again
1295 */
1296export const digestOf = (value: object): string => {
1297 let low = 0xdeadbeef
1298 let high = 0x41c6ce57
1299 // a part's end is mixed in too, so no two splits of one text agree
1300 const add = (part: string) => {
1301 for (let i = 0; i <= part.length; i++) {
1302 const code = i < part.length ? part.charCodeAt(i) : 0x10ffff
1303 low = Math.imul(low ^ code, 2654435761)
1304 high = Math.imul(high ^ code, 1597334677)
1305 }
1306 }
1307 const prototype = prototypeOf(value)
1308 add(
1309 Array.isArray(value) ? "array"
1310 : prototype === Object.prototype ? "object"
1311 : prototype === null ? "null"
1312 : "instance"
1313 )
1314 for (const k of Reflect.ownKeys(value)) {
1315 if (k === "length" && Array.isArray(value)) continue
1316 const v: unknown = Reflect.get(value, k)
1317 add(String(k))
1318 add(typeof v)
1319 add(Object.is(v, -0) ? "-0" : String(v))
1320 }
1321 low =
1322 Math.imul(low ^ (low >>> 16), 2246822507) ^
1323 Math.imul(high ^ (high >>> 13), 3266489909)
1324 high =
1325 Math.imul(high ^ (high >>> 16), 2246822507) ^
1326 Math.imul(low ^ (low >>> 13), 3266489909)
1327 return (4294967296 * (2097151 & high) + (low >>> 0)).toString(36)
1328}
1329
1330export const writeDigestMismatchMessage = <label extends string>(
1331 label: label

Callers 1

checkDigestFunction · 0.70

Calls 6

prototypeOfFunction · 0.90
StringInterface · 0.85
isMethod · 0.80
addFunction · 0.70
getMethod · 0.65
toStringMethod · 0.65

Tested by

no test coverage detected