Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fast-json-patch

Package Overview
Dependencies
Maintainers
3
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fast-json-patch - npm Package Compare versions

Comparing version 2.2.1 to 3.0.0-0

commonjs/core.d.ts

245

dist/fast-json-patch.js

@@ -1,2 +0,2 @@

/*! fast-json-patch, version: 2.2.1 */
/*! fast-json-patch, version: 3.0.0-0 */
var jsonpatch =

@@ -281,6 +281,5 @@ /******/ (function(modules) { // webpackBootstrap

Object.defineProperty(exports, "__esModule", { value: true });
var areEquals = __webpack_require__(3);
var helpers_1 = __webpack_require__(0);
exports.JsonPatchError = helpers_1.PatchError;
exports.deepClone = helpers_1._deepClone;
var helpers_js_1 = __webpack_require__(0);
exports.JsonPatchError = helpers_js_1.PatchError;
exports.deepClone = helpers_js_1._deepClone;
/* We use a Javascript hash to store each

@@ -314,3 +313,3 @@ function. Each hash entry (property) uses

if (removed) {
removed = helpers_1._deepClone(removed);
removed = helpers_js_1._deepClone(removed);
}

@@ -324,7 +323,7 @@ var originalValue = applyOperation(document, { op: "remove", path: this.from }).removed;

// enforce copy by value so further operations don't affect source (see issue #177)
applyOperation(document, { op: "add", path: this.path, value: helpers_1._deepClone(valueToCopy) });
applyOperation(document, { op: "add", path: this.path, value: helpers_js_1._deepClone(valueToCopy) });
return { newDocument: document };
},
test: function (obj, key, document) {
return { newDocument: document, test: areEquals(obj[key], this.value) };
return { newDocument: document, test: _areEquals(obj[key], this.value) };
},

@@ -339,3 +338,3 @@ _get: function (obj, key, document) {

add: function (arr, i, document) {
if (helpers_1.isInteger(i)) {
if (helpers_js_1.isInteger(i)) {
arr.splice(i, 0, this.value);

@@ -427,3 +426,3 @@ }

else if (operation.op === 'test') {
returnValue.test = areEquals(document, operation.value);
returnValue.test = _areEquals(document, operation.value);
if (returnValue.test === false) {

@@ -455,3 +454,3 @@ throw new exports.JsonPatchError("Test operation failed", 'TEST_OPERATION_FAILED', index, operation, document);

if (!mutateDocument) {
document = helpers_1._deepClone(document);
document = helpers_js_1._deepClone(document);
}

@@ -496,6 +495,6 @@ var path = operation.path || "";

else {
if (validateOperation && !helpers_1.isInteger(key)) {
if (validateOperation && !helpers_js_1.isInteger(key)) {
throw new exports.JsonPatchError("Expected an unsigned base-10 integer value, making the new referenced value the array element with the zero-based index", "OPERATION_PATH_ILLEGAL_ARRAY_INDEX", index, operation, document);
} // only parse key when it's an integer for `arr.prop` to work
else if (helpers_1.isInteger(key)) {
else if (helpers_js_1.isInteger(key)) {
key = ~~key;

@@ -517,3 +516,3 @@ }

if (key && key.indexOf('~') != -1) {
key = helpers_1.unescapePathComponent(key);
key = helpers_js_1.unescapePathComponent(key);
}

@@ -556,3 +555,3 @@ if (t >= len) {

if (!mutateDocument) {
document = helpers_1._deepClone(document);
document = helpers_js_1._deepClone(document);
}

@@ -613,3 +612,3 @@ var results = new Array(patch.length);

}
else if ((operation.op === 'add' || operation.op === 'replace' || operation.op === 'test') && helpers_1.hasUndefined(operation.value)) {
else if ((operation.op === 'add' || operation.op === 'replace' || operation.op === 'test') && helpers_js_1.hasUndefined(operation.value)) {
throw new exports.JsonPatchError('Operation `value` property is not present (applicable in `add`, `replace` and `test` operations)', 'OPERATION_VALUE_CANNOT_CONTAIN_UNDEFINED', index, operation, document);

@@ -654,3 +653,3 @@ }

//clone document and sequence so that we can safely try applying operations
applyPatch(helpers_1._deepClone(document), helpers_1._deepClone(sequence), externalValidator || true);
applyPatch(helpers_js_1._deepClone(document), helpers_js_1._deepClone(sequence), externalValidator || true);
}

@@ -674,15 +673,54 @@ else {

exports.validate = validate;
/**
* Default export for backwards compat
*/
exports.default = {
JsonPatchError: exports.JsonPatchError,
deepClone: exports.deepClone,
getValueByPointer: getValueByPointer,
applyOperation: applyOperation,
applyPatch: applyPatch,
applyReducer: applyReducer,
validator: validator,
validate: validate
};
// based on https://github.com/epoberezkin/fast-deep-equal
// MIT License
// Copyright (c) 2017 Evgeny Poberezkin
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
function _areEquals(a, b) {
if (a === b)
return true;
if (a && b && typeof a == 'object' && typeof b == 'object') {
var arrA = Array.isArray(a), arrB = Array.isArray(b), i, length, key;
if (arrA && arrB) {
length = a.length;
if (length != b.length)
return false;
for (i = length; i-- !== 0;)
if (!_areEquals(a[i], b[i]))
return false;
return true;
}
if (arrA != arrB)
return false;
var keys = Object.keys(a);
length = keys.length;
if (length !== Object.keys(b).length)
return false;
for (i = length; i-- !== 0;)
if (!b.hasOwnProperty(keys[i]))
return false;
for (i = length; i-- !== 0;) {
key = keys[i];
if (!_areEquals(a[key], b[key]))
return false;
}
return true;
}
return a !== a && b !== b;
}
exports._areEquals = _areEquals;
;

@@ -694,13 +732,19 @@

var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var core = __webpack_require__(1);
Object.assign(exports, core);
var duplex = __webpack_require__(3);
Object.assign(exports, duplex);
var helpers = __webpack_require__(0);
exports.JsonPatchError = helpers.PatchError;
exports.deepClone = helpers._deepClone;
exports.escapePathComponent = helpers.escapePathComponent;
exports.unescapePathComponent = helpers.unescapePathComponent;
/***/ }),
/* 3 */
/***/ (function(module, exports, __webpack_require__) {
Object.defineProperty(exports, "__esModule", { value: true });

@@ -712,18 +756,4 @@ /*!

*/
var helpers_1 = __webpack_require__(0);
var core_1 = __webpack_require__(1);
/* export all core functions and types */
var core_2 = __webpack_require__(1);
exports.applyOperation = core_2.applyOperation;
exports.applyPatch = core_2.applyPatch;
exports.applyReducer = core_2.applyReducer;
exports.getValueByPointer = core_2.getValueByPointer;
exports.validate = core_2.validate;
exports.validator = core_2.validator;
/* export some helpers */
var helpers_2 = __webpack_require__(0);
exports.JsonPatchError = helpers_2.PatchError;
exports.deepClone = helpers_2._deepClone;
exports.escapePathComponent = helpers_2.escapePathComponent;
exports.unescapePathComponent = helpers_2.unescapePathComponent;
var helpers_js_1 = __webpack_require__(0);
var core_js_1 = __webpack_require__(1);
var beforeDict = new WeakMap();

@@ -779,3 +809,3 @@ var Mirror = /** @class */ (function () {

observer = {};
mirror.value = helpers_1._deepClone(obj);
mirror.value = helpers_js_1._deepClone(obj);
if (callback) {

@@ -825,3 +855,3 @@ observer.callback = callback;

if (observer.patches.length) {
core_1.applyPatch(mirror.value, observer.patches);
core_js_1.applyPatch(mirror.value, observer.patches);
}

@@ -846,4 +876,4 @@ var temp = observer.patches;

}
var newKeys = helpers_1._objectKeys(obj);
var oldKeys = helpers_1._objectKeys(mirror);
var newKeys = helpers_js_1._objectKeys(obj);
var oldKeys = helpers_js_1._objectKeys(mirror);
var changed = false;

@@ -855,6 +885,6 @@ var deleted = false;

var oldVal = mirror[key];
if (helpers_1.hasOwnProperty(obj, key) && !(obj[key] === undefined && oldVal !== undefined && Array.isArray(obj) === false)) {
if (helpers_js_1.hasOwnProperty(obj, key) && !(obj[key] === undefined && oldVal !== undefined && Array.isArray(obj) === false)) {
var newVal = obj[key];
if (typeof oldVal == "object" && oldVal != null && typeof newVal == "object" && newVal != null) {
_generate(oldVal, newVal, patches, path + "/" + helpers_1.escapePathComponent(key), invertible);
_generate(oldVal, newVal, patches, path + "/" + helpers_js_1.escapePathComponent(key), invertible);
}

@@ -865,5 +895,5 @@ else {

if (invertible) {
patches.push({ op: "test", path: path + "/" + helpers_1.escapePathComponent(key), value: helpers_1._deepClone(oldVal) });
patches.push({ op: "test", path: path + "/" + helpers_js_1.escapePathComponent(key), value: helpers_js_1._deepClone(oldVal) });
}
patches.push({ op: "replace", path: path + "/" + helpers_1.escapePathComponent(key), value: helpers_1._deepClone(newVal) });
patches.push({ op: "replace", path: path + "/" + helpers_js_1.escapePathComponent(key), value: helpers_js_1._deepClone(newVal) });
}

@@ -874,5 +904,5 @@ }

if (invertible) {
patches.push({ op: "test", path: path + "/" + helpers_1.escapePathComponent(key), value: helpers_1._deepClone(oldVal) });
patches.push({ op: "test", path: path + "/" + helpers_js_1.escapePathComponent(key), value: helpers_js_1._deepClone(oldVal) });
}
patches.push({ op: "remove", path: path + "/" + helpers_1.escapePathComponent(key) });
patches.push({ op: "remove", path: path + "/" + helpers_js_1.escapePathComponent(key) });
deleted = true; // property has been deleted

@@ -893,4 +923,4 @@ }

var key = newKeys[t];
if (!helpers_1.hasOwnProperty(mirror, key) && obj[key] !== undefined) {
patches.push({ op: "add", path: path + "/" + helpers_1.escapePathComponent(key), value: helpers_1._deepClone(obj[key]) });
if (!helpers_js_1.hasOwnProperty(mirror, key) && obj[key] !== undefined) {
patches.push({ op: "add", path: path + "/" + helpers_js_1.escapePathComponent(key), value: helpers_js_1._deepClone(obj[key]) });
}

@@ -909,82 +939,5 @@ }

exports.compare = compare;
/**
* Default export for backwards compat
*/
// import just to re-export as default
var core = __webpack_require__(1);
var helpers_3 = __webpack_require__(0);
exports.default = __assign({}, core, {
// duplex
unobserve: unobserve,
observe: observe,
generate: generate,
compare: compare,
// helpers
JsonPatchError: helpers_3.PatchError, deepClone: helpers_1._deepClone, escapePathComponent: helpers_1.escapePathComponent,
unescapePathComponent: helpers_3.unescapePathComponent });
/***/ }),
/* 3 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
var isArray = Array.isArray;
var keyList = Object.keys;
var hasProp = Object.prototype.hasOwnProperty;
module.exports = function equal(a, b) {
if (a === b) return true;
if (a && b && typeof a == 'object' && typeof b == 'object') {
var arrA = isArray(a)
, arrB = isArray(b)
, i
, length
, key;
if (arrA && arrB) {
length = a.length;
if (length != b.length) return false;
for (i = length; i-- !== 0;)
if (!equal(a[i], b[i])) return false;
return true;
}
if (arrA != arrB) return false;
var dateA = a instanceof Date
, dateB = b instanceof Date;
if (dateA != dateB) return false;
if (dateA && dateB) return a.getTime() == b.getTime();
var regexpA = a instanceof RegExp
, regexpB = b instanceof RegExp;
if (regexpA != regexpB) return false;
if (regexpA && regexpB) return a.toString() == b.toString();
var keys = keyList(a);
length = keys.length;
if (length !== keyList(b).length)
return false;
for (i = length; i-- !== 0;)
if (!hasProp.call(b, keys[i])) return false;
for (i = length; i-- !== 0;) {
key = keys[i];
if (!equal(a[key], b[key])) return false;
}
return true;
}
return a!==a && b!==b;
};
/***/ })
/******/ ]);

@@ -1,2 +0,2 @@

/*! fast-json-patch, version: 2.2.1 */
/*! fast-json-patch, version: 3.0.0-0 */
var jsonpatch=function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=2)}([function(e,t){

@@ -8,3 +8,3 @@ /*!

*/
var r,n=this&&this.__extends||(r=function(e,t){return(r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)t.hasOwnProperty(r)&&(e[r]=t[r])})(e,t)},function(e,t){function n(){this.constructor=e}r(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)});Object.defineProperty(t,"__esModule",{value:!0});var o=Object.prototype.hasOwnProperty;function a(e,t){return o.call(e,t)}function i(e){if(Array.isArray(e)){for(var t=new Array(e.length),r=0;r<t.length;r++)t[r]=""+r;return t}if(Object.keys)return Object.keys(e);t=[];for(var n in e)a(e,n)&&t.push(n);return t}function p(e){return-1===e.indexOf("/")&&-1===e.indexOf("~")?e:e.replace(/~/g,"~0").replace(/\//g,"~1")}function u(e,t){var r;for(var n in e)if(a(e,n)){if(e[n]===t)return p(n)+"/";if("object"==typeof e[n]&&""!=(r=u(e[n],t)))return p(n)+"/"+r}return""}function s(e,t){var r=[e];for(var n in t){var o="object"==typeof t[n]?JSON.stringify(t[n],null,2):t[n];void 0!==o&&r.push(n+": "+o)}return r.join("\n")}t.hasOwnProperty=a,t._objectKeys=i,t._deepClone=function(e){switch(typeof e){case"object":return JSON.parse(JSON.stringify(e));case"undefined":return null;default:return e}},t.isInteger=function(e){for(var t,r=0,n=e.length;r<n;){if(!((t=e.charCodeAt(r))>=48&&t<=57))return!1;r++}return!0},t.escapePathComponent=p,t.unescapePathComponent=function(e){return e.replace(/~1/g,"/").replace(/~0/g,"~")},t._getPathRecursive=u,t.getPath=function(e,t){if(e===t)return"/";var r=u(e,t);if(""===r)throw new Error("Object not found in root");return"/"+r},t.hasUndefined=function e(t){if(void 0===t)return!0;if(t)if(Array.isArray(t)){for(var r=0,n=t.length;r<n;r++)if(e(t[r]))return!0}else if("object"==typeof t){var o=i(t),a=o.length;for(r=0;r<a;r++)if(e(t[o[r]]))return!0}return!1};var c=function(e){function t(t,r,n,o,a){var i=this.constructor,p=e.call(this,s(t,{name:r,index:n,operation:o,tree:a}))||this;return p.name=r,p.index=n,p.operation=o,p.tree=a,Object.setPrototypeOf(p,i.prototype),p.message=s(t,{name:r,index:n,operation:o,tree:a}),p}return n(t,e),t}(Error);t.PatchError=c},function(e,t,r){Object.defineProperty(t,"__esModule",{value:!0});var n=r(3),o=r(0);t.JsonPatchError=o.PatchError,t.deepClone=o._deepClone;var a={add:function(e,t,r){return e[t]=this.value,{newDocument:r}},remove:function(e,t,r){var n=e[t];return delete e[t],{newDocument:r,removed:n}},replace:function(e,t,r){var n=e[t];return e[t]=this.value,{newDocument:r,removed:n}},move:function(e,t,r){var n=p(r,this.path);n&&(n=o._deepClone(n));var a=u(r,{op:"remove",path:this.from}).removed;return u(r,{op:"add",path:this.path,value:a}),{newDocument:r,removed:n}},copy:function(e,t,r){var n=p(r,this.from);return u(r,{op:"add",path:this.path,value:o._deepClone(n)}),{newDocument:r}},test:function(e,t,r){return{newDocument:r,test:n(e[t],this.value)}},_get:function(e,t,r){return this.value=e[t],{newDocument:r}}},i={add:function(e,t,r){return o.isInteger(t)?e.splice(t,0,this.value):e[t]=this.value,{newDocument:r,index:t}},remove:function(e,t,r){return{newDocument:r,removed:e.splice(t,1)[0]}},replace:function(e,t,r){var n=e[t];return e[t]=this.value,{newDocument:r,removed:n}},move:a.move,copy:a.copy,test:a.test,_get:a._get};function p(e,t){if(""==t)return e;var r={op:"_get",path:t};return u(e,r),r.value}function u(e,r,u,s,c,l){if(void 0===u&&(u=!1),void 0===s&&(s=!0),void 0===c&&(c=!0),void 0===l&&(l=0),u&&("function"==typeof u?u(r,0,e,r.path):f(r,0)),""===r.path){var h={newDocument:e};if("add"===r.op)return h.newDocument=r.value,h;if("replace"===r.op)return h.newDocument=r.value,h.removed=e,h;if("move"===r.op||"copy"===r.op)return h.newDocument=p(e,r.from),"move"===r.op&&(h.removed=e),h;if("test"===r.op){if(h.test=n(e,r.value),!1===h.test)throw new t.JsonPatchError("Test operation failed","TEST_OPERATION_FAILED",l,r,e);return h.newDocument=e,h}if("remove"===r.op)return h.removed=e,h.newDocument=null,h;if("_get"===r.op)return r.value=e,h;if(u)throw new t.JsonPatchError("Operation `op` property is not one of operations defined in RFC-6902","OPERATION_OP_INVALID",l,r,e);return h}s||(e=o._deepClone(e));var d=(r.path||"").split("/"),v=e,y=1,w=d.length,_=void 0,O=void 0,m=void 0;for(m="function"==typeof u?u:f;;){if(O=d[y],c&&"__proto__"==O)throw new TypeError("JSON-Patch: modifying `__proto__` prop is banned for security reasons, if this was on purpose, please set `banPrototypeModifications` flag false and pass it to this function. More info in fast-json-patch README");if(u&&void 0===_&&(void 0===v[O]?_=d.slice(0,y).join("/"):y==w-1&&(_=r.path),void 0!==_&&m(r,0,e,_)),y++,Array.isArray(v)){if("-"===O)O=v.length;else{if(u&&!o.isInteger(O))throw new t.JsonPatchError("Expected an unsigned base-10 integer value, making the new referenced value the array element with the zero-based index","OPERATION_PATH_ILLEGAL_ARRAY_INDEX",l,r,e);o.isInteger(O)&&(O=~~O)}if(y>=w){if(u&&"add"===r.op&&O>v.length)throw new t.JsonPatchError("The specified index MUST NOT be greater than the number of elements in the array","OPERATION_VALUE_OUT_OF_BOUNDS",l,r,e);if(!1===(h=i[r.op].call(r,v,O,e)).test)throw new t.JsonPatchError("Test operation failed","TEST_OPERATION_FAILED",l,r,e);return h}}else if(O&&-1!=O.indexOf("~")&&(O=o.unescapePathComponent(O)),y>=w){if(!1===(h=a[r.op].call(r,v,O,e)).test)throw new t.JsonPatchError("Test operation failed","TEST_OPERATION_FAILED",l,r,e);return h}v=v[O]}}function s(e,r,n,a,i){if(void 0===a&&(a=!0),void 0===i&&(i=!0),n&&!Array.isArray(r))throw new t.JsonPatchError("Patch sequence must be an array","SEQUENCE_NOT_AN_ARRAY");a||(e=o._deepClone(e));for(var p=new Array(r.length),s=0,c=r.length;s<c;s++)p[s]=u(e,r[s],n,!0,i,s),e=p[s].newDocument;return p.newDocument=e,p}function c(e,r,n){var o=u(e,r);if(!1===o.test)throw new t.JsonPatchError("Test operation failed","TEST_OPERATION_FAILED",n,r,e);return o.newDocument}function f(e,r,n,i){if("object"!=typeof e||null===e||Array.isArray(e))throw new t.JsonPatchError("Operation is not an object","OPERATION_NOT_AN_OBJECT",r,e,n);if(!a[e.op])throw new t.JsonPatchError("Operation `op` property is not one of operations defined in RFC-6902","OPERATION_OP_INVALID",r,e,n);if("string"!=typeof e.path)throw new t.JsonPatchError("Operation `path` property is not a string","OPERATION_PATH_INVALID",r,e,n);if(0!==e.path.indexOf("/")&&e.path.length>0)throw new t.JsonPatchError('Operation `path` property must start with "/"',"OPERATION_PATH_INVALID",r,e,n);if(("move"===e.op||"copy"===e.op)&&"string"!=typeof e.from)throw new t.JsonPatchError("Operation `from` property is not present (applicable in `move` and `copy` operations)","OPERATION_FROM_REQUIRED",r,e,n);if(("add"===e.op||"replace"===e.op||"test"===e.op)&&void 0===e.value)throw new t.JsonPatchError("Operation `value` property is not present (applicable in `add`, `replace` and `test` operations)","OPERATION_VALUE_REQUIRED",r,e,n);if(("add"===e.op||"replace"===e.op||"test"===e.op)&&o.hasUndefined(e.value))throw new t.JsonPatchError("Operation `value` property is not present (applicable in `add`, `replace` and `test` operations)","OPERATION_VALUE_CANNOT_CONTAIN_UNDEFINED",r,e,n);if(n)if("add"==e.op){var p=e.path.split("/").length,u=i.split("/").length;if(p!==u+1&&p!==u)throw new t.JsonPatchError("Cannot perform an `add` operation at the desired path","OPERATION_PATH_CANNOT_ADD",r,e,n)}else if("replace"===e.op||"remove"===e.op||"_get"===e.op){if(e.path!==i)throw new t.JsonPatchError("Cannot perform the operation at a path that does not exist","OPERATION_PATH_UNRESOLVABLE",r,e,n)}else if("move"===e.op||"copy"===e.op){var s=l([{op:"_get",path:e.from,value:void 0}],n);if(s&&"OPERATION_PATH_UNRESOLVABLE"===s.name)throw new t.JsonPatchError("Cannot perform the operation from a path that does not exist","OPERATION_FROM_UNRESOLVABLE",r,e,n)}}function l(e,r,n){try{if(!Array.isArray(e))throw new t.JsonPatchError("Patch sequence must be an array","SEQUENCE_NOT_AN_ARRAY");if(r)s(o._deepClone(r),o._deepClone(e),n||!0);else{n=n||f;for(var a=0;a<e.length;a++)n(e[a],a,r,void 0)}}catch(e){if(e instanceof t.JsonPatchError)return e;throw e}}t.getValueByPointer=p,t.applyOperation=u,t.applyPatch=s,t.applyReducer=c,t.validator=f,t.validate=l,t.default={JsonPatchError:t.JsonPatchError,deepClone:t.deepClone,getValueByPointer:p,applyOperation:u,applyPatch:s,applyReducer:c,validator:f,validate:l}},function(e,t,r){var n=this&&this.__assign||function(){return(n=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var o in t=arguments[r])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e}).apply(this,arguments)};Object.defineProperty(t,"__esModule",{value:!0});
var r,n=this&&this.__extends||(r=function(e,t){return(r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)t.hasOwnProperty(r)&&(e[r]=t[r])})(e,t)},function(e,t){function n(){this.constructor=e}r(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)});Object.defineProperty(t,"__esModule",{value:!0});var o=Object.prototype.hasOwnProperty;function a(e,t){return o.call(e,t)}function i(e){if(Array.isArray(e)){for(var t=new Array(e.length),r=0;r<t.length;r++)t[r]=""+r;return t}if(Object.keys)return Object.keys(e);t=[];for(var n in e)a(e,n)&&t.push(n);return t}function p(e){return-1===e.indexOf("/")&&-1===e.indexOf("~")?e:e.replace(/~/g,"~0").replace(/\//g,"~1")}function u(e,t){var r;for(var n in e)if(a(e,n)){if(e[n]===t)return p(n)+"/";if("object"==typeof e[n]&&""!=(r=u(e[n],t)))return p(n)+"/"+r}return""}function s(e,t){var r=[e];for(var n in t){var o="object"==typeof t[n]?JSON.stringify(t[n],null,2):t[n];void 0!==o&&r.push(n+": "+o)}return r.join("\n")}t.hasOwnProperty=a,t._objectKeys=i,t._deepClone=function(e){switch(typeof e){case"object":return JSON.parse(JSON.stringify(e));case"undefined":return null;default:return e}},t.isInteger=function(e){for(var t,r=0,n=e.length;r<n;){if(!((t=e.charCodeAt(r))>=48&&t<=57))return!1;r++}return!0},t.escapePathComponent=p,t.unescapePathComponent=function(e){return e.replace(/~1/g,"/").replace(/~0/g,"~")},t._getPathRecursive=u,t.getPath=function(e,t){if(e===t)return"/";var r=u(e,t);if(""===r)throw new Error("Object not found in root");return"/"+r},t.hasUndefined=function e(t){if(void 0===t)return!0;if(t)if(Array.isArray(t)){for(var r=0,n=t.length;r<n;r++)if(e(t[r]))return!0}else if("object"==typeof t){var o=i(t),a=o.length;for(r=0;r<a;r++)if(e(t[o[r]]))return!0}return!1};var c=function(e){function t(t,r,n,o,a){var i=this.constructor,p=e.call(this,s(t,{name:r,index:n,operation:o,tree:a}))||this;return p.name=r,p.index=n,p.operation=o,p.tree=a,Object.setPrototypeOf(p,i.prototype),p.message=s(t,{name:r,index:n,operation:o,tree:a}),p}return n(t,e),t}(Error);t.PatchError=c},function(e,t,r){Object.defineProperty(t,"__esModule",{value:!0});var n=r(0);t.JsonPatchError=n.PatchError,t.deepClone=n._deepClone;var o={add:function(e,t,r){return e[t]=this.value,{newDocument:r}},remove:function(e,t,r){var n=e[t];return delete e[t],{newDocument:r,removed:n}},replace:function(e,t,r){var n=e[t];return e[t]=this.value,{newDocument:r,removed:n}},move:function(e,t,r){var o=i(r,this.path);o&&(o=n._deepClone(o));var a=p(r,{op:"remove",path:this.from}).removed;return p(r,{op:"add",path:this.path,value:a}),{newDocument:r,removed:o}},copy:function(e,t,r){var o=i(r,this.from);return p(r,{op:"add",path:this.path,value:n._deepClone(o)}),{newDocument:r}},test:function(e,t,r){return{newDocument:r,test:f(e[t],this.value)}},_get:function(e,t,r){return this.value=e[t],{newDocument:r}}},a={add:function(e,t,r){return n.isInteger(t)?e.splice(t,0,this.value):e[t]=this.value,{newDocument:r,index:t}},remove:function(e,t,r){return{newDocument:r,removed:e.splice(t,1)[0]}},replace:function(e,t,r){var n=e[t];return e[t]=this.value,{newDocument:r,removed:n}},move:o.move,copy:o.copy,test:o.test,_get:o._get};function i(e,t){if(""==t)return e;var r={op:"_get",path:t};return p(e,r),r.value}function p(e,r,p,u,c,l){if(void 0===p&&(p=!1),void 0===u&&(u=!0),void 0===c&&(c=!0),void 0===l&&(l=0),p&&("function"==typeof p?p(r,0,e,r.path):s(r,0)),""===r.path){var h={newDocument:e};if("add"===r.op)return h.newDocument=r.value,h;if("replace"===r.op)return h.newDocument=r.value,h.removed=e,h;if("move"===r.op||"copy"===r.op)return h.newDocument=i(e,r.from),"move"===r.op&&(h.removed=e),h;if("test"===r.op){if(h.test=f(e,r.value),!1===h.test)throw new t.JsonPatchError("Test operation failed","TEST_OPERATION_FAILED",l,r,e);return h.newDocument=e,h}if("remove"===r.op)return h.removed=e,h.newDocument=null,h;if("_get"===r.op)return r.value=e,h;if(p)throw new t.JsonPatchError("Operation `op` property is not one of operations defined in RFC-6902","OPERATION_OP_INVALID",l,r,e);return h}u||(e=n._deepClone(e));var d=(r.path||"").split("/"),v=e,y=1,w=d.length,_=void 0,O=void 0,m=void 0;for(m="function"==typeof p?p:s;;){if(O=d[y],c&&"__proto__"==O)throw new TypeError("JSON-Patch: modifying `__proto__` prop is banned for security reasons, if this was on purpose, please set `banPrototypeModifications` flag false and pass it to this function. More info in fast-json-patch README");if(p&&void 0===_&&(void 0===v[O]?_=d.slice(0,y).join("/"):y==w-1&&(_=r.path),void 0!==_&&m(r,0,e,_)),y++,Array.isArray(v)){if("-"===O)O=v.length;else{if(p&&!n.isInteger(O))throw new t.JsonPatchError("Expected an unsigned base-10 integer value, making the new referenced value the array element with the zero-based index","OPERATION_PATH_ILLEGAL_ARRAY_INDEX",l,r,e);n.isInteger(O)&&(O=~~O)}if(y>=w){if(p&&"add"===r.op&&O>v.length)throw new t.JsonPatchError("The specified index MUST NOT be greater than the number of elements in the array","OPERATION_VALUE_OUT_OF_BOUNDS",l,r,e);if(!1===(h=a[r.op].call(r,v,O,e)).test)throw new t.JsonPatchError("Test operation failed","TEST_OPERATION_FAILED",l,r,e);return h}}else if(O&&-1!=O.indexOf("~")&&(O=n.unescapePathComponent(O)),y>=w){if(!1===(h=o[r.op].call(r,v,O,e)).test)throw new t.JsonPatchError("Test operation failed","TEST_OPERATION_FAILED",l,r,e);return h}v=v[O]}}function u(e,r,o,a,i){if(void 0===a&&(a=!0),void 0===i&&(i=!0),o&&!Array.isArray(r))throw new t.JsonPatchError("Patch sequence must be an array","SEQUENCE_NOT_AN_ARRAY");a||(e=n._deepClone(e));for(var u=new Array(r.length),s=0,c=r.length;s<c;s++)u[s]=p(e,r[s],o,!0,i,s),e=u[s].newDocument;return u.newDocument=e,u}function s(e,r,a,i){if("object"!=typeof e||null===e||Array.isArray(e))throw new t.JsonPatchError("Operation is not an object","OPERATION_NOT_AN_OBJECT",r,e,a);if(!o[e.op])throw new t.JsonPatchError("Operation `op` property is not one of operations defined in RFC-6902","OPERATION_OP_INVALID",r,e,a);if("string"!=typeof e.path)throw new t.JsonPatchError("Operation `path` property is not a string","OPERATION_PATH_INVALID",r,e,a);if(0!==e.path.indexOf("/")&&e.path.length>0)throw new t.JsonPatchError('Operation `path` property must start with "/"',"OPERATION_PATH_INVALID",r,e,a);if(("move"===e.op||"copy"===e.op)&&"string"!=typeof e.from)throw new t.JsonPatchError("Operation `from` property is not present (applicable in `move` and `copy` operations)","OPERATION_FROM_REQUIRED",r,e,a);if(("add"===e.op||"replace"===e.op||"test"===e.op)&&void 0===e.value)throw new t.JsonPatchError("Operation `value` property is not present (applicable in `add`, `replace` and `test` operations)","OPERATION_VALUE_REQUIRED",r,e,a);if(("add"===e.op||"replace"===e.op||"test"===e.op)&&n.hasUndefined(e.value))throw new t.JsonPatchError("Operation `value` property is not present (applicable in `add`, `replace` and `test` operations)","OPERATION_VALUE_CANNOT_CONTAIN_UNDEFINED",r,e,a);if(a)if("add"==e.op){var p=e.path.split("/").length,u=i.split("/").length;if(p!==u+1&&p!==u)throw new t.JsonPatchError("Cannot perform an `add` operation at the desired path","OPERATION_PATH_CANNOT_ADD",r,e,a)}else if("replace"===e.op||"remove"===e.op||"_get"===e.op){if(e.path!==i)throw new t.JsonPatchError("Cannot perform the operation at a path that does not exist","OPERATION_PATH_UNRESOLVABLE",r,e,a)}else if("move"===e.op||"copy"===e.op){var s=c([{op:"_get",path:e.from,value:void 0}],a);if(s&&"OPERATION_PATH_UNRESOLVABLE"===s.name)throw new t.JsonPatchError("Cannot perform the operation from a path that does not exist","OPERATION_FROM_UNRESOLVABLE",r,e,a)}}function c(e,r,o){try{if(!Array.isArray(e))throw new t.JsonPatchError("Patch sequence must be an array","SEQUENCE_NOT_AN_ARRAY");if(r)u(n._deepClone(r),n._deepClone(e),o||!0);else{o=o||s;for(var a=0;a<e.length;a++)o(e[a],a,r,void 0)}}catch(e){if(e instanceof t.JsonPatchError)return e;throw e}}function f(e,t){if(e===t)return!0;if(e&&t&&"object"==typeof e&&"object"==typeof t){var r,n,o,a=Array.isArray(e),i=Array.isArray(t);if(a&&i){if((n=e.length)!=t.length)return!1;for(r=n;0!=r--;)if(!f(e[r],t[r]))return!1;return!0}if(a!=i)return!1;var p=Object.keys(e);if((n=p.length)!==Object.keys(t).length)return!1;for(r=n;0!=r--;)if(!t.hasOwnProperty(p[r]))return!1;for(r=n;0!=r--;)if(!f(e[o=p[r]],t[o]))return!1;return!0}return e!=e&&t!=t}t.getValueByPointer=i,t.applyOperation=p,t.applyPatch=u,t.applyReducer=function(e,r,n){var o=p(e,r);if(!1===o.test)throw new t.JsonPatchError("Test operation failed","TEST_OPERATION_FAILED",n,r,e);return o.newDocument},t.validator=s,t.validate=c,t._areEquals=f},function(e,t,r){var n=r(1);Object.assign(t,n);var o=r(3);Object.assign(t,o);var a=r(0);t.JsonPatchError=a.PatchError,t.deepClone=a._deepClone,t.escapePathComponent=a.escapePathComponent,t.unescapePathComponent=a.unescapePathComponent},function(e,t,r){Object.defineProperty(t,"__esModule",{value:!0});
/*!

@@ -15,2 +15,2 @@ * https://github.com/Starcounter-Jack/JSON-Patch

*/
var o=r(0),a=r(1),i=r(1);t.applyOperation=i.applyOperation,t.applyPatch=i.applyPatch,t.applyReducer=i.applyReducer,t.getValueByPointer=i.getValueByPointer,t.validate=i.validate,t.validator=i.validator;var p=r(0);t.JsonPatchError=p.PatchError,t.deepClone=p._deepClone,t.escapePathComponent=p.escapePathComponent,t.unescapePathComponent=p.unescapePathComponent;var u=new WeakMap,s=function(e){this.observers=new Map,this.obj=e},c=function(e,t){this.callback=e,this.observer=t};function f(e,t){t.unobserve()}function l(e,t){var r,n=function(e){return u.get(e)}(e);if(n){var a=function(e,t){return e.observers.get(t)}(n,t);r=a&&a.observer}else n=new s(e),u.set(e,n);if(r)return r;if(r={},n.value=o._deepClone(e),t){r.callback=t,r.next=null;var i=function(){h(r)},p=function(){clearTimeout(r.next),r.next=setTimeout(i)};"undefined"!=typeof window&&(window.addEventListener("mouseup",p),window.addEventListener("keyup",p),window.addEventListener("mousedown",p),window.addEventListener("keydown",p),window.addEventListener("change",p))}return r.patches=[],r.object=e,r.unobserve=function(){h(r),clearTimeout(r.next),function(e,t){e.observers.delete(t.callback)}(n,r),"undefined"!=typeof window&&(window.removeEventListener("mouseup",p),window.removeEventListener("keyup",p),window.removeEventListener("mousedown",p),window.removeEventListener("keydown",p),window.removeEventListener("change",p))},n.observers.set(t,new c(t,r)),r}function h(e,t){void 0===t&&(t=!1);var r=u.get(e.object);d(r.value,e.object,e.patches,"",t),e.patches.length&&a.applyPatch(r.value,e.patches);var n=e.patches;return n.length>0&&(e.patches=[],e.callback&&e.callback(n)),n}function d(e,t,r,n,a){if(t!==e){"function"==typeof t.toJSON&&(t=t.toJSON());for(var i=o._objectKeys(t),p=o._objectKeys(e),u=!1,s=p.length-1;s>=0;s--){var c=e[l=p[s]];if(!o.hasOwnProperty(t,l)||void 0===t[l]&&void 0!==c&&!1===Array.isArray(t))Array.isArray(e)===Array.isArray(t)?(a&&r.push({op:"test",path:n+"/"+o.escapePathComponent(l),value:o._deepClone(c)}),r.push({op:"remove",path:n+"/"+o.escapePathComponent(l)}),u=!0):(a&&r.push({op:"test",path:n,value:e}),r.push({op:"replace",path:n,value:t}),!0);else{var f=t[l];"object"==typeof c&&null!=c&&"object"==typeof f&&null!=f?d(c,f,r,n+"/"+o.escapePathComponent(l),a):c!==f&&(!0,a&&r.push({op:"test",path:n+"/"+o.escapePathComponent(l),value:o._deepClone(c)}),r.push({op:"replace",path:n+"/"+o.escapePathComponent(l),value:o._deepClone(f)}))}}if(u||i.length!=p.length)for(s=0;s<i.length;s++){var l=i[s];o.hasOwnProperty(e,l)||void 0===t[l]||r.push({op:"add",path:n+"/"+o.escapePathComponent(l),value:o._deepClone(t[l])})}}}function v(e,t,r){void 0===r&&(r=!1);var n=[];return d(e,t,n,"",r),n}t.unobserve=f,t.observe=l,t.generate=h,t.compare=v;var y=r(1),w=r(0);t.default=n({},y,{unobserve:f,observe:l,generate:h,compare:v,JsonPatchError:w.PatchError,deepClone:o._deepClone,escapePathComponent:o.escapePathComponent,unescapePathComponent:w.unescapePathComponent})},function(e,t,r){"use strict";var n=Array.isArray,o=Object.keys,a=Object.prototype.hasOwnProperty;e.exports=function e(t,r){if(t===r)return!0;if(t&&r&&"object"==typeof t&&"object"==typeof r){var i,p,u,s=n(t),c=n(r);if(s&&c){if((p=t.length)!=r.length)return!1;for(i=p;0!=i--;)if(!e(t[i],r[i]))return!1;return!0}if(s!=c)return!1;var f=t instanceof Date,l=r instanceof Date;if(f!=l)return!1;if(f&&l)return t.getTime()==r.getTime();var h=t instanceof RegExp,d=r instanceof RegExp;if(h!=d)return!1;if(h&&d)return t.toString()==r.toString();var v=o(t);if((p=v.length)!==o(r).length)return!1;for(i=p;0!=i--;)if(!a.call(r,v[i]))return!1;for(i=p;0!=i--;)if(!e(t[u=v[i]],r[u]))return!1;return!0}return t!=t&&r!=r}}]);
var n=r(0),o=r(1),a=new WeakMap,i=function(e){this.observers=new Map,this.obj=e},p=function(e,t){this.callback=e,this.observer=t};function u(e,t){void 0===t&&(t=!1);var r=a.get(e.object);s(r.value,e.object,e.patches,"",t),e.patches.length&&o.applyPatch(r.value,e.patches);var n=e.patches;return n.length>0&&(e.patches=[],e.callback&&e.callback(n)),n}function s(e,t,r,o,a){if(t!==e){"function"==typeof t.toJSON&&(t=t.toJSON());for(var i=n._objectKeys(t),p=n._objectKeys(e),u=!1,c=p.length-1;c>=0;c--){var f=e[h=p[c]];if(!n.hasOwnProperty(t,h)||void 0===t[h]&&void 0!==f&&!1===Array.isArray(t))Array.isArray(e)===Array.isArray(t)?(a&&r.push({op:"test",path:o+"/"+n.escapePathComponent(h),value:n._deepClone(f)}),r.push({op:"remove",path:o+"/"+n.escapePathComponent(h)}),u=!0):(a&&r.push({op:"test",path:o,value:e}),r.push({op:"replace",path:o,value:t}),!0);else{var l=t[h];"object"==typeof f&&null!=f&&"object"==typeof l&&null!=l?s(f,l,r,o+"/"+n.escapePathComponent(h),a):f!==l&&(!0,a&&r.push({op:"test",path:o+"/"+n.escapePathComponent(h),value:n._deepClone(f)}),r.push({op:"replace",path:o+"/"+n.escapePathComponent(h),value:n._deepClone(l)}))}}if(u||i.length!=p.length)for(c=0;c<i.length;c++){var h=i[c];n.hasOwnProperty(e,h)||void 0===t[h]||r.push({op:"add",path:o+"/"+n.escapePathComponent(h),value:n._deepClone(t[h])})}}}t.unobserve=function(e,t){t.unobserve()},t.observe=function(e,t){var r,o=function(e){return a.get(e)}(e);if(o){var s=function(e,t){return e.observers.get(t)}(o,t);r=s&&s.observer}else o=new i(e),a.set(e,o);if(r)return r;if(r={},o.value=n._deepClone(e),t){r.callback=t,r.next=null;var c=function(){u(r)},f=function(){clearTimeout(r.next),r.next=setTimeout(c)};"undefined"!=typeof window&&(window.addEventListener("mouseup",f),window.addEventListener("keyup",f),window.addEventListener("mousedown",f),window.addEventListener("keydown",f),window.addEventListener("change",f))}return r.patches=[],r.object=e,r.unobserve=function(){u(r),clearTimeout(r.next),function(e,t){e.observers.delete(t.callback)}(o,r),"undefined"!=typeof window&&(window.removeEventListener("mouseup",f),window.removeEventListener("keyup",f),window.removeEventListener("mousedown",f),window.removeEventListener("keydown",f),window.removeEventListener("change",f))},o.observers.set(t,new p(t,r)),r},t.generate=u,t.compare=function(e,t,r){void 0===r&&(r=!1);var n=[];return s(e,t,n,"",r),n}}]);
{
"name": "fast-json-patch",
"version": "2.2.1",
"version": "3.0.0-0",
"description": "Fast implementation of JSON-Patch (RFC-6902) with duplex (observe changes) capabilities",

@@ -25,7 +25,5 @@ "homepage": "https://github.com/Starcounter-Jack/JSON-Patch",

"license": "MIT",
"main": "lib/duplex.js",
"typings": "lib/duplex.d.ts",
"engines": {
"node": ">= 0.4.0"
},
"main": "index.js",
"module": "index.mjs",
"typings": "index.d.ts",
"devDependencies": {

@@ -36,7 +34,6 @@ "benchmark": "^2.1.4",

"chalk": "^2.4.2",
"event-target-shim": "^5.0.1",
"fast-deep-equal": "^2.0.1",
"http-server": "^0.11.1",
"jasmine": "^3.4.0",
"jsdom": "^15.1.1",
"jsonfile": "^5.0.0",
"request": "^2.88.0",

@@ -47,3 +44,2 @@ "sauce-connect-launcher": "^1.2.7",

"typescript": "~3.5.2",
"underscore": "^1.9.1",
"webpack": "^4.35.0",

@@ -53,5 +49,8 @@ "webpack-cli": "^3.3.5"

"scripts": {
"tsc": "tsc",
"version": "tsc && webpack && git add -A",
"build": "tsc && webpack",
"tsc": "npm run tsc-common && npm run tsc-module",
"tsc-common": "tsc",
"tsc-module": "tsc --module esnext --moduleResolution node --outDir \"module/\" && npm run tsc-to-mjs",
"tsc-to-mjs": "bash tsc-to-mjs.sh",
"version": "npm run tsc && webpack && git add -A",
"build": "npm run tsc && webpack",
"serve": "http-server -p 5000 --silent",

@@ -64,11 +63,8 @@ "tsc-watch": "tsc -w",

"test-typings": "tsc test/spec/typings/typingsSpec.ts",
"test-duplex": "jasmine DUPLEX=yes JASMINE_CONFIG_PATH=test/jasmine.json",
"test-core": "jasmine DUPLEX=no JASMINE_CONFIG_PATH=test/jasmine.json test/spec/jsonPatchTestsSpec.js test/spec/coreSpec.js test/spec/validateSpec.js",
"test-duplex": "node --experimental-modules jasmine-run.mjs test/**/*[sS]pec.mjs",
"test-core": "node --experimental-modules jasmine-run.mjs test/spec/jsonPatchTestsSpec.mjs test/spec/coreSpec.mjs test/spec/validateSpec.mjs",
"bench": "npm run bench-core && npm run bench-duplex",
"bench-core": "node test/spec/coreBenchmark.js",
"bench-duplex": "node test/spec/coreBenchmark.js DUPLEX=yes && node test/spec/duplexBenchmark.js"
},
"dependencies": {
"fast-deep-equal": "^2.0.1"
"bench-duplex": "node test/spec/coreBenchmark.js && node test/spec/duplexBenchmark.js"
}
}

@@ -9,12 +9,10 @@ JSON-Patch

With JSON-Patch, you can:
- **applyPatch** to apply patches
- **applyOperation** to apply single operations
- **apply** patches (arrays) and single operations on JS object
- **validate** a sequence of patches
- **observe** for changes (and generate patches when a change is detected)
- **compare** two objects (to obtain the difference).
- **observe** for changes and **generate** patches when a change is detected
- **compare** two objects to obtain the difference
Tested in IE11, Firefox, Chrome, Safari and Node.js
[![Sauce Test Status](https://saucelabs.com/browser-matrix/json-patch.svg)](https://travis-ci.org/Starcounter-Jack/JSON-Patch)
## Why you should use JSON-Patch

@@ -28,49 +26,16 @@

## Footprint
4 KB minified and gzipped (12 KB minified)
## Performance
##### [`add` benchmark](https://run.perf.zone/view/JSON-Patch-Add-Operation-1535541298893)
![image](https://user-images.githubusercontent.com/17054134/44784357-aa422480-ab8d-11e8-8a7e-037e692dd842.png)
##### [`replace` benchmark](https://run.perf.zone/view/JSON-Patch-Replace-Operation-1535540952263)
![image](https://user-images.githubusercontent.com/17054134/44784275-5fc0a800-ab8d-11e8-8a90-e87b8d5409d0.png)
Tested on 29.08.2018. Compared libraries:
- [Starcounter-Jack/JSON-Patch](https://www.npmjs.com/package/fast-json-patch) 2.0.6
- [bruth/jsonpatch-js](https://www.npmjs.com/package/json-patch) 0.7.0
- [dharmafly/jsonpatch.js](https://www.npmjs.com/package/jsonpatch) 3.0.1
- [jiff](https://www.npmjs.com/package/jiff) 0.7.3
- [RFC6902](https://www.npmjs.com/package/rfc6902) 2.4.0
We aim the tests to be fair. Our library puts performance as the #1 priority, while other libraries can have different priorities. If you'd like to update the benchmarks or add a library, please fork the [perf.zone](https://perf.zone) benchmarks linked above and open an issue to include new results.
## Features
* Allows you to apply patches on object trees for incoming traffic.
* Allows you to freely manipulate object trees and then generate patches for outgoing traffic.
* Tested in IE11, Firefox, Chrome, Safari and Node.js
## Install
Install the current version (and save it as a dependency):
[Download as ZIP](https://github.com/Starcounter-Jack/JSON-Patch/archive/master.zip) or install the current version using a package manager (and save it as a dependency):
### npm
```sh
$ npm install fast-json-patch --save
```
### bower
# NPM
npm install fast-json-patch --save
```sh
$ bower install fast-json-patch --save
# or Bower
bower install fast-json-patch --save
```
### [download as ZIP](https://github.com/Starcounter-Jack/JSON-Patch/archive/master.zip)
## Adding to your project

@@ -80,141 +45,49 @@

Include `dist/fast-json-patch.js`.
Load the bundled distribution script:
### In Node.js
Call require to get the instance:
```js
var jsonpatch = require('fast-json-patch')
```html
<script src="dist/fast-json-patch.min.js"></script>
```
Or use ES6 style:
In [browsers that support ECMAScript modules](https://caniuse.com/#feat=es6-module), the below code uses this library as a module:
```js
import { applyOperation } from 'fast-json-patch'
```html
<script type="module">
import * as jsonpatch from 'fast-json-patch/index.mjs';
import { applyOperation } from 'fast-json-patch/index.mjs';
</script>
```
You can also require all API functions individually, all jsonpatch functions can be used as pure functions:
### In Node.js
```js
const { applyOperation } = require('fast-json-patch');
```
In Node 12+ with `--experimental-modules` flag, the below code uses this library as an ECMAScript module:
## Usage
#### Applying patches:
```js
var document = { firstName: "Albert", contactDetails: { phoneNumbers: [] } };
var patch = [
{ op: "replace", path: "/firstName", value: "Joachim" },
{ op: "add", path: "/lastName", value: "Wester" },
{ op: "add", path: "/contactDetails/phoneNumbers/0", value: { number: "555-123" } }
];
document = jsonpatch.applyPatch(document, patch).newDocument;
// document == { firstName: "Joachim", lastName: "Wester", contactDetails: { phoneNumbers: [{number:"555-123"}] } };
import * as jsonpatch from 'fast-json-patch/index.mjs';
import { applyOperation } from 'fast-json-patch/index.mjs';
```
##### For apply individual operations you can use `applyOperation`
In Webpack (and most surely other bundlers based on Babel), the below code uses this library as an ECMAScript module:
`jsonpatch.applyOperation` accepts a single operation object instead of a sequence, and returns the object after applying the operation. It works with all the standard JSON patch operations (`add, replace, move, test, remove and copy`).
```js
var document = { firstName: "Albert", contactDetails: { phoneNumbers: [] } };
var operation = { op: "replace", path: "/firstName", value: "Joachim" };
document = jsonpatch.applyOperation(document, operation).newDocument;
// document == { firstName: "Joachim", contactDetails: { phoneNumbers: [] }}
import * as jsonpatch from 'fast-json-patch';
import { applyOperation } from 'fast-json-patch';
```
#### Using `applyReducer` with `reduce`
In standard Node, the below code uses this library as a CommonJS module:
If you have an array of operations, you can simple reduce them using `applyReducer` as your reducer:
```js
var document = { firstName: "Albert", contactDetails: { phoneNumbers: [ ] } };
var patch = [
{ op:"replace", path: "/firstName", value: "Joachim" },
{ op:"add", path: "/lastName", value: "Wester" },
{ op:"add", path: "/contactDetails/phoneNumbers/0", value: { number: "555-123" } }
];
var updatedDocument = patch.reduce(applyReducer, document);
// updatedDocument == { firstName:"Joachim", lastName:"Wester", contactDetails:{ phoneNumbers[ {number:"555-123"} ] } };
const { applyOperation } = require('fast-json-patch');
const applyOperation = require('fast-json-patch').applyOperation;
```
Generating patches:
## Directories
```js
var document = { firstName: "Joachim", lastName: "Wester", contactDetails: { phoneNumbers: [ { number:"555-123" }] } };
var observer = jsonpatch.observe(document);
document.firstName = "Albert";
document.contactDetails.phoneNumbers[0].number = "123";
document.contactDetails.phoneNumbers.push({ number:"456" });
var patch = jsonpatch.generate(observer);
// patch == [
// { op: "replace", path: "/firstName", value: "Albert"},
// { op: "replace", path: "/contactDetails/phoneNumbers/0/number", value: "123" },
// { op: "add", path: "/contactDetails/phoneNumbers/1", value: {number:"456"}}
// ];
```
Directories used in this package:
Generating patches with test operations for values in the first object:
- `dist/` - contains ES5 files for a Web browser
- `commonjs/` - contains CommonJS module and typings
- `module/` - contains ECMAScript module and typings
- `src/` - contains TypeScript source files
```js
var document = { firstName: "Joachim", lastName: "Wester", contactDetails: { phoneNumbers: [ { number:"555-123" }] } };
var observer = jsonpatch.observe(document);
document.firstName = "Albert";
document.contactDetails.phoneNumbers[0].number = "123";
document.contactDetails.phoneNumbers.push({ number:"456" });
var patch = jsonpatch.generate(observer, true);
// patch == [
// { op: "test", path: "/firstName", value: "Joachim"},
// { op: "replace", path: "/firstName", value: "Albert"},
// { op: "test", path: "/contactDetails/phoneNumbers/0/number", value: "555-123" },
// { op: "replace", path: "/contactDetails/phoneNumbers/0/number", value: "123" },
// { op: "add", path: "/contactDetails/phoneNumbers/1", value: {number:"456"}}
// ];
```
Comparing two object trees:
```js
var documentA = {user: {firstName: "Albert", lastName: "Einstein"}};
var documentB = {user: {firstName: "Albert", lastName: "Collins"}};
var diff = jsonpatch.compare(documentA, documentB);
//diff == [{op: "replace", path: "/user/lastName", value: "Collins"}]
```
Comparing two object trees with test operations for values in the first object:
```js
var documentA = {user: {firstName: "Albert", lastName: "Einstein"}};
var documentB = {user: {firstName: "Albert", lastName: "Collins"}};
var diff = jsonpatch.compare(documentA, documentB, true);
//diff == [
// {op: "test", path: "/user/lastName", value: "Einstein"},
// {op: "replace", path: "/user/lastName", value: "Collins"}
// ];
```
Validating a sequence of patches:
```js
var obj = {user: {firstName: "Albert"}};
var patches = [{op: "replace", path: "/user/firstName", value: "Albert"}, {op: "replace", path: "/user/lastName", value: "Einstein"}];
var errors = jsonpatch.validate(patches, obj);
if (errors.length == 0) {
//there are no errors!
}
else {
for (var i=0; i < errors.length; i++) {
if (!errors[i]) {
console.log("Valid patch at index", i, patches[i]);
}
else {
console.error("Invalid patch at index", i, errors[i], patches[i]);
}
}
}
```
## API

@@ -249,2 +122,15 @@

Example:
```js
var document = { firstName: "Albert", contactDetails: { phoneNumbers: [] } };
var patch = [
{ op: "replace", path: "/firstName", value: "Joachim" },
{ op: "add", path: "/lastName", value: "Wester" },
{ op: "add", path: "/contactDetails/phoneNumbers/0", value: { number: "555-123" } }
];
document = jsonpatch.applyPatch(document, patch).newDocument;
// document == { firstName: "Joachim", lastName: "Wester", contactDetails: { phoneNumbers: [{number:"555-123"}] } };
```
#### `function applyOperation<T>(document: T, operation: Operation, validateOperation: boolean | Validator<T> = false, mutateDocument: boolean = true, banPrototypeModifications: boolean = true, index: number = 0): OperationResult<T>`

@@ -271,2 +157,11 @@

Example:
```js
var document = { firstName: "Albert", contactDetails: { phoneNumbers: [] } };
var operation = { op: "replace", path: "/firstName", value: "Joachim" };
document = jsonpatch.applyOperation(document, operation).newDocument;
// document == { firstName: "Joachim", contactDetails: { phoneNumbers: [] }}
```
#### `jsonpatch.applyReducer<T>(document: T, operation: Operation, index: number): T`

@@ -282,2 +177,15 @@

Example:
```js
var document = { firstName: "Albert", contactDetails: { phoneNumbers: [ ] } };
var patch = [
{ op:"replace", path: "/firstName", value: "Joachim" },
{ op:"add", path: "/lastName", value: "Wester" },
{ op:"add", path: "/contactDetails/phoneNumbers/0", value: { number: "555-123" } }
];
var updatedDocument = patch.reduce(applyReducer, document);
// updatedDocument == { firstName:"Joachim", lastName:"Wester", contactDetails:{ phoneNumbers[ {number:"555-123"} ] } };
```
#### `jsonpatch.deepClone(value: any): any`

@@ -315,2 +223,36 @@

Example:
```js
var document = { firstName: "Joachim", lastName: "Wester", contactDetails: { phoneNumbers: [ { number:"555-123" }] } };
var observer = jsonpatch.observe(document);
document.firstName = "Albert";
document.contactDetails.phoneNumbers[0].number = "123";
document.contactDetails.phoneNumbers.push({ number:"456" });
var patch = jsonpatch.generate(observer);
// patch == [
// { op: "replace", path: "/firstName", value: "Albert"},
// { op: "replace", path: "/contactDetails/phoneNumbers/0/number", value: "123" },
// { op: "add", path: "/contactDetails/phoneNumbers/1", value: {number:"456"}}
// ];
```
Example of generating patches with test operations for values in the first object:
```js
var document = { firstName: "Joachim", lastName: "Wester", contactDetails: { phoneNumbers: [ { number:"555-123" }] } };
var observer = jsonpatch.observe(document);
document.firstName = "Albert";
document.contactDetails.phoneNumbers[0].number = "123";
document.contactDetails.phoneNumbers.push({ number:"456" });
var patch = jsonpatch.generate(observer, true);
// patch == [
// { op: "test", path: "/firstName", value: "Joachim"},
// { op: "replace", path: "/firstName", value: "Albert"},
// { op: "test", path: "/contactDetails/phoneNumbers/0/number", value: "555-123" },
// { op: "replace", path: "/contactDetails/phoneNumbers/0/number", value: "123" },
// { op: "add", path: "/contactDetails/phoneNumbers/1", value: {number:"456"}}
// ];
```
#### `jsonpatch.unobserve(document: any, observer: Observer): void`

@@ -328,2 +270,23 @@

Example:
```js
var documentA = {user: {firstName: "Albert", lastName: "Einstein"}};
var documentB = {user: {firstName: "Albert", lastName: "Collins"}};
var diff = jsonpatch.compare(documentA, documentB);
//diff == [{op: "replace", path: "/user/lastName", value: "Collins"}]
```
Example of comparing two object trees with test operations for values in the first object:
```js
var documentA = {user: {firstName: "Albert", lastName: "Einstein"}};
var documentB = {user: {firstName: "Albert", lastName: "Collins"}};
var diff = jsonpatch.compare(documentA, documentB, true);
//diff == [
// {op: "test", path: "/user/lastName", value: "Einstein"},
// {op: "replace", path: "/user/lastName", value: "Collins"}
// ];
```
#### `jsonpatch.validate(patch: Operation[], document?: any, validator?: Function): JsonPatchError`

@@ -361,2 +324,23 @@

Example:
```js
var obj = {user: {firstName: "Albert"}};
var patches = [{op: "replace", path: "/user/firstName", value: "Albert"}, {op: "replace", path: "/user/lastName", value: "Einstein"}];
var errors = jsonpatch.validate(patches, obj);
if (errors.length == 0) {
//there are no errors!
}
else {
for (var i=0; i < errors.length; i++) {
if (!errors[i]) {
console.log("Valid patch at index", i, patches[i]);
}
else {
console.error("Invalid patch at index", i, errors[i], patches[i]);
}
}
}
```
## `OperationResult` Type

@@ -416,4 +400,27 @@

## Footprint
4 KB minified and gzipped (12 KB minified)
## Performance
##### [`add` benchmark](https://run.perf.zone/view/JSON-Patch-Add-Operation-1535541298893)
![image](https://user-images.githubusercontent.com/17054134/44784357-aa422480-ab8d-11e8-8a7e-037e692dd842.png)
##### [`replace` benchmark](https://run.perf.zone/view/JSON-Patch-Replace-Operation-1535540952263)
![image](https://user-images.githubusercontent.com/17054134/44784275-5fc0a800-ab8d-11e8-8a90-e87b8d5409d0.png)
Tested on 29.08.2018. Compared libraries:
- [Starcounter-Jack/JSON-Patch](https://www.npmjs.com/package/fast-json-patch) 2.0.6
- [bruth/jsonpatch-js](https://www.npmjs.com/package/json-patch) 0.7.0
- [dharmafly/jsonpatch.js](https://www.npmjs.com/package/jsonpatch) 3.0.1
- [jiff](https://www.npmjs.com/package/jiff) 0.7.3
- [RFC6902](https://www.npmjs.com/package/rfc6902) 2.4.0
We aim the tests to be fair. Our library puts performance as the #1 priority, while other libraries can have different priorities. If you'd like to update the benchmarks or add a library, please fork the [perf.zone](https://perf.zone) benchmarks linked above and open an issue to include new results.
## License
MIT

@@ -25,3 +25,3 @@ var webpack = require('webpack');

{
entry: './lib/duplex.js',
entry: './index.js',
mode: 'production',

@@ -44,3 +44,3 @@ optimization: {

{
entry: './lib/duplex.js',
entry: './index.js',
mode: 'production',

@@ -66,2 +66,1 @@ output: {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc