Skip to content

Commit

Permalink
fix: typescript codegen changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Starcounter-Jack committed Mar 24, 2022
1 parent 5f04488 commit d7903fb
Show file tree
Hide file tree
Showing 6 changed files with 294 additions and 583 deletions.
21 changes: 11 additions & 10 deletions commonjs/core.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Object.defineProperty(exports, "__esModule", { value: true });
exports._areEquals = exports.validate = exports.validator = exports.applyReducer = exports.applyPatch = exports.applyOperation = exports.getValueByPointer = exports.deepClone = exports.JsonPatchError = void 0;
var helpers_js_1 = require("./helpers.js");
exports.JsonPatchError = helpers_js_1.PatchError;
exports.deepClone = helpers_js_1._deepClone;
Expand Down Expand Up @@ -30,7 +31,7 @@ var objOps = {
and is potentially unneeded */
var removed = getValueByPointer(document, this.path);
if (removed) {
removed = helpers_js_1._deepClone(removed);
removed = (0, helpers_js_1._deepClone)(removed);
}
var originalValue = applyOperation(document, { op: "remove", path: this.from }).removed;
applyOperation(document, { op: "add", path: this.path, value: originalValue });
Expand All @@ -39,7 +40,7 @@ var objOps = {
copy: function (obj, key, document) {
var valueToCopy = getValueByPointer(document, this.from);
// enforce copy by value so further operations don't affect source (see issue #177)
applyOperation(document, { op: "add", path: this.path, value: helpers_js_1._deepClone(valueToCopy) });
applyOperation(document, { op: "add", path: this.path, value: (0, helpers_js_1._deepClone)(valueToCopy) });
return { newDocument: document };
},
test: function (obj, key, document) {
Expand All @@ -53,7 +54,7 @@ var objOps = {
/* The operations applicable to an array. Many are the same as for the object */
var arrOps = {
add: function (arr, i, document) {
if (helpers_js_1.isInteger(i)) {
if ((0, helpers_js_1.isInteger)(i)) {
arr.splice(i, 0, this.value);
}
else { // array props
Expand Down Expand Up @@ -167,7 +168,7 @@ function applyOperation(document, operation, validateOperation, mutateDocument,
} /* END ROOT OPERATIONS */
else {
if (!mutateDocument) {
document = helpers_js_1._deepClone(document);
document = (0, helpers_js_1._deepClone)(document);
}
var path = operation.path || "";
var keys = path.split('/');
Expand All @@ -186,7 +187,7 @@ function applyOperation(document, operation, validateOperation, mutateDocument,
while (true) {
key = keys[t];
if (key && key.indexOf('~') != -1) {
key = helpers_js_1.unescapePathComponent(key);
key = (0, helpers_js_1.unescapePathComponent)(key);
}
if (banPrototypeModifications &&
(key == '__proto__' ||
Expand All @@ -212,10 +213,10 @@ function applyOperation(document, operation, validateOperation, mutateDocument,
key = obj.length;
}
else {
if (validateOperation && !helpers_js_1.isInteger(key)) {
if (validateOperation && !(0, 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_js_1.isInteger(key)) {
else if ((0, helpers_js_1.isInteger)(key)) {
key = ~~key;
}
}
Expand Down Expand Up @@ -272,7 +273,7 @@ function applyPatch(document, patch, validateOperation, mutateDocument, banProto
}
}
if (!mutateDocument) {
document = helpers_js_1._deepClone(document);
document = (0, helpers_js_1._deepClone)(document);
}
var results = new Array(patch.length);
for (var i = 0, length_1 = patch.length; i < length_1; i++) {
Expand Down Expand Up @@ -328,7 +329,7 @@ function validator(operation, index, document, existingPathFragment) {
else if ((operation.op === 'add' || operation.op === 'replace' || operation.op === 'test') && operation.value === undefined) {
throw new exports.JsonPatchError('Operation `value` property is not present (applicable in `add`, `replace` and `test` operations)', 'OPERATION_VALUE_REQUIRED', index, operation, document);
}
else if ((operation.op === 'add' || operation.op === 'replace' || operation.op === 'test') && helpers_js_1.hasUndefined(operation.value)) {
else if ((operation.op === 'add' || operation.op === 'replace' || operation.op === 'test') && (0, 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);
}
else if (document) {
Expand Down Expand Up @@ -368,7 +369,7 @@ function validate(sequence, document, externalValidator) {
}
if (document) {
//clone document and sequence so that we can safely try applying operations
applyPatch(helpers_js_1._deepClone(document), helpers_js_1._deepClone(sequence), externalValidator || true);
applyPatch((0, helpers_js_1._deepClone)(document), (0, helpers_js_1._deepClone)(sequence), externalValidator || true);
}
else {
externalValidator = externalValidator || validator;
Expand Down
25 changes: 13 additions & 12 deletions commonjs/duplex.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Object.defineProperty(exports, "__esModule", { value: true });
exports.compare = exports.generate = exports.observe = exports.unobserve = void 0;
/*!
* https://github.com/Starcounter-Jack/JSON-Patch
* (c) 2017-2021 Joachim Wester
Expand Down Expand Up @@ -56,7 +57,7 @@ function observe(obj, callback) {
return observer;
}
observer = {};
mirror.value = helpers_js_1._deepClone(obj);
mirror.value = (0, helpers_js_1._deepClone)(obj);
if (callback) {
observer.callback = callback;
observer.next = null;
Expand Down Expand Up @@ -101,7 +102,7 @@ function generate(observer, invertible) {
var mirror = beforeDict.get(observer.object);
_generate(mirror.value, observer.object, observer.patches, "", invertible);
if (observer.patches.length) {
core_js_1.applyPatch(mirror.value, observer.patches);
(0, core_js_1.applyPatch)(mirror.value, observer.patches);
}
var temp = observer.patches;
if (temp.length > 0) {
Expand All @@ -121,34 +122,34 @@ function _generate(mirror, obj, patches, path, invertible) {
if (typeof obj.toJSON === "function") {
obj = obj.toJSON();
}
var newKeys = helpers_js_1._objectKeys(obj);
var oldKeys = helpers_js_1._objectKeys(mirror);
var newKeys = (0, helpers_js_1._objectKeys)(obj);
var oldKeys = (0, helpers_js_1._objectKeys)(mirror);
var changed = false;
var deleted = false;
//if ever "move" operation is implemented here, make sure this test runs OK: "should not generate the same patch twice (move)"
for (var t = oldKeys.length - 1; t >= 0; t--) {
var key = oldKeys[t];
var oldVal = mirror[key];
if (helpers_js_1.hasOwnProperty(obj, key) && !(obj[key] === undefined && oldVal !== undefined && Array.isArray(obj) === false)) {
if ((0, 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 && Array.isArray(oldVal) === Array.isArray(newVal)) {
_generate(oldVal, newVal, patches, path + "/" + helpers_js_1.escapePathComponent(key), invertible);
_generate(oldVal, newVal, patches, path + "/" + (0, helpers_js_1.escapePathComponent)(key), invertible);
}
else {
if (oldVal !== newVal) {
changed = true;
if (invertible) {
patches.push({ op: "test", path: path + "/" + helpers_js_1.escapePathComponent(key), value: helpers_js_1._deepClone(oldVal) });
patches.push({ op: "test", path: path + "/" + (0, helpers_js_1.escapePathComponent)(key), value: (0, helpers_js_1._deepClone)(oldVal) });
}
patches.push({ op: "replace", path: path + "/" + helpers_js_1.escapePathComponent(key), value: helpers_js_1._deepClone(newVal) });
patches.push({ op: "replace", path: path + "/" + (0, helpers_js_1.escapePathComponent)(key), value: (0, helpers_js_1._deepClone)(newVal) });
}
}
}
else if (Array.isArray(mirror) === Array.isArray(obj)) {
if (invertible) {
patches.push({ op: "test", path: path + "/" + helpers_js_1.escapePathComponent(key), value: helpers_js_1._deepClone(oldVal) });
patches.push({ op: "test", path: path + "/" + (0, helpers_js_1.escapePathComponent)(key), value: (0, helpers_js_1._deepClone)(oldVal) });
}
patches.push({ op: "remove", path: path + "/" + helpers_js_1.escapePathComponent(key) });
patches.push({ op: "remove", path: path + "/" + (0, helpers_js_1.escapePathComponent)(key) });
deleted = true; // property has been deleted
}
else {
Expand All @@ -164,8 +165,8 @@ function _generate(mirror, obj, patches, path, invertible) {
}
for (var t = 0; t < newKeys.length; t++) {
var key = newKeys[t];
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]) });
if (!(0, helpers_js_1.hasOwnProperty)(mirror, key) && obj[key] !== undefined) {
patches.push({ op: "add", path: path + "/" + (0, helpers_js_1.escapePathComponent)(key), value: (0, helpers_js_1._deepClone)(obj[key]) });
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions commonjs/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ 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]; };
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.PatchError = exports.hasUndefined = exports.getPath = exports._getPathRecursive = exports.unescapePathComponent = exports.escapePathComponent = exports.isInteger = exports._deepClone = exports._objectKeys = exports.hasOwnProperty = void 0;
var _hasOwnProperty = Object.prototype.hasOwnProperty;
function hasOwnProperty(obj, key) {
return _hasOwnProperty.call(obj, key);
Expand Down Expand Up @@ -122,7 +125,7 @@ function getPath(root, obj) {
if (path === '') {
throw new Error("Object not found in root");
}
return "/" + path;
return "/".concat(path);
}
exports.getPath = getPath;
/**
Expand Down Expand Up @@ -158,7 +161,7 @@ function patchErrorMessageFormatter(message, args) {
for (var key in args) {
var 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("".concat(key, ": ").concat(value));
}
}
return messageParts.join('\n');
Expand Down
8 changes: 5 additions & 3 deletions module/helpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ 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]; };
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
Expand Down Expand Up @@ -114,7 +116,7 @@ export function getPath(root, obj) {
if (path === '') {
throw new Error("Object not found in root");
}
return "/" + path;
return "/".concat(path);
}
/**
* Recursively checks whether an object has any undefined values inside.
Expand Down Expand Up @@ -148,7 +150,7 @@ function patchErrorMessageFormatter(message, args) {
for (var key in args) {
var 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("".concat(key, ": ").concat(value));
}
}
return messageParts.join('\n');
Expand Down

0 comments on commit d7903fb

Please sign in to comment.