Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit 12f9ea3a7b3aaa2b94fac746763bd7715863ac09 1 parent 73a39dd
Byron Ruth bruth authored

Showing 4 changed files with 42 additions and 42 deletions. Show diff stats Hide diff stats

  1. +5 5 README.md
  2. +15 15 jsonpatch.coffee
  3. +14 14 jsonpatch.js
  4. +8 8 test.js
10 README.md
Source Rendered
... ... @@ -1,7 +1,7 @@
1 1 # jsonpatch.js
2 2
3 3 Library to apply JSON Patches in JavaScript
4   -http://tools.ietf.org/html/draft-ietf-appsawg-json-patch-06
  4 +http://tools.ietf.org/html/draft-ietf-appsawg-json-patch-08
5 5
6 6 jsonpatch.js works as in the browser as a script, as a Node module and as an
7 7 AMD module.
@@ -81,20 +81,20 @@ jsonpatch.apply({foo: [{bar: 'baz'}]}, [{op: 'replace', path: '/foo/0/bar', valu
81 81
82 82 ### Move
83 83
84   -Patch syntax: `{op: 'move', path: <path>, to: <path>}`
  84 +Patch syntax: `{op: 'move', from: <path>, path: <path>}`
85 85
86 86 ```javascript
87 87 // Move property, result {bar: [1, 2, 3]}
88   -jsonpatch.apply({foo: [1, 2, 3]}, [{op: 'move', path: '/foo', to: '/bar'}]);
  88 +jsonpatch.apply({foo: [1, 2, 3]}, [{op: 'move', from: '/foo', path: '/bar'}]);
89 89 ```
90 90
91 91 ### Copy
92 92
93   -Patch syntax: `{op: 'copy', path: <path>, to: <path>}`
  93 +Patch syntax: `{op: 'copy', from: <path>, path: <path>}`
94 94
95 95 ```javascript
96 96 // Copy property, result {foo: [1, 2, 3], bar: 2}
97   -jsonpatch.apply({foo: [1, 2, 3]}, [{op: 'copy', path: '/foo/1', to: '/bar'}]);
  97 +jsonpatch.apply({foo: [1, 2, 3]}, [{op: 'copy', from: '/foo/1', path: '/bar'}]);
98 98 ```
99 99
100 100 ### Test
30 jsonpatch.coffee
... ... @@ -1,4 +1,4 @@
1   -# jsonpatch.js 0.3.1
  1 +# jsonpatch.js 0.3.2
2 2 # (c) 2011-2012 Byron Ruth
3 3 # jsonpatch may be freely distributed under the BSD license
4 4
@@ -252,29 +252,29 @@
252 252
253 253 class MovePatch extends JSONPatch
254 254 initialize: (patch) ->
255   - @to = new JSONPointer(patch.to)
256   - len = @path.steps.length
  255 + @from = new JSONPointer(patch.from)
  256 + len = @from.steps.length
257 257
258 258 within = true
259 259 for i in [0..len]
260   - if @path.steps[i] isnt @to.steps[i]
  260 + if @from.steps[i] isnt @path.steps[i]
261 261 within = false
262 262 break
263 263
264 264 if within
265   - if @to.steps.length isnt len
  265 + if @path.steps.length isnt len
266 266 throw new InvalidPatchError("'to' member cannot be a descendent of 'path'")
267   - if @path.accessor is @to.accessor
  267 + if @from.accessor is @path.accessor
268 268 # The path and to pointers reference the same location,
269 269 # therefore apply can be a no-op
270 270 @apply = ->
271 271
272 272 validate: (patch) ->
273   - if 'to' not of patch then throw new InvalidPatchError()
  273 + if 'from' not of patch then throw new InvalidPatchError()
274 274
275 275 apply: (document) ->
276   - reference = @path.getReference(document)
277   - accessor = @path.accessor
  276 + reference = @from.getReference(document)
  277 + accessor = @from.accessor
278 278
279 279 if isArray(reference)
280 280 accessor = parseInt(accessor, 10)
@@ -287,8 +287,8 @@
287 287 value = reference[accessor]
288 288 delete reference[accessor]
289 289
290   - reference = @to.getReference(document)
291   - accessor = @to.accessor
  290 + reference = @path.getReference(document)
  291 + accessor = @path.accessor
292 292
293 293 # Add to object
294 294 if isArray(reference)
@@ -305,8 +305,8 @@
305 305
306 306 class CopyPatch extends MovePatch
307 307 apply: (document) ->
308   - reference = @path.getReference(document)
309   - accessor = @path.accessor
  308 + reference = @from.getReference(document)
  309 + accessor = @from.accessor
310 310
311 311 if isArray(reference)
312 312 accessor = parseInt(accessor, 10)
@@ -318,8 +318,8 @@
318 318 throw new PatchConflictError("Value at #{accessor} does not exist")
319 319 value = reference[accessor]
320 320
321   - reference = @to.getReference(document)
322   - accessor = @to.accessor
  321 + reference = @path.getReference(document)
  322 + accessor = @path.accessor
323 323
324 324 # Add to object
325 325 if isArray(reference)
28 jsonpatch.js
@@ -358,35 +358,35 @@
358 358
359 359 MovePatch.prototype.initialize = function(patch) {
360 360 var i, len, within, _i;
361   - this.to = new JSONPointer(patch.to);
362   - len = this.path.steps.length;
  361 + this.from = new JSONPointer(patch.from);
  362 + len = this.from.steps.length;
363 363 within = true;
364 364 for (i = _i = 0; 0 <= len ? _i <= len : _i >= len; i = 0 <= len ? ++_i : --_i) {
365   - if (this.path.steps[i] !== this.to.steps[i]) {
  365 + if (this.from.steps[i] !== this.path.steps[i]) {
366 366 within = false;
367 367 break;
368 368 }
369 369 }
370 370 if (within) {
371   - if (this.to.steps.length !== len) {
  371 + if (this.path.steps.length !== len) {
372 372 throw new InvalidPatchError("'to' member cannot be a descendent of 'path'");
373 373 }
374   - if (this.path.accessor === this.to.accessor) {
  374 + if (this.from.accessor === this.path.accessor) {
375 375 return this.apply = function() {};
376 376 }
377 377 }
378 378 };
379 379
380 380 MovePatch.prototype.validate = function(patch) {
381   - if (!('to' in patch)) {
  381 + if (!('from' in patch)) {
382 382 throw new InvalidPatchError();
383 383 }
384 384 };
385 385
386 386 MovePatch.prototype.apply = function(document) {
387 387 var accessor, reference, value;
388   - reference = this.path.getReference(document);
389   - accessor = this.path.accessor;
  388 + reference = this.from.getReference(document);
  389 + accessor = this.from.accessor;
390 390 if (isArray(reference)) {
391 391 accessor = parseInt(accessor, 10);
392 392 if (!(accessor in reference)) {
@@ -400,8 +400,8 @@
400 400 value = reference[accessor];
401 401 delete reference[accessor];
402 402 }
403   - reference = this.to.getReference(document);
404   - accessor = this.to.accessor;
  403 + reference = this.path.getReference(document);
  404 + accessor = this.path.accessor;
405 405 if (isArray(reference)) {
406 406 accessor = parseInt(accessor, 10);
407 407 if (accessor < 0 || accessor > reference.length) {
@@ -429,8 +429,8 @@
429 429
430 430 CopyPatch.prototype.apply = function(document) {
431 431 var accessor, reference, value;
432   - reference = this.path.getReference(document);
433   - accessor = this.path.accessor;
  432 + reference = this.from.getReference(document);
  433 + accessor = this.from.accessor;
434 434 if (isArray(reference)) {
435 435 accessor = parseInt(accessor, 10);
436 436 if (!(accessor in reference)) {
@@ -443,8 +443,8 @@
443 443 }
444 444 value = reference[accessor];
445 445 }
446   - reference = this.to.getReference(document);
447   - accessor = this.to.accessor;
  446 + reference = this.path.getReference(document);
  447 + accessor = this.path.accessor;
448 448 if (isArray(reference)) {
449 449 accessor = parseInt(accessor, 10);
450 450 if (accessor < 0 || accessor > reference.length) {
16 test.js
@@ -77,10 +77,10 @@ test('test', function() {
77 77 test('move', function() {
78 78 obj = {foo: 1, baz: [{qux: 'hello'}]};
79 79
80   - jsonpatch.apply(obj, [{op: 'move', path: '/foo', to: '/bar'}]);
  80 + jsonpatch.apply(obj, [{op: 'move', from: '/foo', path: '/bar'}]);
81 81 deepEqual(obj, {baz: [{qux: 'hello'}], bar: 1});
82 82
83   - jsonpatch.apply(obj, [{op: 'move', path: '/baz/0/qux', to: '/baz/1'}]);
  83 + jsonpatch.apply(obj, [{op: 'move', from: '/baz/0/qux', path: '/baz/1'}]);
84 84 deepEqual(obj, {baz: [{}, 'hello'], bar: 1});
85 85 });
86 86
@@ -88,10 +88,10 @@ test('move', function() {
88 88 test('copy', function() {
89 89 obj = {foo: 1, baz: [{qux: 'hello'}]};
90 90
91   - jsonpatch.apply(obj, [{op: 'copy', path: '/foo', to: '/bar'}]);
  91 + jsonpatch.apply(obj, [{op: 'copy', from: '/foo', path: '/bar'}]);
92 92 deepEqual(obj, {foo: 1, baz: [{qux: 'hello'}], bar: 1});
93 93
94   - jsonpatch.apply(obj, [{op: 'copy', path: '/baz/0/qux', to: '/baz/1'}]);
  94 + jsonpatch.apply(obj, [{op: 'copy', from: '/baz/0/qux', path: '/baz/1'}]);
95 95 deepEqual(obj, {foo: 1, baz: [{qux: 'hello'}, 'hello'], bar: 1});
96 96 });
97 97
@@ -115,12 +115,12 @@ JSLitmus.test('Replace Operation', function() {
115 115
116 116 JSLitmus.test('Move Operation', function() {
117 117 obj = {foo: 1, baz: [{qux: 'hello'}], bar: [1, 2, 3, 4]};
118   - jsonpatch.apply(obj, [{op: 'move', path: '/baz/0', to: '/bar/0'}]);
  118 + jsonpatch.apply(obj, [{op: 'move', from: '/baz/0', path: '/bar/0'}]);
119 119 });
120 120
121 121 JSLitmus.test('Copy Operation', function() {
122 122 obj = {foo: 1, baz: [{qux: 'hello'}], bar: [1, 2, 3, 4]};
123   - jsonpatch.apply(obj, [{op: 'copy', path: '/baz/0', to: '/bar/0'}]);
  123 + jsonpatch.apply(obj, [{op: 'copy', from: '/baz/0', path: '/bar/0'}]);
124 124 });
125 125
126 126
@@ -147,13 +147,13 @@ JSLitmus.test('Compiled Replace Operation', function() {
147 147 replaceCompiled(obj);
148 148 });
149 149
150   -var moveCompiled = jsonpatch.compile([{op: 'move', path: '/baz/0', to: '/bar/0'}]);
  150 +var moveCompiled = jsonpatch.compile([{op: 'move', from: '/baz/0', path: '/bar/0'}]);
151 151 JSLitmus.test('Compiled Move Operation', function() {
152 152 obj = {foo: 1, baz: [{qux: 'hello'}], bar: [1, 2, 3, 4]};
153 153 moveCompiled(obj);
154 154 });
155 155
156   -var copyCompiled = jsonpatch.compile([{op: 'copy', path: '/baz/0', to: '/bar/0'}]);
  156 +var copyCompiled = jsonpatch.compile([{op: 'copy', from: '/baz/0', path: '/bar/0'}]);
157 157 JSLitmus.test('Compiled Copy Operation', function() {
158 158 obj = {foo: 1, baz: [{qux: 'hello'}], bar: [1, 2, 3, 4]};
159 159 copyCompiled(obj);

0 comments on commit 12f9ea3

Please sign in to comment.
Something went wrong with that request. Please try again.