diff --git a/dist/fast-json-patch.js b/dist/fast-json-patch.js index d6ec0eb0..6b675460 100644 --- a/dist/fast-json-patch.js +++ b/dist/fast-json-patch.js @@ -108,7 +108,7 @@ var __extends = (this && this.__extends) || (function () { d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); -Object.defineProperty(exports, "__esModule", { value: true }); + var _hasOwnProperty = Object.prototype.hasOwnProperty; function hasOwnProperty(obj, key) { return _hasOwnProperty.call(obj, key); @@ -277,7 +277,7 @@ exports.PatchError = PatchError; /* 1 */ /***/ (function(module, exports, __webpack_require__) { -Object.defineProperty(exports, "__esModule", { value: true }); + var helpers_js_1 = __webpack_require__(0); exports.JsonPatchError = helpers_js_1.PatchError; exports.deepClone = helpers_js_1._deepClone; @@ -717,7 +717,7 @@ exports._areEquals = _areEquals; /* 2 */ /***/ (function(module, exports, __webpack_require__) { -Object.defineProperty(exports, "__esModule", { value: true }); + /*! * https://github.com/Starcounter-Jack/JSON-Patch * (c) 2017 Joachim Wester diff --git a/dist/fast-json-patch.min.js b/dist/fast-json-patch.min.js index aab6dc5a..be54040e 100644 --- a/dist/fast-json-patch.min.js +++ b/dist/fast-json-patch.min.js @@ -5,7 +5,7 @@ var jsonpatch=function(e){var t={};function r(n){if(t[n])return t[n].exports;var * (c) 2017 Joachim Wester * MIT license */ -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=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=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;s0)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=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=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;s0)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 obj.length) { throw new JsonPatchError("The specified index MUST NOT be greater than the number of elements in the array", "OPERATION_VALUE_OUT_OF_BOUNDS", index, operation, document); } - var returnValue = arrOps[operation.op].call(operation, obj, key, document); // Apply patch + const returnValue = arrOps[operation.op].call(operation, obj, key, document); // Apply patch if (returnValue.test === false) { throw new JsonPatchError("Test operation failed", 'TEST_OPERATION_FAILED', index, operation, document); } @@ -228,7 +224,7 @@ export function applyOperation(document, operation, validateOperation, mutateDoc key = unescapePathComponent(key); } if (t >= len) { - var returnValue = objOps[operation.op].call(operation, obj, key, document); // Apply patch + const returnValue = objOps[operation.op].call(operation, obj, key, document); // Apply patch if (returnValue.test === false) { throw new JsonPatchError("Test operation failed", 'TEST_OPERATION_FAILED', index, operation, document); } @@ -253,9 +249,7 @@ export function applyOperation(document, operation, validateOperation, mutateDoc * @param banPrototypeModifications Whether to ban modifications to `__proto__`, defaults to `true`. * @return An array of `{newDocument, result}` after the patch */ -export function applyPatch(document, patch, validateOperation, mutateDocument, banPrototypeModifications) { - if (mutateDocument === void 0) { mutateDocument = true; } - if (banPrototypeModifications === void 0) { banPrototypeModifications = true; } +export function applyPatch(document, patch, validateOperation, mutateDocument = true, banPrototypeModifications = true) { if (validateOperation) { if (!Array.isArray(patch)) { throw new JsonPatchError('Patch sequence must be an array', 'SEQUENCE_NOT_AN_ARRAY'); @@ -264,8 +258,8 @@ export function applyPatch(document, patch, validateOperation, mutateDocument, b if (!mutateDocument) { document = _deepClone(document); } - var results = new Array(patch.length); - for (var i = 0, length_1 = patch.length; i < length_1; i++) { + const results = new Array(patch.length); + for (let i = 0, length = patch.length; i < length; i++) { // we don't need to pass mutateDocument argument because if it was true, we already deep cloned the object, we'll just pass `true` results[i] = applyOperation(document, patch[i], validateOperation, true, banPrototypeModifications, i); document = results[i].newDocument; // in case root was replaced @@ -283,7 +277,7 @@ export function applyPatch(document, patch, validateOperation, mutateDocument, b * @return The updated document */ export function applyReducer(document, operation, index) { - var operationResult = applyOperation(document, operation); + const operationResult = applyOperation(document, operation); if (operationResult.test === false) { // failed test throw new JsonPatchError("Test operation failed", 'TEST_OPERATION_FAILED', index, operation, document); } diff --git a/module/duplex.mjs b/module/duplex.mjs index 74db144e..1ddcfeb9 100644 --- a/module/duplex.mjs +++ b/module/duplex.mjs @@ -10,20 +10,18 @@ export { applyOperation, applyPatch, applyReducer, getValueByPointer, validate, /* export some helpers */ export { PatchError as JsonPatchError, _deepClone as deepClone, escapePathComponent, unescapePathComponent } from './helpers.mjs'; var beforeDict = new WeakMap(); -var Mirror = /** @class */ (function () { - function Mirror(obj) { +class Mirror { + constructor(obj) { this.observers = new Map(); this.obj = obj; } - return Mirror; -}()); -var ObserverInfo = /** @class */ (function () { - function ObserverInfo(callback, observer) { +} +class ObserverInfo { + constructor(callback, observer) { this.callback = callback; this.observer = observer; } - return ObserverInfo; -}()); +} function getMirror(obj) { return beforeDict.get(obj); } @@ -51,7 +49,7 @@ export function observe(obj, callback) { beforeDict.set(obj, mirror); } else { - var observerInfo = getObserverFromMirror(mirror, callback); + const observerInfo = getObserverFromMirror(mirror, callback); observer = observerInfo && observerInfo.observer; } if (observer) { @@ -62,10 +60,10 @@ export function observe(obj, callback) { if (callback) { observer.callback = callback; observer.next = null; - var dirtyCheck = function () { + var dirtyCheck = () => { generate(observer); }; - var fastCheck = function () { + var fastCheck = () => { clearTimeout(observer.next); observer.next = setTimeout(dirtyCheck); }; @@ -79,7 +77,7 @@ export function observe(obj, callback) { } observer.patches = patches; observer.object = obj; - observer.unobserve = function () { + observer.unobserve = () => { generate(observer); clearTimeout(observer.next); removeObserverFromMirror(mirror, observer); @@ -97,8 +95,7 @@ export function observe(obj, callback) { /** * Generate an array of patches from an observer */ -export function generate(observer, invertible) { - if (invertible === void 0) { invertible = false; } +export function generate(observer, invertible = false) { var mirror = beforeDict.get(observer.object); _generate(mirror.value, observer.object, observer.patches, "", invertible); if (observer.patches.length) { @@ -153,9 +150,9 @@ function _generate(mirror, obj, patches, path, invertible) { } else { if (invertible) { - patches.push({ op: "test", path: path, value: mirror }); + patches.push({ op: "test", path, value: mirror }); } - patches.push({ op: "replace", path: path, value: obj }); + patches.push({ op: "replace", path, value: obj }); changed = true; } } @@ -172,8 +169,7 @@ function _generate(mirror, obj, patches, path, invertible) { /** * Create an array of patches from the differences in two objects */ -export function compare(tree1, tree2, invertible) { - if (invertible === void 0) { invertible = false; } +export function compare(tree1, tree2, invertible = false) { var patches = []; _generate(tree1, tree2, patches, '', invertible); return patches; diff --git a/module/helpers.mjs b/module/helpers.mjs index 10844fe8..91ef008b 100644 --- a/module/helpers.mjs +++ b/module/helpers.mjs @@ -3,20 +3,7 @@ * (c) 2017 Joachim Wester * MIT license */ -var __extends = (this && this.__extends) || (function () { - var extendStatics = function (d, b) { - extendStatics = Object.setPrototypeOf || - ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || - function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; - return extendStatics(d, b); - }; - return function (d, b) { - extendStatics(d, b); - function __() { this.constructor = d; } - d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); - }; -})(); -var _hasOwnProperty = Object.prototype.hasOwnProperty; +const _hasOwnProperty = Object.prototype.hasOwnProperty; export function hasOwnProperty(obj, key) { return _hasOwnProperty.call(obj, key); } @@ -144,28 +131,23 @@ export function hasUndefined(obj) { return false; } function patchErrorMessageFormatter(message, args) { - var messageParts = [message]; - for (var key in args) { - var value = typeof args[key] === 'object' ? JSON.stringify(args[key], null, 2) : args[key]; // pretty print + const messageParts = [message]; + for (const key in args) { + const value = typeof args[key] === 'object' ? JSON.stringify(args[key], null, 2) : args[key]; // pretty print if (typeof value !== 'undefined') { - messageParts.push(key + ": " + value); + messageParts.push(`${key}: ${value}`); } } return messageParts.join('\n'); } -var PatchError = /** @class */ (function (_super) { - __extends(PatchError, _super); - function PatchError(message, name, index, operation, tree) { - var _newTarget = this.constructor; - var _this = _super.call(this, patchErrorMessageFormatter(message, { name: name, index: index, operation: operation, tree: tree })) || this; - _this.name = name; - _this.index = index; - _this.operation = operation; - _this.tree = tree; - Object.setPrototypeOf(_this, _newTarget.prototype); // restore prototype chain, see https://stackoverflow.com/a/48342359 - _this.message = patchErrorMessageFormatter(message, { name: name, index: index, operation: operation, tree: tree }); - return _this; +export class PatchError extends Error { + constructor(message, name, index, operation, tree) { + super(patchErrorMessageFormatter(message, { name, index, operation, tree })); + this.name = name; + this.index = index; + this.operation = operation; + this.tree = tree; + Object.setPrototypeOf(this, new.target.prototype); // restore prototype chain, see https://stackoverflow.com/a/48342359 + this.message = patchErrorMessageFormatter(message, { name, index, operation, tree }); } - return PatchError; -}(Error)); -export { PatchError }; +} diff --git a/package.json b/package.json index 9e050ded..fc76d977 100644 --- a/package.json +++ b/package.json @@ -44,9 +44,10 @@ }, "scripts": { "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-common": "tsc && npm run tsc-to-cjs", + "tsc-module": "tsc --target es6 --module es6 --moduleResolution node --outDir \"module/\" && npm run tsc-to-mjs", "tsc-to-mjs": "bash tsc-to-mjs.sh", + "tsc-to-cjs": "bash tsc-to-cjs.sh", "version": "npm run tsc && webpack && git add -A", "build": "npm run tsc && webpack", "serve": "http-server -p 5000 --silent", diff --git a/tsc-to-cjs.sh b/tsc-to-cjs.sh new file mode 100644 index 00000000..ae735bdd --- /dev/null +++ b/tsc-to-cjs.sh @@ -0,0 +1,3 @@ +#!/bin/bash +cd lib +sed -i 's/Object\.defineProperty(exports, "__esModule", { value: true });/ /g' core.js duplex.js helpers.js \ No newline at end of file