benjamine / jsondiffpatch

Diff & patch JavaScript objects
MIT License
4.79k stars 466 forks source link

[question] is it possible to omit certain fields from the diff? #258

Open vlamic opened 5 years ago

vlamic commented 5 years ago

First of all - thanks for your library - it rocks! ❤️

I have a question. I know it's possible to use propertyFilter to exclude certain fields from participating in diff. However, these fields are still present in the resulting diff (applies for added elements). Is there an option to also exclude these field for these added elements?

Thanks!

mebibou commented 3 years ago

+1 for this feature, it is nice to be able to ignore properties but since they are still displayed it seems as if both left and right objects have the same value which is incorrect. Having a displayFilter would be nice?

Note: right now I am using lodash omit to exclude those properties but this works out of the box only on top properties so not ideal

prasanmgc commented 2 years ago

Hi @vlamic,

Could you please explain how you have implemented the propertyFilter to exclude certain fields? your help much appreciated?

Thanks

mebibou commented 2 years ago

@prasanmgc you just need to do propertyFilter: name => !['field1', 'field2'].includes(name) and field1/field2 won't be compared

prasanmgc commented 2 years ago

Thanks a lot @mebibou. It worked. you saved my time.

prasanmgc commented 2 years ago

@mebibou Just a quick question. Is it possible to reference the ignored fields from a class variable? I am getting an error when i use the below. propertyFilter: function (name: string) { !this.ignoredFields.includes(name); }

TypeError: Cannot read property 'ignoredFields' of undefined

mebibou commented 2 years ago

@prasanmgc I suggest you read first what this refers to in JavaScript to understand the error. But you should do this instead

var ignoredFields = [];
create({
  propertyFilter(name: string) { return !ignoredFields.includes(name); }
});
gnyani commented 1 year ago

@mebibou @prasanmgc property filter doesn't remove the fields from the nested objects, did it work for you for nested objects too?