Skip to content
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

Proposal: Add an 'areSame' flag. #52

Open
dwjohnston opened this issue Nov 27, 2019 · 1 comment
Open

Proposal: Add an 'areSame' flag. #52

dwjohnston opened this issue Nov 27, 2019 · 1 comment

Comments

@dwjohnston
Copy link

dwjohnston commented Nov 27, 2019

basically, each diff type can have an additional flag, areSame, which is set true if the objects are the same.

This functionality can be achieved with this:

const result = detailedDiff(oldItem, newItem); 
const areSame = Object.values(result).reduce((acc, cur) => {
        return acc && Object.values(cur).length === 0
    }, true); 
@anko
Copy link
Contributor

anko commented Nov 30, 2019

Simpler workaround:

const result = detailedDiff(oldItem, newItem); 
const areSame = Object.values(result)
    .every((obj) => Object.keys(obj).length === 0);

I like this idea. The internals could determine this basically for free (just updating a variable in a loop that already exists). Externally, it's more expensive to call Object.values and Object.keys like this on large objects.

Going further: Rather than a boolean areSame, make it an integer total that is 0 if the objects are the same, and +1 for every change, of any type. It's as cheap to calculate (update a variable in a loop that already exists), but gives more information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants