@sema-agent/core
Version:
Stateless, task-oriented AI agent core
50 lines (49 loc) • 1.83 kB
JavaScript
const MAX_SAME_VALUE_DEPTH = 256;
export function samePlainValue(a, b) {
return sameValueAt(a, b, 0, new Map(), new Map());
}
function sameValueAt(a, b, depth, aToB, bToA) {
if (Object.is(a, b))
return true;
if (depth >= MAX_SAME_VALUE_DEPTH)
return false;
if (a === null || b === null || typeof a !== "object" || typeof b !== "object")
return false;
try {
const pairedWithA = aToB.get(a);
const pairedWithB = bToA.get(b);
if (pairedWithA !== undefined || pairedWithB !== undefined)
return pairedWithA === b && pairedWithB === a;
aToB.set(a, b);
bToA.set(b, a);
const proto = Object.getPrototypeOf(a);
if (proto !== Object.getPrototypeOf(b))
return false;
if (proto !== Object.prototype && proto !== Array.prototype)
return false;
if (Object.isExtensible(a) !== Object.isExtensible(b))
return false;
const ka = Reflect.ownKeys(a);
const kb = Reflect.ownKeys(b);
if (ka.length !== kb.length)
return false;
for (const [i, k] of ka.entries()) {
if (k !== kb[i])
return false;
const da = Object.getOwnPropertyDescriptor(a, k);
const db = Object.getOwnPropertyDescriptor(b, k);
if (da === undefined || db === undefined)
return false;
if (!("value" in da) || !("value" in db))
return false;
if (da.enumerable !== db.enumerable || da.writable !== db.writable || da.configurable !== db.configurable)
return false;
if (!sameValueAt(da.value, db.value, depth + 1, aToB, bToA))
return false;
}
return true;
}
catch {
return false;
}
}