@stryke/helpers
Version:
A package containing miscellaneous helper functions that are used across many different Storm Software projects.
2 lines (1 loc) • 1.92 kB
JavaScript
import{isSet as s}from"@stryke/type-checks/is-set";import{isSetString as u}from"@stryke/type-checks/is-set-string";const l=typeof Map=="function",a=typeof Set=="function",c=typeof ArrayBuffer=="function"&&ArrayBuffer.isView!==void 0;function o(e,r){if(e===r)return!0;if(e&&r&&typeof e=="object"&&typeof r=="object"){if(e.constructor!==r.constructor)return!1;let n;if(Array.isArray(e)){if(n=e.length,n!=r.length)return!1;for(let t=n;t--!==0;)if(!o(e[t],r[t]))return!1;return!0}let i;if(l&&e instanceof Map&&r instanceof Map){if(e.size!==r.size)return!1;i=e.entries();let t;for(;!(t=i.next()).done;)if(!r.has(t.value[0]))return!1;for(i=e.entries();!(t=i.next()).done;)if(!o(t.value[1],r.get(t.value[0])))return!1;return!0}if(a&&e instanceof Set&&r instanceof Set){if(e.size!==r.size)return!1;i=e.entries();let t;for(;!(t=i.next()).done;)if(!r.has(t.value[0]))return!1;return!0}if(Array.isArray(e)&&Array.isArray(r)&&c&&ArrayBuffer.isView(e)&&ArrayBuffer.isView(r)){if(n=e.length,n!=r.length)return!1;for(let t=n;t--!==0;)if(e[t]!==r[t])return!1;return!0}if(e.constructor===RegExp)return e.source===r.source&&e.flags===r.flags;if(e.valueOf!==Object.prototype.valueOf&&typeof e.valueOf=="function"&&typeof r.valueOf=="function")return e.valueOf()===r.valueOf();if(e.toString!==Object.prototype.toString&&typeof e.toString=="function"&&typeof r.toString=="function")return e.toString()===r.toString();const f=Object.keys(e);if(n=f.length,n!==Object.keys(r).length)return!1;for(let t=n;t--!==0;)if(!s(t)||!u(f[t])||!Object.prototype.hasOwnProperty.call(r,f[t]))return!1;for(let t=n;t--!==0;)if(!(Array.isArray(f)&&(f[t]==="_owner"||f[t]==="__v"||f[t]==="__o")&&e.$$typeof)&&(!s(t)||!u(f[t])||!o(e[f[t]],r[f[t]])))return!1;return!0}return e!==e&&r!==r}export function isEqual(e,r){try{return o(e,r)}catch(n){if(/stack|recursion/i.test(n?.message||""))return console.warn("isEqual cannot handle circular refs"),!1;throw n}}