-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handling of arrays that are the same but ordered differently #29
Comments
Thanks for the feedback. I'd consider an associative array (an object) to be the preferred way of ignoring order. The ordering of an array is usually quite important. I'd think, changing array ordering a destructive operation and would expect to see these changes. I'll have a think, I've a potential idea that might work and give you this option, but I may want to check how it performs. It involves creating a hash of the array value that could be used as an index during comparison, but feel it'd be limited to 1 dimensional arrays. |
So, I've had a look as part of Hacktoberfest. It seems straightforward enough for arrays of primitives: // Should return no difference
const testObjectA = [1, 2, 3];
const testObjectB = [3, 2, 1];
diffler(testObjectA, testObjectB, { respectArrayOrder: false }) // returns {} But for objects we're dealing with references as a possibility of infinite nesting and size and hard to deal with at this point. How many levels deep do you go to say something is the same? const testObjectA = {
myArray: [{ foo: 'bar', bar: { baz: { bat: [{ onAndOnAndOn: 'ad nauseum' }] } } }, { baz: 'bat' }],
};
const testObjectB = {
myArray: [{ baz: 'bat' }, { foo: 'bar', bar: { baz: { bat: [{ onAndOnAndOn: 'ad nauseum' }] } } }],
}; I do see value in this, so I'm happy to keep looking, I'm happy allowing an option to ignore array sort order for primitives. For objects, I think I'd have to create a memoized key or something that we could use to keep an index for sort ordering. If it ends up slowing the core functionality down to much, I may include it as a 'use at your own performance risk' plugin that can extend the functionality. |
Thanks for your work on this i.e. the pull request open. This will be helpful if you are not in control of the way a JSON object is serialized / deserialized. E.g. the object is sent to / retrieved from a third party API which occasionally re-orders arrays for some reason. This is my use case anyway |
Not really a bug I suppose but where you have two arrays in an object that are identical but in a different order diffler sees these as totally different arrays.
gives
{"myArray":{"0":{"foo":{"from":"bar","to":null},"baz":{"from":null,"to":"bat"}},"1":{"baz":{"from":"bat","to":null},"foo":{"from":null,"to":"bar"}}}}
so would be nice to have an option to ignore ordering in arrays
but even if you accept that the order does matter I am not sure how 'human readable' this output is? But I am sure this is pretty complex stuff I wouldn't like to try ;-)
This library seems to fill a gap I am searching for this kind of tool to compare changes between json objects.
The text was updated successfully, but these errors were encountered: