Complete TypeScript implementation of RFC6902 "JavaScript Object Notation (JSON) Patch" (including RFC6901 "JavaScript Object Notation (JSON) Pointer"), for creating and consuming
application/json-patch+json
documents with optional custom minified format support for reducing bandwidth.Also offers "diff" functionality to create patches without
Object.observe
npm install mini-rfc6902
const { create, apply } = require('mini-rfc6902');
or
import { create, apply } from "mini-rfc6902";
create({ first: 'Jake' }, { first: 'Jake', last: 'Holland' });
// [{ op: 'add', path: '/last', value: 'Holland' }]
const obj = { first: 'Jake' }
const patch = [{ op: 'add', path: '/last', value: 'Holland' }];
apply(obj, patch)
// { first: 'Jake', last: 'Holland' }
Optional CreateOpts
argument
opts.eq(x: Exclude<any, null | undefined>, y: Exclude<any, null | undefined>, opts: {skip: () => void}): boolean
User defined equality function, this is called whenever we are comparing two values for equality, if two values are deemed equal we do not traverse deeper inside of it to check for differences
calling the opts.skip()
method from within this definition will allow the default equality handlers to run
User defined clone function, this is called whenever we are returning a value from the input back in a patch, to ensure mutations don't occur.
calling the opts.skip()
method from within this definition will allow the default clone handlers to run
opts.diff(input: Exclude<any, null | undefined>, output: Exclude<any, null | undefined>, ptr: Pointer, opts: {skip: () => void}): Patch
User defined diff creation function, this is called whenever we hit a point to compute the difference between two values
calling the opts.skip()
method from within this definition will allow the default diff handlers to run
Configure whether to transform the output patch into minify
or maximize
, by default all inbuilt operations
return minified patches, but user defined diffs may not
Returns a list of operations (a JSON Patch) comprised of the operations to transform input
into output
.
It attempts to produce the smallest patch, this does not necessarily mean the smallest number of operations,
as a full replacement may result in more bytes being sent.
For array transformations we attempt to reduce the size of operations by running an edit distance style algorithm,
with support for add
, remove
, replace
, copy
, array replace
operations.
Optional ApplyOpts
argument
opts.eq(x: Exclude<any, null | undefined>, y: Exclude<any, null | undefined>, opts: {skip: () => void}): boolean
User defined equality function, this is called whenever we are comparing two values for equality, if two values are deemed equal we do not traverse deeper inside of it to check for differences
calling the opts.skip()
method from within this definition will allow the default equality handlers to run
User defined clone function, this is called whenever we are returning a value from the input back in a patch, to ensure mutations don't occur.
calling the opts.skip()
method from within this definition will allow the default clone handlers to run
Configure whether to transform the output patch into minify
or maximize
, by default all inbuilt operations
return minified patches, but user defined diffs may not
Takes a given patch and applies the operations to a deep copy of the target, it returns the final modified outcome of all the patches.
If any of the operations fail, an error is thrown with details as to what happened.
You can find more about this on GitHub.
Contributions, issues and feature requests are welcome!
Feel free to check issues page.
Thanks to rfc6902 for the inspiration
See also the list of contributors who participated in this project.
This project is MIT licensed.