Skip to content

Commit

Permalink
Update to draft-ietf-appsawg-json-patch-08
Browse files Browse the repository at this point in the history
  • Loading branch information
bruth committed Dec 17, 2012
1 parent 73a39dd commit 12f9ea3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 42 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand Down
30 changes: 15 additions & 15 deletions jsonpatch.coffee
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
28 changes: 14 additions & 14 deletions jsonpatch.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
});

Expand All @@ -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'}]);
});


Expand All @@ -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);
Expand Down

0 comments on commit 12f9ea3

Please sign in to comment.