Comparing version 0.0.3 to 1.0.0-rc1
@@ -5,3 +5,4 @@ --- | ||
 <span class="project-name">jsonpatch.js</span> is an implementation of the [JSONPatch][jsonpatch-spec] and [JSONPointer][jsonpointer-spec] IETF draft specifications whcih supports Node.JS and use in the Browser. | ||
 <span class="project-name">jsonpatch.js</span> is an implementation of the [JSONPatch][jsonpatch-spec] (RFC 6902) and | ||
[JSONPointer][jsonpointer-spec] (RFC 6901) IETF specifications which supports Node.JS and use in the Browser. | ||
@@ -13,4 +14,4 @@ A [Dharmafly][df] project written by [Thomas Parslow][tom] and released with the kind permission of [NetDev][netdev]. | ||
[jsonpatch-spec]: https://tools.ietf.org/html/draft-ietf-appsawg-json-patch | ||
[jsonpointer-spec]: http://tools.ietf.org/html/draft-ietf-appsawg-json-pointer | ||
[jsonpatch-spec]: http://tools.ietf.org/html/rfc6902 | ||
[jsonpointer-spec]: http://tools.ietf.org/html/rfc6901 | ||
[df]: http://dharmafly.com | ||
@@ -17,0 +18,0 @@ [tom]: http://almostobsolete.net |
@@ -6,3 +6,3 @@ --- | ||
A shortcut to apply a patch given as an Array or as a JSON String (see the [draft JSONPatch spec][#jsonpatch] for the patch format) to a document. Will not mutate the original document, however the new document may share some structure with the original. | ||
A shortcut to apply a patch given as an Array or as a JSON String (see the [JSONPatch RFC 6902][#jsonpatch] for the patch format) to a document. Will not mutate the original document, however the new document may share some structure with the original. | ||
@@ -26,3 +26,3 @@ | ||
[#jsonpatch]: https://datatracker.ietf.org/doc/draft-ietf-appsawg-json-patch/ | ||
[#jsonpatch]: http://tools.ietf.org/html/rfc6902 | ||
@@ -29,0 +29,0 @@ Throws: |
@@ -5,7 +5,6 @@ --- | ||
Represents a pointer into a Javascript object. The constructor takes a single string argument giving the pointer (see the [draft JSONPointer spec][#jsonpointer] for the pointer format). | ||
Represents a pointer into a Javascript object. The constructor takes a single string argument giving the pointer (see the [JSONPointer RFC][#jsonpointer] for the pointer format). | ||
mypointer = new JSONPointer('/foo/baz/bar'); | ||
[#jsonpointer]: https://datatracker.ietf.org/doc/draft-ietf-appsawg-json-pointer/ | ||
[#jsonpointer]: http://tools.ietf.org/html/rfc6901 |
@@ -1,2 +0,3 @@ | ||
/* JSONPatch.js | ||
/* @preserve | ||
* JSONPatch.js | ||
* | ||
@@ -7,3 +8,3 @@ * A Dharmafly project written by Thomas Parslow | ||
* | ||
* Copyright 2011 Thomas Parslow. All rights reserved. | ||
* Copyright 2011-2013 Thomas Parslow. All rights reserved. | ||
* Permission is hereby granted,y free of charge, to any person obtaining a copy | ||
@@ -27,9 +28,9 @@ * of this software and associated documentation files (the "Software"), to | ||
* | ||
* Implements the JSON Patch IETF Draft as specified at: | ||
* Implements the JSON Patch IETF RFC 6902 as specified at: | ||
* | ||
* http://tools.ietf.org/html/draft-pbryan-json-patch-06 | ||
* http://tools.ietf.org/html/rfc6902 | ||
* | ||
* Also implements the JSON Pointer IETF Draft as specified at: | ||
* Also implements the JSON Pointer IETF RFC 6901 as specified at: | ||
* | ||
* http://tools.ietf.org/html/draft-pbryan-zyp-json-pointer-05 | ||
* http://tools.ietf.org/html/rfc6901 | ||
* | ||
@@ -51,3 +52,3 @@ */ | ||
}(this, function (exports) { | ||
var apply_patch, JSONPatch, JSONPointer,_operation,_operationRequired,isArray; | ||
var apply_patch, JSONPatch, JSONPointer,_operationRequired,isArray; | ||
@@ -114,3 +115,2 @@ // Taken from underscore.js | ||
* | ||
* Draft Spec: https://datatracker.ietf.org/doc/draft-ietf-appsawg-json-pointer/ | ||
*/ | ||
@@ -173,3 +173,3 @@ exports.JSONPointer = JSONPointer = function JSONPointer (pathStr) { | ||
* the handler with the current doc and the last key (converted to | ||
* an int if the current doc is an array). The handle is expected to | ||
* an int if the current doc is an array). The handler is expected to | ||
* return a new copy of the penultimate part. | ||
@@ -219,3 +219,3 @@ * | ||
* in an array then the existing element at that position (if any) | ||
* and all that follow it have there position incremented to make | ||
* and all that follow it have their position incremented to make | ||
* room. It is an error to add to a parent object that doesn't exist | ||
@@ -288,3 +288,3 @@ * or to try to replace an existing value in an object. | ||
/* Public: Semantically equivalent to an remove followed by an add | ||
/* Public: Semantically equivalent to a remove followed by an add | ||
* except when the pointer points to the root element in which case | ||
@@ -344,3 +344,3 @@ * the whole document is replaced. | ||
/* Public: returns true if this pointer points to a child of the | ||
@@ -380,11 +380,2 @@ * other pointer given. Returns true if both point to the same place. | ||
function contains(arr, value) { | ||
var i, len; | ||
for(i = 0, len = arr.length; i < len; i++) { | ||
if (arr[i] === value) return true; | ||
} | ||
return false; | ||
} | ||
// Check if a is deep equal to b (by the rules given in the | ||
@@ -419,3 +410,3 @@ // JSONPatch spec) | ||
} | ||
} | ||
} | ||
} | ||
@@ -435,3 +426,3 @@ for(key in b) { | ||
function validateOp(operation) { | ||
var key, i, required; | ||
var i, required; | ||
if (!operation.op) { | ||
@@ -456,3 +447,3 @@ throw new InvalidPatch('Operation missing!'); | ||
} | ||
function compileOperation(operation, mutate) { | ||
@@ -465,10 +456,2 @@ validateOp(operation); | ||
// Check that destination isn't a subset of source | ||
if (op == 'move') { | ||
if (path.subsetOf(from)) { | ||
throw new InvalidPatch('destination must not be a child of source'); | ||
} | ||
} | ||
switch (op) { | ||
@@ -488,2 +471,6 @@ case 'add': | ||
case 'move': | ||
// Check that destination isn't inside the source | ||
if (path.subsetOf(from)) { | ||
throw new InvalidPatch('destination must not be a child of source'); | ||
} | ||
return function (doc) { | ||
@@ -523,4 +510,3 @@ var value = from.get(doc); | ||
JSONPatch.prototype._compile = function (patch, mutate) { | ||
var i, n, key; | ||
var _this = this; | ||
var i, _this = this; | ||
this.compiledOps = []; | ||
@@ -541,7 +527,7 @@ | ||
/* Public: Apply the patch to a document and returns the patched | ||
* document. | ||
* document. | ||
* | ||
* doc - The document to which the patch should be applied. | ||
* | ||
* Returns the patched document | ||
* Returns the patched document | ||
*/ | ||
@@ -548,0 +534,0 @@ exports.JSONPatch.prototype.apply = function (doc) { |
{ | ||
"name": "jsonpatch", | ||
"version": "0.0.3", | ||
"description": "An implementation of JSON Patch and JSON Pointer IETF drafts", | ||
"version": "1.0.0-rc1", | ||
"description": "An implementation of JSON Patch and JSON Pointer IETF RFCs", | ||
"keywords": ["diff", "patch", "json", "jsonpatch", "jsonpointer"], | ||
@@ -14,6 +14,8 @@ "maintainers": "Thomas Parslow <tom@almostobsolete.net>", | ||
"expect.js": "0.2.0", | ||
"jshint": "0.3.0" | ||
"jshint": "0.3.0", | ||
"uglify-js": "2.3.5" | ||
}, | ||
"scripts": { | ||
"test": "mocha test/test.*.js && jshint lib" | ||
"test": "mocha test/test.*.js && jshint lib", | ||
"minify": "uglifyjs lib/jsonpatch.js -c --comments -o jsonpatch.min.js" | ||
}, | ||
@@ -27,9 +29,12 @@ "testling": { | ||
"ie": [ 6, 7, 8, 9, 10], | ||
"firefox": [ 13 ], | ||
"chrome": [ 20 ], | ||
"safari": [ 5.1 ], | ||
"opera": [ 12 ] | ||
"firefox": [ 19], | ||
"chrome": [ 25 ], | ||
"safari": [ 6.0 ], | ||
"opera": [ 12 ], | ||
"iphone": [ 6.0 ], | ||
"ipad": [ 6.0 ], | ||
"android-browser": [ 4.2 ] | ||
} | ||
}, | ||
"repository" : {"type": "git", "url": "git://github.com/dhamrafly/jsonpatch.js"}, | ||
"repository" : {"type": "git", "url": "git://github.com/dharmafly/jsonpatch.js"}, | ||
"directories" : { "lib" : "./lib/" , "doc" : "./doc/" }, | ||
@@ -36,0 +41,0 @@ "engines": { "node": ">=0.4.0" }, |
@@ -1,8 +0,5 @@ | ||
NEW: Now supports JSONPointer Draft 09 and JSONPatch Draft 10 (the latest as of 21/Jan/2013) | ||
JSONPatch | ||
========= | ||
An implementation of the [JSONPatch][#jsonpatch] (and [JSONPointer][#jsonpointer]) IETF drafts for Node.JS and the Browser (as a plain module or with AMD). | ||
An implementation of the [JSONPatch][#jsonpatch] and [JSONPointer][#jsonpointer] IETF RFCs that works in Node.JS and the Browser (as a plain module or with AMD). | ||
@@ -31,4 +28,6 @@ A [Dharmafly][#dharmafly] project written by [Thomas Parslow][#tom] <tom@almostobsolete.net> and released with the kind permission of [NetDev][#netdev]. | ||
And that's all you need for basic use, if the patch is invalid or won't apply then you'll get an error thrown. The original doc is NOT mutated so you can use it for other things afterwards. For more see the [docs][#site]. | ||
And that's all you need for basic use. If the patch is invalid or won't apply then you'll get an error thrown. The original doc is NOT mutated so you can use it for other things afterwards, mutating the document is supported via a flag if you need it. | ||
For more see the [docs][#site]. | ||
Is it any good? | ||
@@ -52,12 +51,11 @@ --------------- | ||
Is it finished? | ||
--------------- | ||
Probably, unless the spec changes again :) | ||
Are there tests? | ||
---------------- | ||
Yes, there are tests. It also passes JSHint. | ||
Yes, there are tests. It also passes JSHint. You can (and should) run the tests yourself by running this from the project directory: | ||
npm test | ||
Or you can open `test/runner.html` in a browser of your choice. | ||
We're using [Travis][#travis] and [Testling CI][#testling] to automatically run the tests on Node.JS and in a range of browsers every time a change is commited to this repository. The badges at the top of this readme display the current build status (which should always be passing). | ||
@@ -83,5 +81,5 @@ | ||
[#backbone]: http://documentcloud.github.com/backbone/ | ||
[#jsonpatch]: https://datatracker.ietf.org/doc/draft-ietf-appsawg-json-patch/ | ||
[#jsonpointer]: https://datatracker.ietf.org/doc/draft-ietf-appsawg-json-pointer/ | ||
[#jsonpatch]: http://tools.ietf.org/html/rfc6902 | ||
[#jsonpointer]: http://tools.ietf.org/html/rfc6901 | ||
[#travis]: http://travis-ci.org/dharmafly/jsonpatch.js | ||
[#testling]: http://ci.testling.com/dharmafly/jsonpatch.js | ||
[#testling]: http://ci.testling.com/dharmafly/jsonpatch.js |
@@ -55,259 +55,269 @@ // A wrapper round the tests from https://github.com/json-patch/json-patch-tests | ||
} | ||
add_tests('tests.json', [ | ||
{ "comment": "empty list, empty docs", | ||
"doc": {}, | ||
"patch": [], | ||
"expected": {} }, | ||
{ "comment": "empty list, empty docs", | ||
"doc": {}, | ||
"patch": [], | ||
"expected": {} }, | ||
{ "comment": "empty patch list", | ||
"doc": {"foo": 1}, | ||
"patch": [], | ||
"expected": {"foo": 1} }, | ||
{ "comment": "empty patch list", | ||
"doc": {"foo": 1}, | ||
"patch": [], | ||
"expected": {"foo": 1} }, | ||
{ "comment": "rearrangements ok?", | ||
"doc": {"foo": 1, "bar": 2}, | ||
"patch": [], | ||
"expected": {"bar":2, "foo": 1} }, | ||
{ "comment": "rearrangements OK?", | ||
"doc": {"foo": 1, "bar": 2}, | ||
"patch": [], | ||
"expected": {"bar":2, "foo": 1} }, | ||
{ "comment": "rearrangements ok? how about one level down ... array", | ||
"doc": [{"foo": 1, "bar": 2}], | ||
"patch": [], | ||
"expected": [{"bar":2, "foo": 1}] }, | ||
{ "comment": "rearrangements OK? How about one level down ... array", | ||
"doc": [{"foo": 1, "bar": 2}], | ||
"patch": [], | ||
"expected": [{"bar":2, "foo": 1}] }, | ||
{ "comment": "rearrangements ok? how about one level down...", | ||
"doc": {"foo":{"foo": 1, "bar": 2}}, | ||
"patch": [], | ||
"expected": {"foo":{"bar":2, "foo": 1}} }, | ||
{ "comment": "rearrangements OK? How about one level down...", | ||
"doc": {"foo":{"foo": 1, "bar": 2}}, | ||
"patch": [], | ||
"expected": {"foo":{"bar":2, "foo": 1}} }, | ||
{ "comment": "add replaces any existing field", | ||
"doc": {"foo": null}, | ||
"patch": [{"op": "add", "path": "/foo", "value":1}], | ||
"expected": {"foo": 1} }, | ||
{ "comment": "add replaces any existing field", | ||
"doc": {"foo": null}, | ||
"patch": [{"op": "add", "path": "/foo", "value":1}], | ||
"expected": {"foo": 1} }, | ||
{ "comment": "toplevel array", | ||
"doc": [], | ||
"patch": [{"op": "add", "path": "/0", "value": "foo"}], | ||
"expected": ["foo"] }, | ||
{ "comment": "toplevel array", | ||
"doc": [], | ||
"patch": [{"op": "add", "path": "/0", "value": "foo"}], | ||
"expected": ["foo"] }, | ||
{ "comment": "toplevel array, no change", | ||
"doc": ["foo"], | ||
"patch": [], | ||
"expected": ["foo"] }, | ||
{ "comment": "toplevel array, no change", | ||
"doc": ["foo"], | ||
"patch": [], | ||
"expected": ["foo"] }, | ||
{ "comment": "toplevel object, numeric string", | ||
"doc": {}, | ||
"patch": [{"op": "add", "path": "/foo", "value": "1"}], | ||
"expected": {"foo":"1"} }, | ||
{ "comment": "toplevel object, numeric string", | ||
"doc": {}, | ||
"patch": [{"op": "add", "path": "/foo", "value": "1"}], | ||
"expected": {"foo":"1"} }, | ||
{ "comment": "toplevel object, integer", | ||
"doc": {}, | ||
"patch": [{"op": "add", "path": "/foo", "value": 1}], | ||
"expected": {"foo":1} }, | ||
{ "comment": "toplevel object, integer", | ||
"doc": {}, | ||
"patch": [{"op": "add", "path": "/foo", "value": 1}], | ||
"expected": {"foo":1} }, | ||
{ "comment": "toplevel scalar values ok?", | ||
"doc": "foo", | ||
"patch": [{"op": "replace", "path": "", "value": "bar"}], | ||
"expected": "bar", | ||
"disabled": true }, | ||
{ "comment": "Toplevel scalar values OK?", | ||
"doc": "foo", | ||
"patch": [{"op": "replace", "path": "", "value": "bar"}], | ||
"expected": "bar", | ||
"disabled": true }, | ||
{ "comment": "add, / target", | ||
"doc": {}, | ||
"patch": [ {"op": "add", "path": "/", "value":1 } ], | ||
"expected": {"":1} }, | ||
{ "comment": "Add, / target", | ||
"doc": {}, | ||
"patch": [ {"op": "add", "path": "/", "value":1 } ], | ||
"expected": {"":1} }, | ||
{ "comment": "add composite value at top level", | ||
"doc": {"foo": 1}, | ||
"patch": [{"op": "add", "path": "/bar", "value": [1, 2]}], | ||
"expected": {"foo": 1, "bar": [1, 2]} }, | ||
{ "comment": "Add composite value at top level", | ||
"doc": {"foo": 1}, | ||
"patch": [{"op": "add", "path": "/bar", "value": [1, 2]}], | ||
"expected": {"foo": 1, "bar": [1, 2]} }, | ||
{ "comment": "add into composite value", | ||
"doc": {"foo": 1, "baz": [{"qux": "hello"}]}, | ||
"patch": [{"op": "add", "path": "/baz/0/foo", "value": "world"}], | ||
"expected": {"foo": 1, "baz": [{"qux": "hello", "foo": "world"}]} }, | ||
{ "comment": "Add into composite value", | ||
"doc": {"foo": 1, "baz": [{"qux": "hello"}]}, | ||
"patch": [{"op": "add", "path": "/baz/0/foo", "value": "world"}], | ||
"expected": {"foo": 1, "baz": [{"qux": "hello", "foo": "world"}]} }, | ||
{ "doc": {"bar": [1, 2]}, | ||
"patch": [{"op": "add", "path": "/bar/8", "value": "5"}], | ||
"error": "out of bounds (upper)" }, | ||
{ "doc": {"bar": [1, 2]}, | ||
"patch": [{"op": "add", "path": "/bar/8", "value": "5"}], | ||
"error": "Out of bounds (upper)" }, | ||
{ "doc": {"bar": [1, 2]}, | ||
"patch": [{"op": "add", "path": "/bar/-1", "value": "5"}], | ||
"error": "out of bounds (lower)" }, | ||
{ "doc": {"bar": [1, 2]}, | ||
"patch": [{"op": "add", "path": "/bar/-1", "value": "5"}], | ||
"error": "Out of bounds (lower)" }, | ||
{ "doc": {"foo": 1}, | ||
"patch": [{"op": "add", "path": "/bar", "value": true}], | ||
"expected": {"foo": 1, "bar": true} }, | ||
{ "doc": {"foo": 1}, | ||
"patch": [{"op": "add", "path": "/bar", "value": true}], | ||
"expected": {"foo": 1, "bar": true} }, | ||
{ "doc": {"foo": 1}, | ||
"patch": [{"op": "add", "path": "/bar", "value": false}], | ||
"expected": {"foo": 1, "bar": false} }, | ||
{ "doc": {"foo": 1}, | ||
"patch": [{"op": "add", "path": "/bar", "value": false}], | ||
"expected": {"foo": 1, "bar": false} }, | ||
{ "doc": {"foo": 1}, | ||
"patch": [{"op": "add", "path": "/bar", "value": null}], | ||
"expected": {"foo": 1, "bar": null} }, | ||
{ "doc": {"foo": 1}, | ||
"patch": [{"op": "add", "path": "/bar", "value": null}], | ||
"expected": {"foo": 1, "bar": null} }, | ||
{ "comment": "0 can be an array index or object element name", | ||
"doc": {"foo": 1}, | ||
"patch": [{"op": "add", "path": "/0", "value": "bar"}], | ||
"expected": {"foo": 1, "0": "bar" } }, | ||
{ "comment": "0 can be an array index or object element name", | ||
"doc": {"foo": 1}, | ||
"patch": [{"op": "add", "path": "/0", "value": "bar"}], | ||
"expected": {"foo": 1, "0": "bar" } }, | ||
{ "doc": ["foo"], | ||
"patch": [{"op": "add", "path": "/1", "value": "bar"}], | ||
"expected": ["foo", "bar"] }, | ||
{ "doc": ["foo"], | ||
"patch": [{"op": "add", "path": "/1", "value": "bar"}], | ||
"expected": ["foo", "bar"] }, | ||
{ "doc": ["foo", "sil"], | ||
"patch": [{"op": "add", "path": "/1", "value": "bar"}], | ||
"expected": ["foo", "bar", "sil"] }, | ||
{ "doc": ["foo", "sil"], | ||
"patch": [{"op": "add", "path": "/1", "value": "bar"}], | ||
"expected": ["foo", "bar", "sil"] }, | ||
{ "doc": ["foo", "sil"], | ||
"patch": [{"op": "add", "path": "/0", "value": "bar"}], | ||
"expected": ["bar", "foo", "sil"] }, | ||
{ "doc": ["foo", "sil"], | ||
"patch": [{"op": "add", "path": "/0", "value": "bar"}], | ||
"expected": ["bar", "foo", "sil"] }, | ||
{ "doc": ["foo", "sil"], | ||
"patch": [{"op":"add", "path": "/2", "value": "bar"}], | ||
"expected": ["foo", "sil", "bar"] }, | ||
{ "doc": ["foo", "sil"], | ||
"patch": [{"op":"add", "path": "/2", "value": "bar"}], | ||
"expected": ["foo", "sil", "bar"] }, | ||
{ "comment": "test against implementation-specific numeric parsing", | ||
"doc": {"1e0": "foo"}, | ||
"patch": [{"op": "test", "path": "/1e0", "value": "foo"}], | ||
"expected": {"1e0": "foo"} }, | ||
{ "comment": "test against implementation-specific numeric parsing", | ||
"doc": {"1e0": "foo"}, | ||
"patch": [{"op": "test", "path": "/1e0", "value": "foo"}], | ||
"expected": {"1e0": "foo"} }, | ||
{ "comment": "test with bad number should fail", | ||
"doc": ["foo", "bar"], | ||
"patch": [{"op": "test", "path": "/1e0", "value": "bar"}], | ||
"error": "test op shouldn't get array element 1" }, | ||
{ "comment": "test with bad number should fail", | ||
"doc": ["foo", "bar"], | ||
"patch": [{"op": "test", "path": "/1e0", "value": "bar"}], | ||
"error": "test op shouldn't get array element 1" }, | ||
{ "doc": ["foo", "sil"], | ||
"patch": [{"op": "add", "path": "/bar", "value": 42}], | ||
"error": "object operation on array target" }, | ||
{ "doc": ["foo", "sil"], | ||
"patch": [{"op": "add", "path": "/bar", "value": 42}], | ||
"error": "Object operation on array target" }, | ||
{ "doc": ["foo", "sil"], | ||
"patch": [{"op": "add", "path": "/1", "value": ["bar", "baz"]}], | ||
"expected": ["foo", ["bar", "baz"], "sil"], | ||
"comment": "value in array add not flattened" }, | ||
{ "doc": ["foo", "sil"], | ||
"patch": [{"op": "add", "path": "/1", "value": ["bar", "baz"]}], | ||
"expected": ["foo", ["bar", "baz"], "sil"], | ||
"comment": "value in array add not flattened" }, | ||
{ "doc": {"foo": 1, "bar": [1, 2, 3, 4]}, | ||
"patch": [{"op": "remove", "path": "/bar"}], | ||
"expected": {"foo": 1} }, | ||
{ "doc": {"foo": 1, "bar": [1, 2, 3, 4]}, | ||
"patch": [{"op": "remove", "path": "/bar"}], | ||
"expected": {"foo": 1} }, | ||
{ "doc": {"foo": 1, "baz": [{"qux": "hello"}]}, | ||
"patch": [{"op": "remove", "path": "/baz/0/qux"}], | ||
"expected": {"foo": 1, "baz": [{}]} }, | ||
{ "doc": {"foo": 1, "baz": [{"qux": "hello"}]}, | ||
"patch": [{"op": "remove", "path": "/baz/0/qux"}], | ||
"expected": {"foo": 1, "baz": [{}]} }, | ||
{ "doc": {"foo": 1, "baz": [{"qux": "hello"}]}, | ||
"patch": [{"op": "replace", "path": "/foo", "value": [1, 2, 3, 4]}], | ||
"expected": {"foo": [1, 2, 3, 4], "baz": [{"qux": "hello"}]} }, | ||
{ "doc": {"foo": 1, "baz": [{"qux": "hello"}]}, | ||
"patch": [{"op": "replace", "path": "/foo", "value": [1, 2, 3, 4]}], | ||
"expected": {"foo": [1, 2, 3, 4], "baz": [{"qux": "hello"}]} }, | ||
{ "doc": {"foo": [1, 2, 3, 4], "baz": [{"qux": "hello"}]}, | ||
"patch": [{"op": "replace", "path": "/baz/0/qux", "value": "world"}], | ||
"expected": {"foo": [1, 2, 3, 4], "baz": [{"qux": "world"}]} }, | ||
{ "doc": {"foo": [1, 2, 3, 4], "baz": [{"qux": "hello"}]}, | ||
"patch": [{"op": "replace", "path": "/baz/0/qux", "value": "world"}], | ||
"expected": {"foo": [1, 2, 3, 4], "baz": [{"qux": "world"}]} }, | ||
{ "doc": ["foo"], | ||
"patch": [{"op": "replace", "path": "/0", "value": "bar"}], | ||
"expected": ["bar"] }, | ||
{ "doc": ["foo"], | ||
"patch": [{"op": "replace", "path": "/0", "value": "bar"}], | ||
"expected": ["bar"] }, | ||
{ "doc": [""], | ||
"patch": [{"op": "replace", "path": "/0", "value": 0}], | ||
"expected": [0] }, | ||
{ "doc": [""], | ||
"patch": [{"op": "replace", "path": "/0", "value": 0}], | ||
"expected": [0] }, | ||
{ "doc": [""], | ||
"patch": [{"op": "replace", "path": "/0", "value": true}], | ||
"expected": [true] }, | ||
{ "doc": [""], | ||
"patch": [{"op": "replace", "path": "/0", "value": true}], | ||
"expected": [true] }, | ||
{ "doc": [""], | ||
"patch": [{"op": "replace", "path": "/0", "value": false}], | ||
"expected": [false] }, | ||
{ "doc": [""], | ||
"patch": [{"op": "replace", "path": "/0", "value": false}], | ||
"expected": [false] }, | ||
{ "doc": [""], | ||
"patch": [{"op": "replace", "path": "/0", "value": null}], | ||
"expected": [null] }, | ||
{ "doc": [""], | ||
"patch": [{"op": "replace", "path": "/0", "value": null}], | ||
"expected": [null] }, | ||
{ "doc": ["foo", "sil"], | ||
"patch": [{"op": "replace", "path": "/1", "value": ["bar", "baz"]}], | ||
"expected": ["foo", ["bar", "baz"]], | ||
"comment": "value in array replace not flattened" }, | ||
{ "doc": ["foo", "sil"], | ||
"patch": [{"op": "replace", "path": "/1", "value": ["bar", "baz"]}], | ||
"expected": ["foo", ["bar", "baz"]], | ||
"comment": "value in array replace not flattened" }, | ||
{ "comment": "spurious patch properties", | ||
"doc": {"foo": 1}, | ||
"patch": [{"op": "test", "path": "/foo", "value": 1, "spurious": 1}], | ||
"expected": {"foo": 1} }, | ||
{ "comment": "spurious patch properties", | ||
"doc": {"foo": 1}, | ||
"patch": [{"op": "test", "path": "/foo", "value": 1, "spurious": 1}], | ||
"expected": {"foo": 1} }, | ||
{ "doc": {"foo": null}, | ||
"patch": [{"op": "test", "path": "/foo", "value": null}], | ||
"comment": "null value should still be valid obj property" }, | ||
{ "doc": {"foo": null}, | ||
"patch": [{"op": "test", "path": "/foo", "value": null}], | ||
"comment": "null value should still be valid obj property" }, | ||
{ "doc": {"foo": {"foo": 1, "bar": 2}}, | ||
"patch": [{"op": "test", "path": "/foo", "value": {"bar": 2, "foo": 1}}], | ||
"comment": "test should pass despite rearrangement" }, | ||
{ "doc": {"foo": {"foo": 1, "bar": 2}}, | ||
"patch": [{"op": "test", "path": "/foo", "value": {"bar": 2, "foo": 1}}], | ||
"comment": "test should pass despite rearrangement" }, | ||
{ "doc": {"foo": [{"foo": 1, "bar": 2}]}, | ||
"patch": [{"op": "test", "path": "/foo", "value": [{"bar": 2, "foo": 1}]}], | ||
"comment": "test should pass despite (nested) rearrangement" }, | ||
{ "doc": {"foo": [{"foo": 1, "bar": 2}]}, | ||
"patch": [{"op": "test", "path": "/foo", "value": [{"bar": 2, "foo": 1}]}], | ||
"comment": "test should pass despite (nested) rearrangement" }, | ||
{ "doc": {"foo": {"bar": [1, 2, 5, 4]}}, | ||
"patch": [{"op": "test", "path": "/foo", "value": {"bar": [1, 2, 5, 4]}}], | ||
"comment": "test should pass - no error" }, | ||
{ "doc": {"foo": {"bar": [1, 2, 5, 4]}}, | ||
"patch": [{"op": "test", "path": "/foo", "value": {"bar": [1, 2, 5, 4]}}], | ||
"comment": "test should pass - no error" }, | ||
{ "doc": {"foo": {"bar": [1, 2, 5, 4]}}, | ||
"patch": [{"op": "test", "path": "/foo", "value": [1, 2]}], | ||
"error": "test op should fail" }, | ||
{ "doc": {"foo": {"bar": [1, 2, 5, 4]}}, | ||
"patch": [{"op": "test", "path": "/foo", "value": [1, 2]}], | ||
"error": "test op should fail" }, | ||
{ "comment": "json-pointer tests" }, | ||
{ "comment": "json-pointer tests" }, | ||
{ "comment": "whole document", | ||
"doc": { "foo": 1 }, | ||
"patch": [{"op": "test", "path": "", "value": {"foo": 1}}], | ||
"disabled": true }, | ||
{ "comment": "Whole document", | ||
"doc": { "foo": 1 }, | ||
"patch": [{"op": "test", "path": "", "value": {"foo": 1}}], | ||
"disabled": true }, | ||
{ "comment": "empty-string element", | ||
"doc": { "": 1 }, | ||
"patch": [{"op": "test", "path": "/", "value": 1}] }, | ||
{ "comment": "Empty-string element", | ||
"doc": { "": 1 }, | ||
"patch": [{"op": "test", "path": "/", "value": 1}] }, | ||
{ "doc": { | ||
"foo": ["bar", "baz"], | ||
"": 0, | ||
"a/b": 1, | ||
"c%d": 2, | ||
"e^f": 3, | ||
"g|h": 4, | ||
"i\\j": 5, | ||
"k\"l": 6, | ||
" ": 7, | ||
"m~n": 8 | ||
}, | ||
"patch": [{"op": "test", "path": "/foo", "value": ["bar", "baz"]}, | ||
{"op": "test", "path": "/foo/0", "value": "bar"}, | ||
{"op": "test", "path": "/", "value": 0}, | ||
{"op": "test", "path": "/a~1b", "value": 1}, | ||
{"op": "test", "path": "/c%d", "value": 2}, | ||
{"op": "test", "path": "/e^f", "value": 3}, | ||
{"op": "test", "path": "/g|h", "value": 4}, | ||
{"op": "test", "path": "/i\\j", "value": 5}, | ||
{"op": "test", "path": "/k\"l", "value": 6}, | ||
{"op": "test", "path": "/ ", "value": 7}, | ||
{"op": "test", "path": "/m~0n", "value": 8}] }, | ||
{ "doc": { | ||
"foo": ["bar", "baz"], | ||
"": 0, | ||
"a/b": 1, | ||
"c%d": 2, | ||
"e^f": 3, | ||
"g|h": 4, | ||
"i\\j": 5, | ||
"k\"l": 6, | ||
" ": 7, | ||
"m~n": 8 | ||
}, | ||
"patch": [{"op": "test", "path": "/foo", "value": ["bar", "baz"]}, | ||
{"op": "test", "path": "/foo/0", "value": "bar"}, | ||
{"op": "test", "path": "/", "value": 0}, | ||
{"op": "test", "path": "/a~1b", "value": 1}, | ||
{"op": "test", "path": "/c%d", "value": 2}, | ||
{"op": "test", "path": "/e^f", "value": 3}, | ||
{"op": "test", "path": "/g|h", "value": 4}, | ||
{"op": "test", "path": "/i\\j", "value": 5}, | ||
{"op": "test", "path": "/k\"l", "value": 6}, | ||
{"op": "test", "path": "/ ", "value": 7}, | ||
{"op": "test", "path": "/m~0n", "value": 8}] }, | ||
{ "comment": "move to same location has no effect", | ||
"doc": {"foo": 1}, | ||
"patch": [{"op": "move", "from": "/foo", "path": "/foo"}], | ||
"expected": {"foo": 1} }, | ||
{ "comment": "Move to same location has no effect", | ||
"doc": {"foo": 1}, | ||
"patch": [{"op": "move", "from": "/foo", "path": "/foo"}], | ||
"expected": {"foo": 1} }, | ||
{ "doc": {"foo": 1, "baz": [{"qux": "hello"}]}, | ||
"patch": [{"op": "move", "from": "/foo", "path": "/bar"}], | ||
"expected": {"baz": [{"qux": "hello"}], "bar": 1} }, | ||
{ "doc": {"foo": 1, "baz": [{"qux": "hello"}]}, | ||
"patch": [{"op": "move", "from": "/foo", "path": "/bar"}], | ||
"expected": {"baz": [{"qux": "hello"}], "bar": 1} }, | ||
{ "doc": {"baz": [{"qux": "hello"}], "bar": 1}, | ||
"patch": [{"op": "move", "from": "/baz/0/qux", "path": "/baz/1"}], | ||
"expected": {"baz": [{}, "hello"], "bar": 1} }, | ||
{ "doc": {"baz": [{"qux": "hello"}], "bar": 1}, | ||
"patch": [{"op": "move", "from": "/baz/0/qux", "path": "/baz/1"}], | ||
"expected": {"baz": [{}, "hello"], "bar": 1} }, | ||
{ "doc": {"baz": [{"qux": "hello"}], "bar": 1}, | ||
"patch": [{"op": "copy", "from": "/baz/0", "path": "/boo"}], | ||
"expected": {"baz":[{"qux":"hello"}],"bar":1,"boo":{"qux":"hello"}} }, | ||
{ "doc": {"baz": [{"qux": "hello"}], "bar": 1}, | ||
"patch": [{"op": "copy", "from": "/baz/0", "path": "/boo"}], | ||
"expected": {"baz":[{"qux":"hello"}],"bar":1,"boo":{"qux":"hello"}} }, | ||
{ "comment": "replacing the root of the document is possible with add", | ||
"doc": {"foo": "bar"}, | ||
"patch": [{"op": "add", "path": "", "value": {"baz": "qux"}}], | ||
"expected": {"baz":"qux"}}, | ||
{ "comment": "replacing the root of the document is possible with add", | ||
"doc": {"foo": "bar"}, | ||
"patch": [{"op": "add", "path": "", "value": {"baz": "qux"}}], | ||
"expected": {"baz":"qux"}}, | ||
{ "comment": "tests complete" } | ||
{ "comment": "Adding to \"/-\" adds to the end of the array", | ||
"doc": [ 1, 2 ], | ||
"patch": [ { "op": "add", "path": "/-", "value": { "foo": [ "bar", "baz" ] } } ], | ||
"expected": [ 1, 2, { "foo": [ "bar", "baz" ] } ]}, | ||
{ "comment": "Adding to \"/-\" adds to the end of the array, even n levels down", | ||
"doc": [ 1, 2, [ 3, [ 4, 5 ] ] ], | ||
"patch": [ { "op": "add", "path": "/2/1/-", "value": { "foo": [ "bar", "baz" ] } } ], | ||
"expected": [ 1, 2, [ 3, [ 4, 5, { "foo": [ "bar", "baz" ] } ] ] ]}, | ||
{ "comment": "tests complete" } | ||
]); | ||
@@ -321,3 +331,3 @@ | ||
"error": | ||
"path /a does not exist -- missing objects are not created recursively" | ||
"path /a does not exist -- missing objects are not created recursively" | ||
}, | ||
@@ -328,11 +338,11 @@ | ||
"doc": { | ||
"foo": "bar" | ||
}, | ||
"foo": "bar" | ||
}, | ||
"patch": [ | ||
{ "op": "add", "path": "/baz", "value": "qux" } | ||
], | ||
{ "op": "add", "path": "/baz", "value": "qux" } | ||
], | ||
"expected": { | ||
"baz": "qux", | ||
"foo": "bar" | ||
} | ||
"baz": "qux", | ||
"foo": "bar" | ||
} | ||
}, | ||
@@ -343,10 +353,10 @@ | ||
"doc": { | ||
"foo": [ "bar", "baz" ] | ||
}, | ||
"foo": [ "bar", "baz" ] | ||
}, | ||
"patch": [ | ||
{ "op": "add", "path": "/foo/1", "value": "qux" } | ||
], | ||
{ "op": "add", "path": "/foo/1", "value": "qux" } | ||
], | ||
"expected": { | ||
"foo": [ "bar", "qux", "baz" ] | ||
} | ||
"foo": [ "bar", "qux", "baz" ] | ||
} | ||
}, | ||
@@ -357,11 +367,11 @@ | ||
"doc": { | ||
"baz": "qux", | ||
"foo": "bar" | ||
}, | ||
"baz": "qux", | ||
"foo": "bar" | ||
}, | ||
"patch": [ | ||
{ "op": "remove", "path": "/baz" } | ||
], | ||
{ "op": "remove", "path": "/baz" } | ||
], | ||
"expected": { | ||
"foo": "bar" | ||
} | ||
"foo": "bar" | ||
} | ||
}, | ||
@@ -372,10 +382,10 @@ | ||
"doc": { | ||
"foo": [ "bar", "qux", "baz" ] | ||
}, | ||
"foo": [ "bar", "qux", "baz" ] | ||
}, | ||
"patch": [ | ||
{ "op": "remove", "path": "/foo/1" } | ||
], | ||
{ "op": "remove", "path": "/foo/1" } | ||
], | ||
"expected": { | ||
"foo": [ "bar", "baz" ] | ||
} | ||
"foo": [ "bar", "baz" ] | ||
} | ||
}, | ||
@@ -386,12 +396,12 @@ | ||
"doc": { | ||
"baz": "qux", | ||
"foo": "bar" | ||
}, | ||
"baz": "qux", | ||
"foo": "bar" | ||
}, | ||
"patch": [ | ||
{ "op": "replace", "path": "/baz", "value": "boo" } | ||
], | ||
{ "op": "replace", "path": "/baz", "value": "boo" } | ||
], | ||
"expected": { | ||
"baz": "boo", | ||
"foo": "bar" | ||
} | ||
"baz": "boo", | ||
"foo": "bar" | ||
} | ||
}, | ||
@@ -402,23 +412,23 @@ | ||
"doc": { | ||
"foo": { | ||
"bar": "baz", | ||
"waldo": "fred" | ||
}, | ||
"qux": { | ||
"corge": "grault" | ||
} | ||
}, | ||
"foo": { | ||
"bar": "baz", | ||
"waldo": "fred" | ||
}, | ||
"qux": { | ||
"corge": "grault" | ||
} | ||
}, | ||
"patch": [ | ||
{ "op": "move", "from": "/foo/waldo", "path": "/qux/thud" } | ||
], | ||
{ "op": "move", "from": "/foo/waldo", "path": "/qux/thud" } | ||
], | ||
"expected": { | ||
"foo": { | ||
"bar": "baz" | ||
"foo": { | ||
"bar": "baz" | ||
}, | ||
"qux": { | ||
"corge": "grault", | ||
"thud": "fred" | ||
} | ||
} | ||
}, | ||
"qux": { | ||
"corge": "grault", | ||
"thud": "fred" | ||
} | ||
} | ||
}, | ||
@@ -428,10 +438,10 @@ { | ||
"doc": { | ||
"foo": [ "all", "grass", "cows", "eat" ] | ||
}, | ||
"foo": [ "all", "grass", "cows", "eat" ] | ||
}, | ||
"patch": [ | ||
{ "op": "move", "from": "/foo/1", "path": "/foo/3" } | ||
], | ||
{ "op": "move", "from": "/foo/1", "path": "/foo/3" } | ||
], | ||
"expected": { | ||
"foo": [ "all", "cows", "eat", "grass" ] | ||
} | ||
"foo": [ "all", "cows", "eat", "grass" ] | ||
} | ||
@@ -443,12 +453,12 @@ }, | ||
"doc": { | ||
"baz": "qux", | ||
"foo": [ "a", 2, "c" ] | ||
}, | ||
"baz": "qux", | ||
"foo": [ "a", 2, "c" ] | ||
}, | ||
"patch": [ | ||
{ "op": "test", "path": "/baz", "value": "qux" }, | ||
{ "op": "test", "path": "/foo/1", "value": 2 } | ||
], | ||
{ "op": "test", "path": "/baz", "value": "qux" }, | ||
{ "op": "test", "path": "/foo/1", "value": 2 } | ||
], | ||
"expected": { | ||
"baz": "qux", | ||
"foo": [ "a", 2, "c" ] | ||
"baz": "qux", | ||
"foo": [ "a", 2, "c" ] | ||
} | ||
@@ -460,7 +470,7 @@ }, | ||
"doc": { | ||
"baz": "qux" | ||
}, | ||
"baz": "qux" | ||
}, | ||
"patch": [ | ||
{ "op": "test", "path": "/baz", "value": "bar" } | ||
], | ||
{ "op": "test", "path": "/baz", "value": "bar" } | ||
], | ||
"error": "string not equivalent" | ||
@@ -472,14 +482,14 @@ }, | ||
"doc": { | ||
"foo": "bar" | ||
}, | ||
"foo": "bar" | ||
}, | ||
"patch": [ | ||
{ "op": "add", "path": "/child", "value": { "grandchild": { } } } | ||
], | ||
{ "op": "add", "path": "/child", "value": { "grandchild": { } } } | ||
], | ||
"expected": { | ||
"foo": "bar", | ||
"child": { | ||
"grandchild": { | ||
"foo": "bar", | ||
"child": { | ||
"grandchild": { | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
@@ -490,32 +500,32 @@ | ||
"doc": { | ||
"foo":"bar" | ||
}, | ||
"foo":"bar" | ||
}, | ||
"patch": [ | ||
{ "op": "add", "path": "/baz", "value": "qux", "xyz": 123 } | ||
], | ||
{ "op": "add", "path": "/baz", "value": "qux", "xyz": 123 } | ||
], | ||
"expected": { | ||
"foo":"bar", | ||
"baz":"qux" | ||
} | ||
"foo":"bar", | ||
"baz":"qux" | ||
} | ||
}, | ||
{ | ||
{ | ||
"comment": "A.12. Adding to a Non-existant Target", | ||
"doc": { | ||
"foo": "bar" | ||
}, | ||
"foo": "bar" | ||
}, | ||
"patch": [ | ||
{ "op": "add", "path": "/baz/bat", "value": "qux" } | ||
], | ||
{ "op": "add", "path": "/baz/bat", "value": "qux" } | ||
], | ||
"error": "add to a non-existant target" | ||
}, | ||
{ | ||
{ | ||
"comment": "A.13 Invalid JSON Patch Document", | ||
"doc": { | ||
"foo": "bar" | ||
"foo": "bar" | ||
}, | ||
"patch": [ | ||
{ "op": "add", "path": "/baz", "value": "qux", "op": "remove" } | ||
], | ||
{ "op": "add", "path": "/baz", "value": "qux", "op": "remove" } | ||
], | ||
"error": "operation has two 'op' members" | ||
@@ -527,9 +537,9 @@ }, | ||
"doc": { | ||
"/": 9, | ||
"~1": 10 | ||
"/": 9, | ||
"~1": 10 | ||
}, | ||
"patch": [{"op": "test", "path": "/~01", "value": 10}], | ||
"expected": { | ||
"/": 9, | ||
"~1": 10 | ||
"/": 9, | ||
"~1": 10 | ||
} | ||
@@ -541,4 +551,4 @@ }, | ||
"doc": { | ||
"/": 9, | ||
"~1": 10 | ||
"/": 9, | ||
"~1": 10 | ||
}, | ||
@@ -552,3 +562,3 @@ "patch": [{"op": "test", "path": "/~01", "value": "10"}], | ||
"doc": { | ||
"foo": ["bar"] | ||
"foo": ["bar"] | ||
}, | ||
@@ -555,0 +565,0 @@ "patch": [{ "op": "add", "path": "/foo/-", "value": ["abc", "def"] }], |
@@ -40,3 +40,3 @@ if ('function' === typeof require) { | ||
}); | ||
it('should add an element to list, pushing up the remaing values', function () { | ||
@@ -60,3 +60,3 @@ example = add('/foo/anArray/1',example,'test'); | ||
}); | ||
it('should fail if adding to an array would create a sparse array', function () { | ||
@@ -201,3 +201,3 @@ expect(function () { | ||
// Don't run this test on browsers without the JSON object | ||
// only run this test on browsers with the JSON object | ||
if (typeof JSON === 'object') { | ||
@@ -231,27 +231,32 @@ it('should not mutate the source document', function () { | ||
it('should mutate the document if the mutate flag is passed in', function () { | ||
var doc = { | ||
"foo": { | ||
"anArray": [ | ||
{ "prop": 44 }, | ||
"second", | ||
"third" | ||
], | ||
"another prop": { | ||
"baz": "A string" | ||
if (typeof JSON === 'object') { | ||
it('should mutate the document if the mutate flag is true', function () { | ||
var doc = { | ||
"foo": { | ||
"anArray": [ | ||
{ "prop": 44 }, | ||
"second", | ||
"third" | ||
], | ||
"another prop": { | ||
"baz": "A string" | ||
} | ||
} | ||
} | ||
}; | ||
var patch = [ | ||
{"op": "remove", "path": "/foo/another prop/baz"}, | ||
{"op": "add", "path": "/foo/new", "value": "hello"}, | ||
{"op": "move", "from": "/foo/new", "path": "/newnew"}, | ||
{"op": "copy", "from": "/foo/anArray/1", "path": "/foo/anArray/-"}, | ||
{"op": "test", "path": "/foo/anArray/3", "value": "second"} | ||
]; | ||
patch = new jsonpatch.JSONPatch(patch, true); // mutate = true | ||
var patched = patch.apply(doc); | ||
// Check that the doc has not been mutated | ||
expect(patched).eql(doc) | ||
}); | ||
}; | ||
var json = JSON.stringify(doc); | ||
var patch = [ | ||
{"op": "remove", "path": "/foo/another prop/baz"}, | ||
{"op": "add", "path": "/foo/new", "value": "hello"}, | ||
{"op": "move", "from": "/foo/new", "path": "/newnew"}, | ||
{"op": "copy", "from": "/foo/anArray/1", "path": "/foo/anArray/-"}, | ||
{"op": "test", "path": "/foo/anArray/3", "value": "second"} | ||
]; | ||
patch = new jsonpatch.JSONPatch(patch, true); // mutate = true | ||
var patched = patch.apply(doc); | ||
// Check that the doc has been mutated | ||
expect(JSON.stringify(doc)).not.equal(json) | ||
// Check that it returned a reference to the original doc | ||
expect(patched).eql(doc) | ||
}); | ||
} | ||
@@ -258,0 +263,0 @@ describe('.apply()', function () { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
234290
29
7266
4
83