bruth / jsonpatch-js

A JavaScript implementation of the JSON Media Type for partial modifications: http://tools.ietf.org/html/rfc6902
http://bruth.github.io/jsonpatch-js
BSD 2-Clause "Simplified" License
181 stars 24 forks source link

Negative indices when adding to an array should insert relative to the end #7

Closed krisnye closed 12 years ago

krisnye commented 12 years ago

I know this isn't in the JSON-PATCH spec, but it should be.

If you add to an array with a negative index that should be relative to the end of the array, just like in the standard Array splice method. Your current code would handle this already if you simply removed the if (acc < 0) expression in add.

How else do you inform a patch to just add to the end of an existing array?

bruth commented 12 years ago

My understanding of a PATCH is to apply a deterministic augmentation to some document/object. Negative indexes are relative to the current size of the array, that is to say the known length of the array. As an example, if Client A and Client B attempts to apply a patch at the same time with a different sized array locally.. a patch with a relative index would certainly work, but neither client would have a consistent representation of the document.

Clients should calculate patch documents from their current state of the document and the last known state of the document. If the patch fails to be applied (e.g. on the server), the client's job is to get the latest representation of resource and attempt to create a new patch (also known as optimistic concurrency).

To answer your request, I am not inclined to implement this feature simply because patch documents would more likely to be incompatible between other JSON-Patch implementations.