• Stars
    star
    588
  • Rank 76,022 (Top 2 %)
  • Language
    JavaScript
  • License
    Other
  • Created over 10 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

JSON Patch and diff based on rfc6902

JSON Diff and Patch

Jiff is an implementation of JSON Patch RFC6902, plus a Diff implementation that generates compliant patches.

It also provides advanced and experimental APIs based on patch algebra, such as patch inverses ("reverse" patches), commutation (patch reordering), and even rebasing (moving patches from one history to another).

Get it

npm install --save jiff

bower install --save jiff

Example

var a = [
	{ name: 'a' },
	{ name: 'b' },
	{ name: 'c' },
]

var b = a.slice();
b.splice(1, 1);
b.push({ name: 'd' });

// Generate diff (ie JSON Patch) from a to b
var patch = jiff.diff(a, b);

// [{"op":"add","path":"/3","value":{"name":"d"}},{"op":"remove","path":"/1"}]
console.log(JSON.stringify(patch));

var patched = jiff.patch(patch, a);

// [{"name":"a"},{"name":"c"},{"name":"d"}]
console.log(JSON.stringify(patched));

API

patch

var b = jiff.patch(patch, a [, options]);

Given an rfc6902 JSON Patch, apply it to a and return a new patched JSON object/array/value. Patching is atomic, and is performed on a clone of a. Thus, if patching fails mid-patch, a will still be in a consistent state.

  • options
    • options.findContext : function(index, array, context) -> number: Experimental function to be called before each change to an array. It is passed the array and index of the change, and a patch context (see options.makeContext below). It should return an adjusted index at which the change will actually be applied. This allows for smart patching of arrays that may have changed since the patch was created.

Throws InvalidPatchOperationError and TestFailedError.

patchInPlace

a = jiff.patchInPlace(patch, a [, options]);

Given an rfc6902 JSON Patch, apply it directly to a, mutating a.

Note that this is an opt-in violation of the patching algorithm outlined in rfc6902. It may provide some performance benefits as it avoids creating a new clone of a before patching.

However, if patching fails mid-patch, a will be left in an inconsistent state.

Throws InvalidPatchOperationError and TestFailedError.

diff

var patch = jiff.diff(a, b [, hashFunction | options]);

Computes and returns a JSON Patch from a to b: a and b must be valid JSON objects/arrays/values. If patch is applied to a, it will yield b.

The optional third parameter can be either an options object (preferably) or a function (deprecated: allowed backward compatibility).

  • options:
    • options.hash : function(x) -> string|number: used to recognize when two objects are the same. If not provided, JSON.stringify will be used for objects and arrays, and simply returns x for all other primitive values.
    • options.makeContext : function(index, array) -> *: Experimental function that will be called for each item added or removed from an array. It can return any legal JSON value or undefined, which if not null or undefined, will be fed directly to the findContext function provided to jiff.patch.
    • options.invertible : boolean: by default, jiff generates patches containing extra test operations to ensure they are invertible via jiff.inverse. When options.invertible === false will omit the extra test operations. This will result in smaller patches, but they will not be invertible.
  • hashFunction(x) -> string|number: same as options.hash above

While jiff's patch algorithm handles all the JSON Patch operations required by rfc6902, the diff algorithm currently does not generate move, or copy operations, only add, remove, and replace.

inverse

var patchInverse = jiff.inverse(patch);

Compute an inverse patch. Applying the inverse of a patch will undo the effect of the original.

Due to the current JSON Patch format defined in rfc6902, not all patches can be inverted. To be invertible, a patch must have the following characteristics:

  1. Each remove and replace operation must be preceded by a test operation that verifies the value at the path being removed/replaced.
  2. The patch must not contain any copy operations. Read this discussion to understand why copy operations are not (yet) invertible. You can achieve the same effect by using add instead of copy, albeit potentially at the cost of increased patch size.

clone

var b = jiff.clone(a);

Creates a deep copy of a, which must be a valid JSON object/array/value.

NOTE: In jiff <= 0.6.x, jiff.clone incorrectly caused some ISO Date-formatted strings (eg "2014-12-03T11:40:16.816Z") to be turned into Date objects. Thus, a clone might not end up as an exact copy.

As of 0.7.0 jiff.clone creates exact copies.

If you have code that depended on that hidden deserialization, it will break. Date deserialization is now the responsibility of the party who parsed the JSON string from which the original object/array/etc. (ie, the one passed to jiff.clone) was created.

Patch context

As of v0.2, jiff.diff and jiff.patch support patch contexts, an extra bit of information carried with each patch operation. Patch contexts allow smarter patching, especially in the case of arrays, where items may have moved and thus their indices changed.

Using patch contexts can greatly improve patch accuracy for arrays, at the cost of increasing the size of patches.

Patch contexts are entirely opt-in. To use them, you must provide a pair of closely related functions: makeContext and findContext. An API for creating default makeContext and findContext functions is provided in jiff/lib/context, or you can implement your own.

When you supply the optional makeContext function to jiff.diff, it will be used to generated a context for each change to an array.

Likewise, when you supply the optional findContext function to jiff.patch (or jiff.patchInPlace), it will be used to find adjusted array indices where patches should actually be applied.

The context is opaque, and jiff itself will not attempt to inspect or interpret it: jiff.diff will simply add whatever is returned by makeContext to patch operations, and jiff.patch will simply hand it to findContext when it sees a context in a patch operation.

Experimental APIs

These APIs are still considered experimental, signatures may change.

jiff/lib/context

var context = require('jiff/lib/context');

// Create a makeContext function that can be passed to jiff.diff
var makeContext = context.makeContext(size);

// Create a findContext function that can be passed to jiff.patch
var findContext = context.makeContextFinder(equals);

Provides simple, but effective default implementations of makeContext and findContext functions that can be passed to jiff.diff and jiff.patch to take advantage of smarter array patching.

context.makeContext(size) returns a function that can be passed as options.makeContext to jiff.diff. * size: number is the number of array items before and after each change to include in the patch.

context.makeContextFinder(equals) returns a function that can be passed as options.findContext to jiff.patch. * equals: function(a, b) -> boolean a function to compare two array items, must return truthy when a and b are equal, falsy otherwise.

jiff/lib/rebase

var rebase = require('jiff/lib/rebase');
var patchRebased = rebase(patchHistory, patch);

Yes, this is git rebase for JSON Patch.

Given a patchHistory (Array of patches), and a single patch rooted at the same starting document context, rebase patch onto patchHistory, so that it may be applied after patchHistory.

Rebasing is dependent on commutation, and so is also highly experimental. If the rebase cannot be performed, it will throw a TypeError.

jiff/lib/commute

var commute = require('jiff/lib/commute');
var [p2c, p1c] = commute(p1, p2);

Given two patches p1 and p2, which are intended to be applied in the order p1 then p2, transform them so that they can be safely applied in the order p2c and then p1c.

Commutation is currently highly experimental. It works for patch operations whose path refers to a common array ancestor by transforming array indices. Operations that share a common object ancestor are simply swapped for now, which is likely not the right thing in most cases!

Commutation does attempt to detect operations that cannot be commuted, and in such cases, will throw a TypeError.

Errors

InvalidPatchOperationError

Thrown when any invalid patch operation is encountered. Invalid patch operations are outlined in sections 4.x and 5 in rfc6902. For example: non-existent path in a remove operation, array path index out of bounds, etc.

TestFailedError

Thrown when a test operation fails.

License

MIT

More Repositories

1

most

Ultra-high performance reactive programming
JavaScript
3,496
star
2

when

A solid, fast Promises/A+ and when() implementation, plus other async goodies.
JavaScript
3,450
star
3

curl

curl.js is small, fast, extensible module loader that handles AMD, CommonJS Modules/1.1, CSS, HTML/text, and legacy scripts.
JavaScript
1,889
star
4

rest

RESTful HTTP client for JavaScript
JavaScript
1,001
star
5

wire

A light, fast, flexible Javascript IOC container
JavaScript
863
star
6

meld

AOP for JS with before, around, on, afterReturning, afterThrowing, after advice, and pointcuts
JavaScript
645
star
7

poly

Small, fast, awesome. The only ES5-ish set of polyfills (shims) you can mix-and-match because they're individual modules.
JavaScript
139
star
8

msgs

Message oriented programming
JavaScript
121
star
9

seed

Starter kit for building cujo.js apps
JavaScript
67
star
10

cram

Simple, yet powerful, AMD and CommonJS module bundler.
JavaScript
59
star
11

cola

Extensible, two-way data binding.
JavaScript
41
star
12

promise-perf-tests

(No longer maintained) Basic performance tests for promise implementations
JavaScript
31
star
13

most-w3msg

cujojs/most W3C Messaging - reactive event streams for WebSocket, Worker, EventSource, MessagePort, etc
JavaScript
30
star
14

canhaz

a project and code bootstrapping tool that will save you tons of typing. Stop typing boilerplate, canhaz it instead!
Shell
27
star
15

cujojs.github.com

The cujojs website
JavaScript
24
star
16

cujo

The Unframework
24
star
17

aop

Small AOP lib for JS with before, around, afterReturning, afterThrowing, after advice and a simple API
JavaScript
15
star
18

pile

Simple data structures for JS
JavaScript
10
star
19

robo

A simple, asynchronous finite state machine
JavaScript
8
star
20

most-dom

most.js DOM extras - signals from form values, streams from event delegation, etc.
JavaScript
6
star
21

shim

A collection of UMD modules that shim (aka "polyfill") old environments to support modern (aka "ES5-ish" and "ES6-ish") javascript.
JavaScript
4
star
22

latr

Higher-order promise and async task management modules.
JavaScript
3
star