@tanstack/persister
Version:
Utilities for persisting state to local storage, session storage, indexedDB, and more.
73 lines (72 loc) • 1.84 kB
JavaScript
function replaceEqualDeep(a, b) {
if (a === b) {
return a;
}
const array = isPlainArray(a) && isPlainArray(b);
if (array || isPlainObject(a) && isPlainObject(b)) {
const aItems = array ? a : Object.keys(a);
const aSize = aItems.length;
const bItems = array ? b : Object.keys(b);
const bSize = bItems.length;
const copy = array ? [] : {};
let equalItems = 0;
for (let i = 0; i < bSize; i++) {
const key = array ? i : bItems[i];
if ((!array && aItems.includes(key) || array) && a[key] === void 0 && b[key] === void 0) {
copy[key] = void 0;
equalItems++;
} else {
copy[key] = replaceEqualDeep(a[key], b[key]);
if (copy[key] === a[key] && a[key] !== void 0) {
equalItems++;
}
}
}
return aSize === bSize && equalItems === aSize ? a : copy;
}
return b;
}
function shallowEqualObjects(a, b) {
if (!b || Object.keys(a).length !== Object.keys(b).length) {
return false;
}
for (const key in a) {
if (a[key] !== b[key]) {
return false;
}
}
return true;
}
function isPlainArray(value) {
return Array.isArray(value) && value.length === Object.keys(value).length;
}
function isPlainObject(o) {
if (!hasObjectPrototype(o)) {
return false;
}
const ctor = o.constructor;
if (ctor === void 0) {
return true;
}
const prot = ctor.prototype;
if (!hasObjectPrototype(prot)) {
return false;
}
if (!prot.hasOwnProperty("isPrototypeOf")) {
return false;
}
if (Object.getPrototypeOf(o) !== Object.prototype) {
return false;
}
return true;
}
function hasObjectPrototype(o) {
return Object.prototype.toString.call(o) === "[object Object]";
}
export {
isPlainArray,
isPlainObject,
replaceEqualDeep,
shallowEqualObjects
};
//# sourceMappingURL=compare.js.map