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

Support reference equality immutability #169

Open
RoboPhred opened this issue Feb 18, 2021 · 3 comments
Open

Support reference equality immutability #169

RoboPhred opened this issue Feb 18, 2021 · 3 comments

Comments

@RoboPhred
Copy link

RoboPhred commented Feb 18, 2021

This question was asked before at #91 but the only proposed answer was to modify the library.

I believe making this library optionally support immutable reference equality would be easy if the prefilter function also passed the lhs and rhs values to the prefilter. This would allow us to opt-in to reference equality using

function prefilterImmutable(path, key, lhs, rhs) {
  return lhs === rhs
}
diff(foo, bar, prefilterImmutable)

This allows skipping reference equality without having to modify the library's core function. The existing prefilter option is taken advantage of, and also is expanded on to allow other types of prefilters relying on the value of the thing being filtered (for example, possibly against objects signifying equality using ID properties).

@RoboPhred RoboPhred changed the title Support reference equality immutability using prefilter Support reference equality immutability Feb 18, 2021
@RoboPhred
Copy link
Author

It has occurred to me after writing this that a reference equality check should in fact be baked into the library, and there is no point making it optional. If two objects are the same reference, then by definition they have the same content, and there is never any need to recurse into them.

@LoganDark
Copy link

the only proposed answer was to modify the library.

You can also open a pull request

@YodelMonster
Copy link

Not a perfect implementation but you have the option of passing prefilter as an object which accepts a function by key normalize which does receive lhs and rhs and you can control checks by that.

const c = DeepDiff.diff(a1, a2, {
normalize: (currentPath, key, lhs, rhs)=> lhs===rhs ? [] : [lhs,rhs],
});

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

3 participants