Skip to content

Commit

Permalink
[patch] resolve error thrown if mixed primitives/object types
Browse files Browse the repository at this point in the history
  • Loading branch information
omgaz committed Sep 22, 2022
1 parent 6d166de commit 7a1ab3f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ function diffler(obj1, obj2) {
var obj1Val = obj1[key],
obj2Val = obj2[key];

if (typeof obj1Val !== typeof obj2Val) {
diff[key] = {
from: obj1Val,
to: obj2Val,
};
break;
}

// If property exists in obj1 and not in obj2 then it has been removed
if (!(key in obj2)) {
diff[key] = {
Expand Down
34 changes: 34 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ describe('getDiff', () => {
assert.equal(differenceTo.a.to, null);
});

// https://github.com/omgaz/diffler/issues/31
it('should detect comparisons with defined undefined', () => {
const differenceFrom = diffler({ a: undefined, b: 'things' }, { a: 'more', b: 'things' });
const differenceTo = diffler({ a: 'some', b: 'things' }, { a: undefined, b: 'things' });
Expand All @@ -146,5 +147,38 @@ describe('getDiff', () => {
assert.equal(differenceFrom.a.from, undefined);
assert.equal(differenceTo.a.to, undefined);
});

// https://github.com/omgaz/diffler/issues/31
it('should detect comparisons with arrays of mixed types', () => {
const difference = diffler({ a: [1], b: ['one'] }, { a: ['one'], b: [1] });

assert.equal(Object.keys(difference).length, 2);

console.log(difference);

assert.equal(difference.a[0].from, '1');
assert.equal(difference.a[0].to, 'one');
assert.equal(difference.b[0].from, 'one');
assert.equal(difference.b[0].to, 1);
});

// https://github.com/omgaz/diffler/issues/31
it('should detect comparisons with arrays of mixed primitives and objects', () => {
const difference = diffler(
{ a: ['something'], b: [{ b: 'something' }] },
{ a: [{ a: 'something' }], b: ['something'] },
);

assert.equal(Object.keys(difference).length, 2);

assert.equal(difference.a[0].from, 'something');
assert.deepEqual(difference.a[0].to, {
a: 'something',
});
assert.deepEqual(difference.b[0].from, {
b: 'something',
});
assert.equal(difference.b[0].to, 'something');
});
});
});
2 changes: 1 addition & 1 deletion tests/performance.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function suite() {
}

const ops = benchmarker.bench10(suite);
const benchmark = 277610; // update value to set new benchmark
const benchmark = 255538; // update value to set new benchmark
console.info(`Executed ${ops} ops/s`);
const opsDiff = benchmark / ops;
const opsDiffAsPercentage = Math.round(opsDiff * 100);
Expand Down

0 comments on commit 7a1ab3f

Please sign in to comment.