@stackmeister/json-patch
Advanced tools
| export type { JsonAddOperation, JsonCopyOperation, JsonMoveOperation, JsonOperation, JsonPatch, JsonPatchError, JsonRemoveOperation, JsonReplaceOperation, JsonTestError, JsonTestOperation, } from './patches'; | ||
| export { applyMutation, applyOperation, applyPatch, createDraft, createPatch, add, replace, remove, move, copy, test, } from './patches'; |
+72
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { | ||
| value: true | ||
| }); | ||
| Object.defineProperty(exports, "add", { | ||
| enumerable: true, | ||
| get: function get() { | ||
| return _patches.add; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "applyMutation", { | ||
| enumerable: true, | ||
| get: function get() { | ||
| return _patches.applyMutation; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "applyOperation", { | ||
| enumerable: true, | ||
| get: function get() { | ||
| return _patches.applyOperation; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "applyPatch", { | ||
| enumerable: true, | ||
| get: function get() { | ||
| return _patches.applyPatch; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "copy", { | ||
| enumerable: true, | ||
| get: function get() { | ||
| return _patches.copy; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createDraft", { | ||
| enumerable: true, | ||
| get: function get() { | ||
| return _patches.createDraft; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createPatch", { | ||
| enumerable: true, | ||
| get: function get() { | ||
| return _patches.createPatch; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "move", { | ||
| enumerable: true, | ||
| get: function get() { | ||
| return _patches.move; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "remove", { | ||
| enumerable: true, | ||
| get: function get() { | ||
| return _patches.remove; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "replace", { | ||
| enumerable: true, | ||
| get: function get() { | ||
| return _patches.replace; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "test", { | ||
| enumerable: true, | ||
| get: function get() { | ||
| return _patches.test; | ||
| } | ||
| }); | ||
| var _patches = require("./patches"); |
+179
| import type { JsonPointer } from '@stackmeister/json-pointer'; | ||
| /** | ||
| * An operation to add a new property in an object or a new value to an array. | ||
| */ | ||
| export declare type JsonAddOperation<Value = unknown> = { | ||
| readonly op: 'add'; | ||
| readonly path: JsonPointer; | ||
| readonly value: Value; | ||
| }; | ||
| /** | ||
| * An action to remove a property in an object or an index in an array. | ||
| * | ||
| * In arrays, elements with higher indexes will move down one index | ||
| */ | ||
| export declare type JsonRemoveOperation = { | ||
| readonly op: 'remove'; | ||
| readonly path: JsonPointer; | ||
| }; | ||
| /** | ||
| * An action to replace a property in an object or an index in an array. | ||
| * | ||
| * In arrays, this operation will throw an error if the element doesn't exist. | ||
| */ | ||
| export declare type JsonReplaceOperation<Value = unknown> = { | ||
| readonly op: 'replace'; | ||
| readonly path: JsonPointer; | ||
| readonly value: Value; | ||
| }; | ||
| /** | ||
| * An action to value from one property or index to another. | ||
| * | ||
| * Equal to a remove() and add() operation on the path and value. | ||
| */ | ||
| export declare type JsonMoveOperation = { | ||
| readonly op: 'move'; | ||
| readonly from: JsonPointer; | ||
| readonly path: JsonPointer; | ||
| }; | ||
| /** | ||
| * An action to copy a value from one property or index to another. | ||
| */ | ||
| export declare type JsonCopyOperation = { | ||
| readonly op: 'copy'; | ||
| readonly from: JsonPointer; | ||
| readonly path: JsonPointer; | ||
| }; | ||
| /** | ||
| * An action to test if a value equals a given value. | ||
| * | ||
| * @see {JsonTestError} | ||
| */ | ||
| export declare type JsonTestOperation<Value = unknown> = { | ||
| readonly op: 'test'; | ||
| readonly path: JsonPointer; | ||
| readonly value: Value; | ||
| }; | ||
| /** | ||
| * An operation that can be performed in a JSON-Patch. | ||
| */ | ||
| export declare type JsonOperation = JsonAddOperation | JsonRemoveOperation | JsonReplaceOperation | JsonMoveOperation | JsonCopyOperation | JsonTestOperation; | ||
| /** | ||
| * A JSON-Patch document. | ||
| * | ||
| * @see {JsonOperation} | ||
| */ | ||
| export declare type JsonPatch = JsonOperation[]; | ||
| /** | ||
| * An error that occurs during JSON-Patch application or value observation. | ||
| */ | ||
| export declare class JsonPatchError extends Error { | ||
| } | ||
| /** | ||
| * An error that occurs when a test-operation failed. | ||
| * | ||
| * @see {JsonTestOperation} | ||
| */ | ||
| export declare class JsonTestError extends JsonPatchError { | ||
| } | ||
| /** | ||
| * Creates an JSON add operation. | ||
| * | ||
| * @param path the path to add the value to. | ||
| * @param value the value to add. | ||
| * @returns the add operation. | ||
| */ | ||
| export declare const add: <Value = unknown>(path: JsonPointer, value: Value) => JsonAddOperation<Value>; | ||
| /** | ||
| * Creates a JSON remove operation. | ||
| * | ||
| * @param path the path to remove. | ||
| * @returns the remove operation. | ||
| */ | ||
| export declare const remove: (path: JsonPointer) => JsonRemoveOperation; | ||
| /** | ||
| * Creates a JSON replace operation. | ||
| * | ||
| * @param path the path to replace the value of. | ||
| * @param value the value to place. | ||
| * @returns the replace operation. | ||
| */ | ||
| export declare const replace: <Value = unknown>(path: JsonPointer, value: Value) => JsonReplaceOperation<Value>; | ||
| /** | ||
| * Creates a JSON move operation. | ||
| * | ||
| * @param from the path to move the value from. | ||
| * @param path the path to move the value to. | ||
| * @returns the move operation. | ||
| */ | ||
| export declare const move: (from: JsonPointer, path: JsonPointer) => JsonMoveOperation; | ||
| /** | ||
| * Creates a JSON copy operation. | ||
| * | ||
| * @param from the path to copy the value from. | ||
| * @param path the path to copy the value to. | ||
| * @returns the copy operation. | ||
| */ | ||
| export declare const copy: (from: JsonPointer, path: JsonPointer) => JsonCopyOperation; | ||
| /** | ||
| * Creates a JSON test operation. | ||
| * | ||
| * @param path the path to test. | ||
| * @param value the value the path's value needs to equal. | ||
| * @returns the test operation. | ||
| */ | ||
| export declare const test: <Value = unknown>(path: JsonPointer, value: Value) => JsonTestOperation<Value>; | ||
| /** | ||
| * Applies a single JSON Patch operation to a value. | ||
| * | ||
| * Can be perfectly used with Array.prototype.reduce on an array of operations. | ||
| * | ||
| * @param value the value to apply the operation on. | ||
| * @param operation the operation to apply. | ||
| * @returns the patched value. | ||
| */ | ||
| export declare const applyOperation: <Value>(value: Value, operation: JsonOperation) => Value; | ||
| /** | ||
| * Applies a JsonPatch document to a value. | ||
| * | ||
| * @param value the value to apply the document on. | ||
| * @param document the JSON Patch document to apply. | ||
| * @returns the patched value. | ||
| */ | ||
| export declare const applyPatch: <Value>(value: Value, document: JsonPatch) => Value; | ||
| /** | ||
| * Creates an observed draft of a value. | ||
| * | ||
| * Modifying the draft will result in onOperation being triggered with respective | ||
| * JSON Patch operations for the mutations done. | ||
| * | ||
| * @param value the value to observe. | ||
| * @param onOperation the handler when a JSON Operation is triggered by mutation. | ||
| * @returns the draft value. | ||
| */ | ||
| export declare const createDraft: <Value extends unknown[] | Record<string, unknown>>(value: Value, onOperation: (operation: JsonOperation) => void) => Value; | ||
| /** | ||
| * Creates a patch from a set of mutations on a value. | ||
| * | ||
| * The passed function receives a draft of the value and can | ||
| * mutate it as needed. For every mutation done on the draft, | ||
| * a JSON Patch operation will be created. | ||
| * | ||
| * After all mutations are done, you will receive a full JSON Patch document | ||
| * for the respective mutations done. | ||
| * | ||
| * @param value the value to create a patch for. | ||
| * @param mutate a function that mutates the value. | ||
| * @returns the patch for the mutations done on the value. | ||
| */ | ||
| export declare const createPatch: <Value extends unknown[] | Record<string, unknown>>(value: Value, mutate: (value: Value) => void) => JsonPatch; | ||
| /** | ||
| * Applies a set of mutations on a value and returns a new value with these mutations applied. | ||
| * | ||
| * This can be used to immutably modify values with a mutable API. | ||
| * | ||
| * @param value the value to mutate. | ||
| * @param mutate a function that mutates the value. | ||
| * @returns a new value with the done mutations applied. | ||
| */ | ||
| export declare const applyMutation: <Value extends unknown[] | Record<string, unknown>>(value: Value, mutate: (value: Value) => void) => Value; |
+370
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { | ||
| value: true | ||
| }); | ||
| exports.test = exports.replace = exports.remove = exports.move = exports.createPatch = exports.createDraft = exports.copy = exports.applyPatch = exports.applyOperation = exports.applyMutation = exports.add = exports.JsonTestError = exports.JsonPatchError = void 0; | ||
| var _equals = _interopRequireDefault(require("@stackmeister/equals")); | ||
| var _jsonPointer = require("@stackmeister/json-pointer"); | ||
| var _types = require("@stackmeister/types"); | ||
| function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
| function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } | ||
| function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | ||
| function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | ||
| function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | ||
| function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | ||
| function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } | ||
| function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } | ||
| function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } | ||
| function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } | ||
| function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
| function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
| function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } | ||
| function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
| function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } | ||
| function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } | ||
| function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } | ||
| function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } | ||
| function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); } | ||
| function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct.bind(); } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); } | ||
| function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } | ||
| function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; } | ||
| function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } | ||
| function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } | ||
| /** | ||
| * An error that occurs during JSON-Patch application or value observation. | ||
| */ | ||
| var JsonPatchError = /*#__PURE__*/function (_Error) { | ||
| _inherits(JsonPatchError, _Error); | ||
| var _super = _createSuper(JsonPatchError); | ||
| function JsonPatchError() { | ||
| _classCallCheck(this, JsonPatchError); | ||
| return _super.apply(this, arguments); | ||
| } | ||
| return _createClass(JsonPatchError); | ||
| }( /*#__PURE__*/_wrapNativeSuper(Error)); | ||
| /** | ||
| * An error that occurs when a test-operation failed. | ||
| * | ||
| * @see {JsonTestOperation} | ||
| */ | ||
| exports.JsonPatchError = JsonPatchError; | ||
| var JsonTestError = /*#__PURE__*/function (_JsonPatchError) { | ||
| _inherits(JsonTestError, _JsonPatchError); | ||
| var _super2 = _createSuper(JsonTestError); | ||
| function JsonTestError() { | ||
| _classCallCheck(this, JsonTestError); | ||
| return _super2.apply(this, arguments); | ||
| } | ||
| return _createClass(JsonTestError); | ||
| }(JsonPatchError); | ||
| /** | ||
| * Creates an JSON add operation. | ||
| * | ||
| * @param path the path to add the value to. | ||
| * @param value the value to add. | ||
| * @returns the add operation. | ||
| */ | ||
| exports.JsonTestError = JsonTestError; | ||
| var add = function add(path, value) { | ||
| return { | ||
| op: 'add', | ||
| path: path, | ||
| value: value | ||
| }; | ||
| }; | ||
| /** | ||
| * Creates a JSON remove operation. | ||
| * | ||
| * @param path the path to remove. | ||
| * @returns the remove operation. | ||
| */ | ||
| exports.add = add; | ||
| var remove = function remove(path) { | ||
| return { | ||
| op: 'remove', | ||
| path: path | ||
| }; | ||
| }; | ||
| /** | ||
| * Creates a JSON replace operation. | ||
| * | ||
| * @param path the path to replace the value of. | ||
| * @param value the value to place. | ||
| * @returns the replace operation. | ||
| */ | ||
| exports.remove = remove; | ||
| var replace = function replace(path, value) { | ||
| return { | ||
| op: 'replace', | ||
| path: path, | ||
| value: value | ||
| }; | ||
| }; | ||
| /** | ||
| * Creates a JSON move operation. | ||
| * | ||
| * @param from the path to move the value from. | ||
| * @param path the path to move the value to. | ||
| * @returns the move operation. | ||
| */ | ||
| exports.replace = replace; | ||
| var move = function move(from, path) { | ||
| return { | ||
| op: 'move', | ||
| from: from, | ||
| path: path | ||
| }; | ||
| }; | ||
| /** | ||
| * Creates a JSON copy operation. | ||
| * | ||
| * @param from the path to copy the value from. | ||
| * @param path the path to copy the value to. | ||
| * @returns the copy operation. | ||
| */ | ||
| exports.move = move; | ||
| var copy = function copy(from, path) { | ||
| return { | ||
| op: 'copy', | ||
| from: from, | ||
| path: path | ||
| }; | ||
| }; | ||
| /** | ||
| * Creates a JSON test operation. | ||
| * | ||
| * @param path the path to test. | ||
| * @param value the value the path's value needs to equal. | ||
| * @returns the test operation. | ||
| */ | ||
| exports.copy = copy; | ||
| var test = function test(path, value) { | ||
| return { | ||
| op: 'test', | ||
| path: path, | ||
| value: value | ||
| }; | ||
| }; | ||
| /** | ||
| * Applies a single JSON Patch operation to a value. | ||
| * | ||
| * Can be perfectly used with Array.prototype.reduce on an array of operations. | ||
| * | ||
| * @param value the value to apply the operation on. | ||
| * @param operation the operation to apply. | ||
| * @returns the patched value. | ||
| */ | ||
| exports.test = test; | ||
| var applyOperation = function applyOperation(value, operation) { | ||
| switch (operation.op) { | ||
| case 'add': | ||
| return (0, _jsonPointer.set)(operation.path, value, operation.value); | ||
| case 'remove': | ||
| return (0, _jsonPointer.remove)(operation.path, value); | ||
| case 'replace': | ||
| return (0, _jsonPointer.set)(operation.path, value, operation.value); | ||
| case 'move': | ||
| { | ||
| var targetValue = (0, _jsonPointer.get)(operation.from, value); | ||
| return (0, _jsonPointer.set)(operation.path, (0, _jsonPointer.remove)(operation.from, value), targetValue); | ||
| } | ||
| case 'copy': | ||
| { | ||
| var _targetValue = (0, _jsonPointer.get)(operation.from, value); | ||
| return (0, _jsonPointer.set)(operation.path, value, _targetValue); | ||
| } | ||
| case 'test': | ||
| { | ||
| var _targetValue2 = (0, _jsonPointer.get)(operation.path, value); | ||
| if (!(0, _equals.default)(_targetValue2, operation.value)) { | ||
| throw new JsonTestError("Value at path ".concat(operation.path, " is not equal to test value.")); | ||
| } | ||
| return value; | ||
| } | ||
| } | ||
| }; | ||
| /** | ||
| * Applies a JsonPatch document to a value. | ||
| * | ||
| * @param value the value to apply the document on. | ||
| * @param document the JSON Patch document to apply. | ||
| * @returns the patched value. | ||
| */ | ||
| exports.applyOperation = applyOperation; | ||
| var applyPatch = function applyPatch(value, document) { | ||
| return document.reduce(applyOperation, value); | ||
| }; | ||
| exports.applyPatch = applyPatch; | ||
| var createObservedArray = function createObservedArray(value, options) { | ||
| var draft = value.map(function (itemValue, index) { | ||
| return createObserved(itemValue, _objectSpread(_objectSpread({}, options), {}, { | ||
| path: "".concat(options.path, "/").concat(index) | ||
| })); | ||
| }); | ||
| // Observe array keys | ||
| return new Proxy(draft, { | ||
| set: function set(target, key, newValue) { | ||
| if (_typeof(key) === 'symbol' || !(0, _types.isNumeric)(key)) { | ||
| throw new JsonPatchError("Untracked change of key ".concat(key.toString(), " on path ").concat(options.path, ".")); | ||
| } | ||
| var index = (0, _types.toInteger)(key); | ||
| var childPath = "".concat(options.path, "/").concat(index); | ||
| if (index >= value.length) { | ||
| throw new JsonPatchError("Array index at path ".concat(childPath, " is out of range. In JSON-Patch drafts you should only edit existing array keys and create new ones by using Array.prototype.push().")); | ||
| } | ||
| options.onOperation(replace(childPath, newValue)); | ||
| return Reflect.set(target, index, newValue); | ||
| }, | ||
| get: function get(target, key, receiver) { | ||
| switch (key) { | ||
| case 'push': | ||
| { | ||
| return function () { | ||
| for (var _len = arguments.length, newValues = new Array(_len), _key = 0; _key < _len; _key++) { | ||
| newValues[_key] = arguments[_key]; | ||
| } | ||
| Reflect.get(target, key, receiver).apply(target, newValues); | ||
| newValues.forEach(function (newValue) { | ||
| return options.onOperation(add("".concat(options.path, "/-"), newValue)); | ||
| }); | ||
| }; | ||
| } | ||
| case 'splice': | ||
| case 'copyWithin': | ||
| case 'fill': | ||
| case 'unshift': | ||
| case 'reverse': | ||
| case 'sort': | ||
| { | ||
| return function () { | ||
| for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
| args[_key2] = arguments[_key2]; | ||
| } | ||
| Reflect.get(target, key, receiver).apply(target, args); | ||
| options.onOperation(replace(options.path, target)); | ||
| }; | ||
| } | ||
| } | ||
| return Reflect.get(target, key, receiver); | ||
| } | ||
| }); | ||
| }; | ||
| var createObservedObject = function createObservedObject(value, options) { | ||
| var draft = Object.fromEntries(Object.entries(value).map(function (_ref) { | ||
| var _ref2 = _slicedToArray(_ref, 2), | ||
| key = _ref2[0], | ||
| keyValue = _ref2[1]; | ||
| return [key, createObserved(keyValue, _objectSpread(_objectSpread({}, options), {}, { | ||
| path: "".concat(options.path, "/").concat(key) | ||
| }))]; | ||
| })); | ||
| // Observe object keys | ||
| return new Proxy(draft, { | ||
| set: function set(target, key, newValue) { | ||
| if (_typeof(key) === 'symbol') { | ||
| throw new JsonPatchError("Untracked change of key ".concat(key.toString(), " on path ").concat(options.path, ".")); | ||
| } | ||
| var childPath = "".concat(options.path, "/").concat(key); | ||
| if ((0, _jsonPointer.get)(childPath, options.root, { | ||
| default: undefined | ||
| }) === undefined) { | ||
| options.onOperation(add("".concat(options.path, "/").concat(key), newValue)); | ||
| } else { | ||
| options.onOperation(replace("".concat(options.path, "/").concat(key), newValue)); | ||
| } | ||
| return Reflect.set(target, key, newValue); | ||
| }, | ||
| defineProperty: function defineProperty(target, key, descriptor) { | ||
| var _descriptor$value, _descriptor$get; | ||
| if (_typeof(key) === 'symbol') { | ||
| throw new JsonPatchError("Untracked addition of key ".concat(key.toString(), " on path ").concat(options.path, ".")); | ||
| } | ||
| options.onOperation(add("".concat(options.path, "/").concat(key), (_descriptor$value = descriptor.value) !== null && _descriptor$value !== void 0 ? _descriptor$value : (_descriptor$get = descriptor.get) === null || _descriptor$get === void 0 ? void 0 : _descriptor$get.call(descriptor))); | ||
| return Reflect.defineProperty(target, key, descriptor); | ||
| }, | ||
| deleteProperty: function deleteProperty(target, key) { | ||
| if (_typeof(key) === 'symbol') { | ||
| throw new JsonPatchError("Untracked deletion of key ".concat(key.toString(), " on path ").concat(options.path, ".")); | ||
| } | ||
| options.onOperation(remove("".concat(options.path, "/").concat(key))); | ||
| return Reflect.deleteProperty(target, key); | ||
| } | ||
| }); | ||
| }; | ||
| var createObserved = function createObserved(value, options) { | ||
| if ((0, _types.isArray)(value)) { | ||
| return createObservedArray(value, options); | ||
| } | ||
| if ((0, _types.isObject)(value)) { | ||
| return createObservedObject(value, options); | ||
| } | ||
| return value; | ||
| }; | ||
| /** | ||
| * Creates an observed draft of a value. | ||
| * | ||
| * Modifying the draft will result in onOperation being triggered with respective | ||
| * JSON Patch operations for the mutations done. | ||
| * | ||
| * @param value the value to observe. | ||
| * @param onOperation the handler when a JSON Operation is triggered by mutation. | ||
| * @returns the draft value. | ||
| */ | ||
| var createDraft = function createDraft(value, onOperation) { | ||
| return createObserved(value, { | ||
| path: '', | ||
| root: value, | ||
| onOperation: onOperation | ||
| }); | ||
| }; | ||
| /** | ||
| * Creates a patch from a set of mutations on a value. | ||
| * | ||
| * The passed function receives a draft of the value and can | ||
| * mutate it as needed. For every mutation done on the draft, | ||
| * a JSON Patch operation will be created. | ||
| * | ||
| * After all mutations are done, you will receive a full JSON Patch document | ||
| * for the respective mutations done. | ||
| * | ||
| * @param value the value to create a patch for. | ||
| * @param mutate a function that mutates the value. | ||
| * @returns the patch for the mutations done on the value. | ||
| */ | ||
| exports.createDraft = createDraft; | ||
| var createPatch = function createPatch(value, mutate) { | ||
| var collectedOperations = []; | ||
| var draft = createDraft(value, function (operation) { | ||
| return collectedOperations.push(operation); | ||
| }); | ||
| mutate(draft); | ||
| return collectedOperations; | ||
| }; | ||
| /** | ||
| * Applies a set of mutations on a value and returns a new value with these mutations applied. | ||
| * | ||
| * This can be used to immutably modify values with a mutable API. | ||
| * | ||
| * @param value the value to mutate. | ||
| * @param mutate a function that mutates the value. | ||
| * @returns a new value with the done mutations applied. | ||
| */ | ||
| exports.createPatch = createPatch; | ||
| var applyMutation = function applyMutation(value, mutate) { | ||
| var patch = createPatch(value, mutate); | ||
| return applyPatch(value, patch); | ||
| }; | ||
| exports.applyMutation = applyMutation; |
+2
-2
| { | ||
| "name": "@stackmeister/json-patch", | ||
| "version": "0.1.4", | ||
| "version": "0.1.5", | ||
| "license": "MIT", | ||
@@ -47,3 +47,3 @@ "main": "cjs/index.js", | ||
| }, | ||
| "gitHead": "10afb42327a036a31985be857f0a68c765e99a66" | ||
| "gitHead": "7cda2f247123d83cf1a4c697dc6748885d336561" | ||
| } |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Empty package
Supply chain riskPackage does not contain any code. It may be removed, is name squatting, or the result of a faulty package publish.
Found 1 instance in 1 package
30590
623.85%7
133.33%621
Infinity%