benjamine / jsondiffpatch

Diff & patch JavaScript objects
MIT License
4.85k stars 472 forks source link

Add Pipe remove/replace methods and document them #197

Closed schnerd closed 7 years ago

schnerd commented 7 years ago

I'm creating a plugin (schnerd/jsondiffpatch-arrays-by-hash) and would love to have these pipe methods to manipulate the pipes array when adding my plugin.

Thoughts?

benjamine commented 7 years ago

makes sense! thank you for the contribution 👍

schnerd commented 7 years ago

@benjamine awesome! when do you generally do new releases on npm?

I currently have the following code:

const diffPipe = jsonDiffPatch.processor.pipes.diff;
const patchPipe = jsonDiffPatch.processor.pipes.patch;

let filterIndex = findIndex(diffPipe.filters, {filterName: 'arrays'});
diffPipe.filters[filterIndex] = jsondiffpatchArraysByHash.diffFilter;

// Replace patch arrays filter with our custom patch filter
filterIndex = findIndex(patchPipe.filters, {filterName: 'arrays'});
patchPipe.filters[filterIndex] = jsondiffpatchArraysByHash.patchFilter;

// Replace patch arrays collect children filter with our custom patch filter
filterIndex = findIndex(patchPipe.filters, {filterName: 'arraysCollectChildren'});
patchPipe.filters[filterIndex] = jsondiffpatchArraysByHash.collectChildrenPatchFilter;

but hoping to simplify it to this after next jsondiffpatch publish.

jsonDiffPatch.processor.pipes.diff
  .replace('arrays', jsondiffpatchArraysByHash.diffFilter);
jsonDiffPatch.processor.pipes.patch
  .replace('arrays', jsondiffpatchArraysByHash.patchFilter)
  .replace('arraysCollectChildren', jsondiffpatchArraysByHash.collectChildrenPatchFilter);
benjamine commented 7 years ago

sure, pushed as 0.2.5 👍

benjamine commented 7 years ago

btw, if you plan to open source that plugin would love to check it out and maybe add it on a section of 3rd party plugins on this project's README.

schnerd commented 7 years ago

You're awesome, thank you!

Here's the plugin: https://github.com/schnerd/jsondiffpatch-arrays-by-hash

It's basically a fork of the array filters that stores the result of objectHash in the diff to better support "merges" when arrays have been reordered. Might help #167 #154 #91. Would love to hear your thoughts.

Still doing some final testing, but once i'm done I can either reply here or submit a PR with a link on the 3rd party plugins page in this repo.

schnerd commented 7 years ago

Alright–done testing and updating documentation, and just published to npm. Feel free to check it out and link to it from the main jsondiffpatch docs if you'd like.