Starcounter-Jack / JSON-Patch

Lean and mean Javascript implementation of the JSON-Patch standard (RFC 6902). Update JSON documents using delta patches.
MIT License
1.78k stars 215 forks source link

Extended operations a.k.a. user-defined operations #284

Open superguineapig opened 2 years ago

superguineapig commented 2 years ago

Hello there! I've been using this library in a number of projects and have found it very useful.

Recently, at my company I needed the ability to perform simple mutations of JSON documents beyond those defined in the RFC-6902 specification. I really love the way the RFC defined its patch operation objects and wondered if there would be a way to extend that schema into user-defined operations. ("smart patches", if you will...)

I wound up implementing the specific changes I needed in our product, but quickly realized that it could be generalized to "reasonably arbitrary" operations and also work along side of the standard patch operations, and that we would have need to utilize such functionality immediately.

Using the JSON-Patch library as a starting point, I added "hooks" to allow processing of user-defined operation logic. It worked very well with almost no impact to the existing behavior and API.

I wasn't really planning on publishing this fork myself, (though I am using this code in production), but It seems like this functionality could be helpful to others, so I did some refactoring and added a full suite of tests and documentation and am submitting this pull request in case you'd like to incorporate this functionality into the public library as a major version release.

Please check out the docs in 'EXTENDED.md' for a much better overview of what this branch is all about.

The tests are at 'test/spec/extendedSpec.mjs'

I tried my best to not disturb any existing core functionality (core API is the same, all core tests pass w/o modifications) and match existing coding style.

Thanks for the hard work so far. JSON-Patch has been very helpful in several of my projects.

I hope you'll find this pull useful as well.

Cheers!

Starcounter-Jack commented 2 years ago

I will have a look at this. A plugin system that does not add size or lower performance for users without a plugin could work.