Skip to content

Commit 7a1ab3f

Browse files
committed
[patch] resolve error thrown if mixed primitives/object types
1 parent 6d166de commit 7a1ab3f

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

src/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ function diffler(obj1, obj2) {
1919
var obj1Val = obj1[key],
2020
obj2Val = obj2[key];
2121

22+
if (typeof obj1Val !== typeof obj2Val) {
23+
diff[key] = {
24+
from: obj1Val,
25+
to: obj2Val,
26+
};
27+
break;
28+
}
29+
2230
// If property exists in obj1 and not in obj2 then it has been removed
2331
if (!(key in obj2)) {
2432
diff[key] = {

tests/index.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ describe('getDiff', () => {
134134
assert.equal(differenceTo.a.to, null);
135135
});
136136

137+
// https://github.com/omgaz/diffler/issues/31
137138
it('should detect comparisons with defined undefined', () => {
138139
const differenceFrom = diffler({ a: undefined, b: 'things' }, { a: 'more', b: 'things' });
139140
const differenceTo = diffler({ a: 'some', b: 'things' }, { a: undefined, b: 'things' });
@@ -146,5 +147,38 @@ describe('getDiff', () => {
146147
assert.equal(differenceFrom.a.from, undefined);
147148
assert.equal(differenceTo.a.to, undefined);
148149
});
150+
151+
// https://github.com/omgaz/diffler/issues/31
152+
it('should detect comparisons with arrays of mixed types', () => {
153+
const difference = diffler({ a: [1], b: ['one'] }, { a: ['one'], b: [1] });
154+
155+
assert.equal(Object.keys(difference).length, 2);
156+
157+
console.log(difference);
158+
159+
assert.equal(difference.a[0].from, '1');
160+
assert.equal(difference.a[0].to, 'one');
161+
assert.equal(difference.b[0].from, 'one');
162+
assert.equal(difference.b[0].to, 1);
163+
});
164+
165+
// https://github.com/omgaz/diffler/issues/31
166+
it('should detect comparisons with arrays of mixed primitives and objects', () => {
167+
const difference = diffler(
168+
{ a: ['something'], b: [{ b: 'something' }] },
169+
{ a: [{ a: 'something' }], b: ['something'] },
170+
);
171+
172+
assert.equal(Object.keys(difference).length, 2);
173+
174+
assert.equal(difference.a[0].from, 'something');
175+
assert.deepEqual(difference.a[0].to, {
176+
a: 'something',
177+
});
178+
assert.deepEqual(difference.b[0].from, {
179+
b: 'something',
180+
});
181+
assert.equal(difference.b[0].to, 'something');
182+
});
149183
});
150184
});

tests/performance.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function suite() {
88
}
99

1010
const ops = benchmarker.bench10(suite);
11-
const benchmark = 277610; // update value to set new benchmark
11+
const benchmark = 255538; // update value to set new benchmark
1212
console.info(`Executed ${ops} ops/s`);
1313
const opsDiff = benchmark / ops;
1414
const opsDiffAsPercentage = Math.round(opsDiff * 100);

0 commit comments

Comments
 (0)