Browse files

Update to draft-ietf-appsawg-json-patch-08

  • Loading branch information...
1 parent 73a39dd commit 12f9ea3a7b3aaa2b94fac746763bd7715863ac09 @bruth bruth committed Dec 17, 2012
Showing with 42 additions and 42 deletions.
  1. +5 −5 README.md
  2. +15 −15 jsonpatch.coffee
  3. +14 −14 jsonpatch.js
  4. +8 −8 test.js
View
10 README.md
@@ -1,7 +1,7 @@
# jsonpatch.js
Library to apply JSON Patches in JavaScript
-http://tools.ietf.org/html/draft-ietf-appsawg-json-patch-06
+http://tools.ietf.org/html/draft-ietf-appsawg-json-patch-08
jsonpatch.js works as in the browser as a script, as a Node module and as an
AMD module.
@@ -81,20 +81,20 @@ jsonpatch.apply({foo: [{bar: 'baz'}]}, [{op: 'replace', path: '/foo/0/bar', valu
### Move
-Patch syntax: `{op: 'move', path: <path>, to: <path>}`
+Patch syntax: `{op: 'move', from: <path>, path: <path>}`
```javascript
// Move property, result {bar: [1, 2, 3]}
-jsonpatch.apply({foo: [1, 2, 3]}, [{op: 'move', path: '/foo', to: '/bar'}]);
+jsonpatch.apply({foo: [1, 2, 3]}, [{op: 'move', from: '/foo', path: '/bar'}]);
```
### Copy
-Patch syntax: `{op: 'copy', path: <path>, to: <path>}`
+Patch syntax: `{op: 'copy', from: <path>, path: <path>}`
```javascript
// Copy property, result {foo: [1, 2, 3], bar: 2}
-jsonpatch.apply({foo: [1, 2, 3]}, [{op: 'copy', path: '/foo/1', to: '/bar'}]);
+jsonpatch.apply({foo: [1, 2, 3]}, [{op: 'copy', from: '/foo/1', path: '/bar'}]);
```
### Test
View
30 jsonpatch.coffee
@@ -1,4 +1,4 @@
-# jsonpatch.js 0.3.1
+# jsonpatch.js 0.3.2
# (c) 2011-2012 Byron Ruth
# jsonpatch may be freely distributed under the BSD license
@@ -252,29 +252,29 @@
class MovePatch extends JSONPatch
initialize: (patch) ->
- @to = new JSONPointer(patch.to)
- len = @path.steps.length
+ @from = new JSONPointer(patch.from)
+ len = @from.steps.length
within = true
for i in [0..len]
- if @path.steps[i] isnt @to.steps[i]
+ if @from.steps[i] isnt @path.steps[i]
within = false
break
if within
- if @to.steps.length isnt len
+ if @path.steps.length isnt len
throw new InvalidPatchError("'to' member cannot be a descendent of 'path'")
- if @path.accessor is @to.accessor
+ if @from.accessor is @path.accessor
# The path and to pointers reference the same location,
# therefore apply can be a no-op
@apply = ->
validate: (patch) ->
- if 'to' not of patch then throw new InvalidPatchError()
+ if 'from' not of patch then throw new InvalidPatchError()
apply: (document) ->
- reference = @path.getReference(document)
- accessor = @path.accessor
+ reference = @from.getReference(document)
+ accessor = @from.accessor
if isArray(reference)
accessor = parseInt(accessor, 10)
@@ -287,8 +287,8 @@
value = reference[accessor]
delete reference[accessor]
- reference = @to.getReference(document)
- accessor = @to.accessor
+ reference = @path.getReference(document)
+ accessor = @path.accessor
# Add to object
if isArray(reference)
@@ -305,8 +305,8 @@
class CopyPatch extends MovePatch
apply: (document) ->
- reference = @path.getReference(document)
- accessor = @path.accessor
+ reference = @from.getReference(document)
+ accessor = @from.accessor
if isArray(reference)
accessor = parseInt(accessor, 10)
@@ -318,8 +318,8 @@
throw new PatchConflictError("Value at #{accessor} does not exist")
value = reference[accessor]
- reference = @to.getReference(document)
- accessor = @to.accessor
+ reference = @path.getReference(document)
+ accessor = @path.accessor
# Add to object
if isArray(reference)
View
28 jsonpatch.js
@@ -358,35 +358,35 @@
MovePatch.prototype.initialize = function(patch) {
var i, len, within, _i;
- this.to = new JSONPointer(patch.to);
- len = this.path.steps.length;
+ this.from = new JSONPointer(patch.from);
+ len = this.from.steps.length;
within = true;
for (i = _i = 0; 0 <= len ? _i <= len : _i >= len; i = 0 <= len ? ++_i : --_i) {
- if (this.path.steps[i] !== this.to.steps[i]) {
+ if (this.from.steps[i] !== this.path.steps[i]) {
within = false;
break;
}
}
if (within) {
- if (this.to.steps.length !== len) {
+ if (this.path.steps.length !== len) {
throw new InvalidPatchError("'to' member cannot be a descendent of 'path'");
}
- if (this.path.accessor === this.to.accessor) {
+ if (this.from.accessor === this.path.accessor) {
return this.apply = function() {};
}
}
};
MovePatch.prototype.validate = function(patch) {
- if (!('to' in patch)) {
+ if (!('from' in patch)) {
throw new InvalidPatchError();
}
};
MovePatch.prototype.apply = function(document) {
var accessor, reference, value;
- reference = this.path.getReference(document);
- accessor = this.path.accessor;
+ reference = this.from.getReference(document);
+ accessor = this.from.accessor;
if (isArray(reference)) {
accessor = parseInt(accessor, 10);
if (!(accessor in reference)) {
@@ -400,8 +400,8 @@
value = reference[accessor];
delete reference[accessor];
}
- reference = this.to.getReference(document);
- accessor = this.to.accessor;
+ reference = this.path.getReference(document);
+ accessor = this.path.accessor;
if (isArray(reference)) {
accessor = parseInt(accessor, 10);
if (accessor < 0 || accessor > reference.length) {
@@ -429,8 +429,8 @@
CopyPatch.prototype.apply = function(document) {
var accessor, reference, value;
- reference = this.path.getReference(document);
- accessor = this.path.accessor;
+ reference = this.from.getReference(document);
+ accessor = this.from.accessor;
if (isArray(reference)) {
accessor = parseInt(accessor, 10);
if (!(accessor in reference)) {
@@ -443,8 +443,8 @@
}
value = reference[accessor];
}
- reference = this.to.getReference(document);
- accessor = this.to.accessor;
+ reference = this.path.getReference(document);
+ accessor = this.path.accessor;
if (isArray(reference)) {
accessor = parseInt(accessor, 10);
if (accessor < 0 || accessor > reference.length) {
View
16 test.js
@@ -77,21 +77,21 @@ test('test', function() {
test('move', function() {
obj = {foo: 1, baz: [{qux: 'hello'}]};
- jsonpatch.apply(obj, [{op: 'move', path: '/foo', to: '/bar'}]);
+ jsonpatch.apply(obj, [{op: 'move', from: '/foo', path: '/bar'}]);
deepEqual(obj, {baz: [{qux: 'hello'}], bar: 1});
- jsonpatch.apply(obj, [{op: 'move', path: '/baz/0/qux', to: '/baz/1'}]);
+ jsonpatch.apply(obj, [{op: 'move', from: '/baz/0/qux', path: '/baz/1'}]);
deepEqual(obj, {baz: [{}, 'hello'], bar: 1});
});
test('copy', function() {
obj = {foo: 1, baz: [{qux: 'hello'}]};
- jsonpatch.apply(obj, [{op: 'copy', path: '/foo', to: '/bar'}]);
+ jsonpatch.apply(obj, [{op: 'copy', from: '/foo', path: '/bar'}]);
deepEqual(obj, {foo: 1, baz: [{qux: 'hello'}], bar: 1});
- jsonpatch.apply(obj, [{op: 'copy', path: '/baz/0/qux', to: '/baz/1'}]);
+ jsonpatch.apply(obj, [{op: 'copy', from: '/baz/0/qux', path: '/baz/1'}]);
deepEqual(obj, {foo: 1, baz: [{qux: 'hello'}, 'hello'], bar: 1});
});
@@ -115,12 +115,12 @@ JSLitmus.test('Replace Operation', function() {
JSLitmus.test('Move Operation', function() {
obj = {foo: 1, baz: [{qux: 'hello'}], bar: [1, 2, 3, 4]};
- jsonpatch.apply(obj, [{op: 'move', path: '/baz/0', to: '/bar/0'}]);
+ jsonpatch.apply(obj, [{op: 'move', from: '/baz/0', path: '/bar/0'}]);
});
JSLitmus.test('Copy Operation', function() {
obj = {foo: 1, baz: [{qux: 'hello'}], bar: [1, 2, 3, 4]};
- jsonpatch.apply(obj, [{op: 'copy', path: '/baz/0', to: '/bar/0'}]);
+ jsonpatch.apply(obj, [{op: 'copy', from: '/baz/0', path: '/bar/0'}]);
});
@@ -147,13 +147,13 @@ JSLitmus.test('Compiled Replace Operation', function() {
replaceCompiled(obj);
});
-var moveCompiled = jsonpatch.compile([{op: 'move', path: '/baz/0', to: '/bar/0'}]);
+var moveCompiled = jsonpatch.compile([{op: 'move', from: '/baz/0', path: '/bar/0'}]);
JSLitmus.test('Compiled Move Operation', function() {
obj = {foo: 1, baz: [{qux: 'hello'}], bar: [1, 2, 3, 4]};
moveCompiled(obj);
});
-var copyCompiled = jsonpatch.compile([{op: 'copy', path: '/baz/0', to: '/bar/0'}]);
+var copyCompiled = jsonpatch.compile([{op: 'copy', from: '/baz/0', path: '/bar/0'}]);
JSLitmus.test('Compiled Copy Operation', function() {
obj = {foo: 1, baz: [{qux: 'hello'}], bar: [1, 2, 3, 4]};
copyCompiled(obj);

0 comments on commit 12f9ea3

Please sign in to comment.