DoneDeal0 / superdiff

Superdiff compares two arrays or objects and returns a full diff of their differences in a readable format.
https://www.npmjs.com/package/@donedeal0/superdiff
437 stars 3 forks source link

ignoreArrayOrder: true + referenceProperty set should make superdiff disregard changes of position in arrays #22

Closed martinjo closed 1 month ago

martinjo commented 1 month ago

I was hoping to get this case to work:

describe('JSON Diff with Superdiff', () => { const options = { referenceProperty: 'id', ignoreArrayOrder: false };

it('should ignore position changes in arrays', () => { const original = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]; const updated = [{ id: 2, name: 'Bob' }, { id: 1, name: 'Alice' }];

const diff = getObjectDiff(original, updated, options);
expect(diff.status).toBe('equal');

});

But instead it will list both array items as updated. Is there a way to make superdiff disregard changes of items position in an array completely?

DoneDeal0 commented 1 month ago

Hi @martinjo ,

It's easy to add. I'll add some tests in the week and publish a release asap.

martinjo commented 1 month ago

Oh, nice!

Thanks! 🙏

DoneDeal0 commented 1 month ago

The option has been added and the library has been updated on npm. Let me know if it works for you!

If your company uses the project for commercial purposes, feel free to support it.

Have a great day!

Release.

EDIT: Please note that you should use getListDiff to compare two arrays, and not getObjectDiff, as in your exemple.