Browse Source

fix: isDeepEqual utility

e22m4u 4 months ago
parent
commit
a43db1e7c3
3 changed files with 7 additions and 0 deletions
  1. 1 0
      dist/cjs/index.cjs
  2. 2 0
      src/utils/is-deep-equal.js
  3. 4 0
      src/utils/is-deep-equal.spec.js

+ 1 - 0
dist/cjs/index.cjs

@@ -44,6 +44,7 @@ function isDeepEqual(firstValue, secondValue) {
   const compare = /* @__PURE__ */ __name((a, b) => {
   const compare = /* @__PURE__ */ __name((a, b) => {
     if (a === null || b === null) return a === b;
     if (a === null || b === null) return a === b;
     if (typeof a !== "object" || typeof b !== "object") return a === b;
     if (typeof a !== "object" || typeof b !== "object") return a === b;
+    if (a.constructor !== b.constructor) return false;
     const dataTypeA = Array.isArray(a) ? "array" : "object";
     const dataTypeA = Array.isArray(a) ? "array" : "object";
     const dataTypeB = Array.isArray(b) ? "array" : "object";
     const dataTypeB = Array.isArray(b) ? "array" : "object";
     if (dataTypeA !== dataTypeB) return false;
     if (dataTypeA !== dataTypeB) return false;

+ 2 - 0
src/utils/is-deep-equal.js

@@ -15,6 +15,8 @@ export function isDeepEqual(firstValue, secondValue) {
     // inputs is primitive, then I can compare them by reference.
     // inputs is primitive, then I can compare them by reference.
     if (a === null || b === null) return a === b;
     if (a === null || b === null) return a === b;
     if (typeof a !== 'object' || typeof b !== 'object') return a === b;
     if (typeof a !== 'object' || typeof b !== 'object') return a === b;
+    // Check if constructor of the two inputs are the same.
+    if (a.constructor !== b.constructor) return false;
     // Check if the data type of the two inputs are the same,
     // Check if the data type of the two inputs are the same,
     // both are arrays or objects. If they are not, return false.
     // both are arrays or objects. If they are not, return false.
     const dataTypeA = Array.isArray(a) ? 'array' : 'object';
     const dataTypeA = Array.isArray(a) ? 'array' : 'object';

+ 4 - 0
src/utils/is-deep-equal.spec.js

@@ -218,6 +218,10 @@ describe('isDeepEqual', function () {
       check(a, b, false);
       check(a, b, false);
       check(a, c, true);
       check(a, c, true);
     });
     });
+
+    it('non-plain object and empty object', function () {
+      check(new Date(), {}, false);
+    });
   });
   });
 
 
   it('undefined', function () {
   it('undefined', function () {