@observ33r/object-equals
Version:
A fast, flexible and robust utility for deep equality comparison with type-specific logic and engine-aware design.
2 lines (1 loc) • 4.98 kB
JavaScript
const{getOwnPropertySymbols:t,keys:e,prototype:{hasOwnProperty:r,toString:n}}=Object,o=Array.prototype.splice,f=ArrayBuffer.isView,i=Symbol.for("react.transitional.element"),u="object"==typeof process,c="object"==typeof Deno,a="object"==typeof Bun,s=u&&void 0!==process.versions?.v8&&!a,l=u&&void 0!==process.versions?.webkit;if(c)try{var{isDeepStrictEqual:y}=await import("node:util")}catch{var y=!1}if("function"!=typeof p&&!c)try{var{Buffer:p}=await import("node:buffer")}catch{var p=!1}function b(u,g,v,h,j,d,A,w){if("object"==typeof u&&"object"==typeof g){if(!h){if(null===u||null===g)return u===g;var S=u.constructor;if(S!==g.constructor){if(!r.call(u,"constructor")&&!r.call(g,"constructor"))return!1;S=void 0}}if(j&&u.$$typeof===i&&g.$$typeof===i)return u.type===g.type&&u.key===g.key&&u.ref===g.ref&&b(u.props,g.props,v,h,j,d,A,w);if(v){let t=w.get(u);if(void 0!==t)return t===g;if(t=w.get(g),void 0!==t)return t===u;w.set(u,g).set(g,u)}for(;;){if(void 0!==S){if(S===Object){const n=e(u),o=n.length;if(o!==e(g).length)return!1;if(s&&o>1&&o<20||l&&o<66)for(const t in u){if(!r.call(u,t))continue;const e=g[t];if(void 0===e&&!r.call(g,t))return!1;const n=u[t];if(n!==e&&!b(n,e,v,h,j,d,A,w))return!1}else for(let t=o-1;t>=0;t--){const e=n[t],o=g[e];if(void 0===o&&!r.call(g,e))return!1;const f=u[e];if(f!==o&&!b(f,o,v,h,j,d,A,w))return!1}if(d){const e=t(u),n=e.length;if(n!==t(g).length)return!1;for(let t=n-1;t>=0;t--){const n=e[t],o=g[n];if(void 0===o&&!r.call(g,n))return!1;const f=u[n];if(f!==o&&!b(f,o,v,h,j,d,A,w))return!1}}return!0}if(S===Array){const t=u.length;if(t!==g.length)return!1;for(let e=t-1;e>=0;e--)if(u[e]!==g[e]&&!b(u[e],g[e],v,h,j,d,A,w))return!1;return!0}if(S===DataView){const t=u.byteLength;if(t!==g.byteLength)return!1;if(a&&t>160)return Bun.deepEquals(u,g);if(t>384){if(c&&y)return y(u,g);if(p)return u=new Uint8Array(u.buffer,u.byteOffset,t),g=new Uint8Array(g.buffer,g.byteOffset,t),0===p.compare(u,g)}const e=-4&t;if(e)for(let t=e-4;t>=0;t-=4)if(u.getInt32(t)!==g.getInt32(t))return!1;for(let r=t-1;r>=e;r--)if(u.getInt8(r)!==g.getInt8(r))return!1;return!0}if(S===ArrayBuffer||S===SharedArrayBuffer){if(a)return Bun.deepEquals(u,g);if(c&&y&&u.byteLength>1024)return y(u,g);u=new Uint8Array(u),g=new Uint8Array(g),S=Uint8Array}if(S===Uint8Array||f(u)){const t=u.length;if(t!==g.length)return!1;if(t>288&&p)return a||S===Uint8Array&&"[object Uint8Array]"===m||(u=new Uint8Array(u.buffer),g=new Uint8Array(g.buffer)),0===p.compare(u,g);const e=u.byteOffset,r=g.byteOffset,n=t>64&&e%4==0&&r%4==0?-4&t:0;if(n){const n=t/4|0,o=new Int32Array(u.buffer,e,n),f=new Int32Array(g.buffer,r,n);for(let t=n-1;t>=0;t--)if(o[t]!==f[t])return!1}for(let e=t-1;e>=n;e--)if(u[e]!==g[e])return!1;return!0}if(S===Map){if(u.size!==g.size)return!1;for(const{0:t,1:e}of u.entries()){const r=g.get(t);if(void 0===r&&!g.has(t))return!1;if(e!==r&&!b(e,r,v,h,j,d,A,w))return!1}return!0}if(S===Set){if(u.size!==g.size)return!1;const t=[];for(const e of u.values())if(!g.has(e)){if("object"!=typeof e)return!1;t.push(e)}if(0===t.length)return!0;const e=[];for(const t of g.values())null!==t&&"object"==typeof t&&e.push(t);if(t.length!==e.length)return!1;if(1===t.length)return b(t[0],e[0],v,h,j,d,A,w);t:for(let r=t.length-1;r>=0;r--){for(let n=e.length-1;n>=0;n--)if(b(t[r],e[n],v,h,j,d,A,w)){o.call(e,n,1);continue t}return!1}return!0}if(S===Date){const t=u.getTime(),e=g.getTime();return t===e||t!=t&&e!=e}if(S===RegExp)return u.source===g.source&&u.flags===g.flags&&u.lastIndex===g.lastIndex;if(S===Boolean||S===String)return u.valueOf()===g.valueOf();if(S===Number||S===BigInt){const t=u.valueOf(),e=g.valueOf();return t===e||t!=t&&e!=e}if(S===Error)return u.name===g.name&&u.message===g.message&&u.cause===g.cause}if(void 0===m){var m=n.call(u);if(m!==n.call(g))return!1;if(void 0!==(S="[object Object]"===m?Object:"[object Array]"===m?Array:"[object DataView]"===m?DataView:"[object ArrayBuffer]"===m?ArrayBuffer:"[object SharedArrayBuffer]"===m?SharedArrayBuffer:"[object Map]"===m?Map:"[object Set]"===m?Set:"[object Date]"===m?Date:"[object RegExp]"===m?RegExp:"[object Boolean]"===m?Boolean:"[object String]"===m?String:"[object Number]"===m?Number:"[object BigInt]"===m?BigInt:"[object Error]"===m?Error:f(u)?Uint8Array:void 0))continue}if(A){if("function"==typeof u.valueOf&&"function"==typeof g.valueOf){const t=u.valueOf(),e=g.valueOf();if(t!==u&&e!==g)return t===e||b(t,e,v,h,j,d,A,w)}if("function"==typeof u.toString&&"function"==typeof g.toString){const t=u.toString(),e=g.toString();if(t!==u&&e!==g)return t===e}}throw TypeError(`Unsupported object type: ${m}`)}}return j&&"function"==typeof u&&"function"==typeof g?u.toString()===g.toString():u===g||u!=u&&g!=g}function g(t,e,r){if(t===e)return!0;if(null==r&&"object"!=typeof r)return b(t,e,!1,!1,!1,!1,!1,void 0);const n=!0===r.circular;return b(t,e,n,!0===r.crossrealm,!0===r.react,!0===r.symbols,!0===r.fallback,n?null!=r.cache?r.cache:new Map:void 0)}export{g as objectEquals,b as objectEqualsCore};