Ignore transient fields
Is there an easy way to configure JsonDiffPatch to ignore some of the fields when comparing two objects? I have a scenario where JSON data is loaded from a storage and then additional derived fields are added to it for internal use by the application. When running diff, I don't want them to be included in the patch.
No, unfortunately, there's no way to filter fields, I take it as a feature request. I think having the opt to provide a filter: function(original, new, memberName) { return true/false; } would be the way. ps: pull requests are welcome.
+1 for this feature. Specifically, I am in a situation where I want to ignore some fields tacked on by the framework I am using (angularjs ng-repeat directive).
@sansjunk I just create temporary objects for diffing and delete them from both the objects. just before the diff is done. ''' delete left._ignoreField; delete right._ignoreField; '''
Yes that's one solution, but in my case I need to retain the added fields since they are used by the framework to track changes. Anyway I am working around this issue but I think this would be a good feature to have.
I'm adding the example I used in #77 just for clarity here too:
this is an example of how to filter object properties by writing a diff plugin:
var diffpatcher = jsondiffpatch.create();
// insert a new diff filter, right before "objects" one
diffpatcher.processor.pipes.diff.before('objects', objectPropertyTrimFilter);
function trimObject(obj) {
trimmedObject = {};
for (var name in obj) {
if (name.slice(0, 2) !== '$$') {
trimmedObject[name] = obj[name];
}
}
return trimmedObject;
}
function objectPropertyTrimFilter(context) {
if (!context.leftIsArray && context.leftType === 'object') {
// replace context left object with trimmed version
context.left = trimObject(context.left);
}
if (!context.rightIsArray && context.rightType === 'object') {
// replace context right object with trimmed version
context.right = trimObject(context.right);
}
}
// use your diffpatcher
left = { a: 1, $$key: 4 };
right = { a: 3, $$key: 6 };
diffpatcher.diff(left, right);
// result is { a: [1, 3] }
+1
+1
done, I added support for a propertyFilter(name, context) option, that would let you ignore properties either by name, or by any other test of the context left or right objects (see https://github.com/benjamine/jsondiffpatch#options )