Skip to content

Commit 802d3f9

Browse files
fix: compare mixed types of object
1 parent 849a3b4 commit 802d3f9

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

.changeset/warm-stingrays-notice.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opentf/obj-diff": minor
3+
---
4+
5+
Fixed mixed type comparison.

packages/obj-diff/__tests__/diff.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,4 +388,15 @@ describe("diff", () => {
388388
b = { s: new Set([1, 2, 3]) };
389389
expect(diff(a, b)).toEqual([{ t: 2, p: ["s"], v: new Set([1, 2, 3]) }]);
390390
});
391+
392+
test("mix objects", () => {
393+
const a = {
394+
m: new Map([[1, 2]]),
395+
};
396+
const b = {
397+
m: new Set([1]),
398+
};
399+
400+
expect(diff(a, b)).toEqual([{ t: 2, p: ["m"], v: new Set([1]) }]);
401+
});
391402
});

packages/obj-diff/src/diff.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ function objDiff(
88
objRefSet1: WeakSet<WeakKey>,
99
objRefSet2: WeakSet<WeakKey>
1010
): DiffResult[] {
11-
if (typeof a !== typeof b) {
12-
return [{ t: CHANGED, p: path, v: b }];
13-
}
14-
1511
const result: DiffResult[] = [];
1612

1713
if (typeof a === "object" && a !== null && b !== null) {
@@ -23,7 +19,7 @@ function objDiff(
2319
objRefSet1.add(a as WeakKey);
2420
objRefSet2.add(b as WeakKey);
2521

26-
if (Array.isArray(a)) {
22+
if (Array.isArray(a) && Array.isArray(b)) {
2723
for (let i = 0; i < a.length; i++) {
2824
if (Object.hasOwn(b, i)) {
2925
result.push(
@@ -53,7 +49,10 @@ function objDiff(
5349
return result;
5450
}
5551

56-
if (Object.prototype.toString.call(a) === "[object Object]") {
52+
if (
53+
Object.prototype.toString.call(a) === "[object Object]" &&
54+
Object.prototype.toString.call(b) === "[object Object]"
55+
) {
5756
for (const k of Object.keys(a)) {
5857
if (Object.hasOwn(b, k)) {
5958
result.push(
@@ -83,13 +82,13 @@ function objDiff(
8382
return result;
8483
}
8584

86-
if (a instanceof Date) {
85+
if (a instanceof Date && b instanceof Date) {
8786
if (!Object.is(a.getTime(), (b as Date).getTime())) {
8887
return [{ t: CHANGED, p: path, v: b }];
8988
}
9089
}
9190

92-
if (a instanceof Map) {
91+
if (a instanceof Map && b instanceof Map) {
9392
if (a.size !== (b as Map<unknown, unknown>).size) {
9493
return [{ t: CHANGED, p: path, v: b }];
9594
}
@@ -101,7 +100,7 @@ function objDiff(
101100
}
102101
}
103102

104-
if (a instanceof Set) {
103+
if (a instanceof Set && b instanceof Set) {
105104
if (a.size !== (b as Set<unknown>).size) {
106105
return [{ t: CHANGED, p: path, v: b }];
107106
}
@@ -112,6 +111,12 @@ function objDiff(
112111
}
113112
}
114113
}
114+
115+
if (
116+
Object.prototype.toString.call(a) !== Object.prototype.toString.call(b)
117+
) {
118+
return [{ t: CHANGED, p: path, v: b }];
119+
}
115120
} else {
116121
if (!Object.is(a, b)) {
117122
return [{ t: CHANGED, p: path, v: b }];

0 commit comments

Comments
 (0)