### Install immutable-json-patch Source: https://github.com/josdejong/immutable-json-patch/blob/main/README.md Install the immutable-json-patch library using npm. This command downloads and installs the package and its dependencies into your project. ```bash $ npm install immutable-json-patch ``` -------------------------------- ### immutableJSONPatch Options Example Source: https://github.com/josdejong/immutable-json-patch/blob/main/README.md Example of using the `options` object with `before` and `after` hooks for the `immutableJSONPatch` function. These hooks allow intercepting operations and documents before and after they are applied. ```javascript const options = { before: (document, operation) => { console.log('before operation', { document, operation }) // return { document?: unknown, operation?: JSONPatchOperation } | undefined }, after: (document, operation, previousDocument) => { console.log('after operation', { document, operation, previousDocument }) // return document | undefined } } // When the `before` or `after` callback returns an object with altered `document`, this will be used to apply the operation. When and altered `operation` is returned from `before` in an object, this operation will be applied instead of the original operation. ``` -------------------------------- ### Build and Test Commands (npm) Source: https://github.com/josdejong/immutable-json-patch/blob/main/README.md Provides npm commands for installing dependencies, building the library for different formats (ESM, CommonJs, UMD), running unit tests, linting with BiomeJS, and executing comprehensive build and test sequences. ```bash $ npm install $ npm run build $ npm test $ npm run lint $ npm run build-and-test ``` -------------------------------- ### Use immutable-json-patch Example Source: https://github.com/josdejong/immutable-json-patch/blob/main/README.md Demonstrates applying JSON patch operations immutably and generating reverse operations. It shows how to replace, add, and remove properties from a JSON document and then revert these changes. ```javascript import { immutableJSONPatch, revertJSONPatch } from 'immutable-json-patch' const document = { baz: 'qux', foo: 'bar' } console.log('document', document) const operations = [ { op: 'replace', path: '/baz', value: 'boo' }, { op: 'add', path: '/hello', value: ['world'] }, { op: 'remove', path: '/foo' } ] console.log('operations', operations) const updatedDocument = immutableJSONPatch(document, operations) console.log('updatedDocument', updatedDocument) // updatedDocument = { // "baz": "boo", // "hello": ["world"] // } const reverseOperations = revertJSONPatch(document, operations) console.log('reverseOperations', reverseOperations) // reverseOperations = [ // { op: 'add', path: '/foo', value: 'bar' }, // { op: 'remove', path: '/hello' }, // { op: 'replace', path: '/baz', value: 'qux' } // ] const revertedDocument = immutableJSONPatch(updatedDocument, reverseOperations) console.log('revertedDocument', revertedDocument) // revertedDocument = { // "baz": "qux", // "foo": "bar" // } ``` -------------------------------- ### Apply and Revert JSON Patch with Immutable.js Source: https://github.com/josdejong/immutable-json-patch/blob/main/test-lib/apps/umdBrowserApp.html This snippet shows how to apply a JSON patch to an existing JSON object immutably and then revert the changes using the reverse patch operations. It utilizes the `immutableJSONPatch` function to apply patches and `revertJSONPatch` to generate the necessary operations for reverting. ```javascript const json = {} const operations = [ { op: 'add', path: '/hello', value: 'world' } ] const updatedJson = immutableJSONPatch.immutableJSONPatch(json, operations) document.body.appendChild(document.createTextNode('updated json:' + JSON.stringify(updatedJson))) document.body.appendChild(document.createElement('BR')) const reverseOperations = immutableJSONPatch.revertJSONPatch(json, operations) const revertedJson = immutableJSONPatch.immutableJSONPatch(updatedJson, reverseOperations) document.body.appendChild(document.createTextNode('reverted json:' + JSON.stringify(revertedJson))) document.body.appendChild(document.createElement('BR')) ``` -------------------------------- ### Immutable Data Manipulation Functions (TypeScript) Source: https://github.com/josdejong/immutable-json-patch/blob/main/README.md Enables immutable operations on JSON data, including getting, setting, updating, deleting, checking existence, and inserting values at specified paths. ```typescript declare function getIn(object: U, path: JSONPath) : T | undefined declare function setIn (object: U, path: JSONPath, value: V, createPath = false) : T declare function updateIn (object: T, path: JSONPath, transform: (value: U) => V) : T declare function deleteIn (object: U, path: JSONPath) : T declare function existsIn (document: T, path: JSONPath) : boolean declare function insertAt (document: T, path: JSONPath, value: U) : T declare function transform ( document: U, callback: (document: V, path: JSONPath) => W, path: JSONPath = [] ) : T ``` -------------------------------- ### Apply JSON Patch and Generate Reverse Operations Source: https://github.com/josdejong/immutable-json-patch/blob/main/docs/index.html This JavaScript code applies a JSON patch to a document and generates the operations needed to revert the changes. It parses input JSON and operations, uses `immutableJSONPatch` to apply the patch, and `revertJSONPatch` to get the reverse operations. Error handling is included for invalid inputs. ```javascript import { immutableJSONPatch, revertJSONPatch } from 'https://cdn.jsdelivr.net/npm/immutable-json-patch/+esm' function applyPatch () { try { const document = JSON.parse(jsonText.value) const operations = JSON.parse(operationsText.value) const updatedDocument = immutableJSONPatch(document, operations) const reverseOperations = revertJSONPatch(document, operations) updatedJsonText.value = JSON.stringify(updatedDocument, null, 2) reverseOperationsText.value = JSON.stringify(reverseOperations, null, 2) updatedJsonText.classList.remove('error') } catch (err) { updatedJsonText.value = err.message reverseOperationsText.value = '' updatedJsonText.classList.add('error') } } const jsonText = document.getElementById('json-text') const operationsText = document.getElementById('operations-text') const updatedJsonText = document.getElementById('updated-json-text') const reverseOperationsText = document.getElementById('reverse-operations-text') const applyPatchButton = document.getElementById('apply-patch') applyPatchButton.onclick = applyPatch applyPatch() ``` -------------------------------- ### Build and Test Source: https://github.com/josdejong/immutable-json-patch/blob/main/tools/how-to-publish.md Executes the build and test process for the project. This is a prerequisite before publishing to ensure the code is stable. ```bash npm run build-and-test ``` -------------------------------- ### Create and Push Version Tag Source: https://github.com/josdejong/immutable-json-patch/blob/main/tools/how-to-publish.md Creates a git tag for the new version and pushes the tag to the remote repository. This is crucial for version control and releases. ```bash git tag v1.2.3 git push --tags ``` -------------------------------- ### Publish to npm Source: https://github.com/josdejong/immutable-json-patch/blob/main/tools/how-to-publish.md Publishes the package to the npm registry. This makes the new version available to users. ```bash npm publish ``` -------------------------------- ### Checkout Develop Branch Source: https://github.com/josdejong/immutable-json-patch/blob/main/tools/how-to-publish.md Switches the current working branch back to 'develop' after the release process is complete. ```bash git checkout develop ``` -------------------------------- ### Merge Develop into Main Source: https://github.com/josdejong/immutable-json-patch/blob/main/tools/how-to-publish.md Merges the latest changes from the 'develop' branch into the 'main' branch, preparing for a release. ```bash git checkout main git merge develop ``` -------------------------------- ### Commit Changes Source: https://github.com/josdejong/immutable-json-patch/blob/main/tools/how-to-publish.md Stages and commits the updated CHANGELOG.md, package.json, and package-lock.json files. A descriptive commit message is used to record the version change. ```bash git add CHANGELOG.md package.json package-lock.json git commit -m "Publish v1.2.3" ``` -------------------------------- ### Push Changes Source: https://github.com/josdejong/immutable-json-patch/blob/main/tools/how-to-publish.md Pushes the local commits to the remote repository. ```bash git push ``` -------------------------------- ### Load immutable-json-patch (ESM) Source: https://github.com/josdejong/immutable-json-patch/blob/main/README.md Import the necessary functions, `immutableJSONPatch` and `revertJSONPatch`, from the 'immutable-json-patch' library using ECMAScript Module (ESM) syntax. ```javascript import { immutableJSONPatch, revertJSONPatch } from 'immutable-json-patch' ``` -------------------------------- ### Load immutable-json-patch (CommonJS) Source: https://github.com/josdejong/immutable-json-patch/blob/main/README.md Load the `immutableJSONPatch` and `revertJSONPatch` functions using the CommonJS module system's `require` function. ```javascript const { immutableJSONPatch, revertJSONPatch } = require('immutable-json-patch') ``` -------------------------------- ### JSON Pointer Utility Functions (TypeScript) Source: https://github.com/josdejong/immutable-json-patch/blob/main/README.md Provides functions for parsing, compiling, appending, and checking JSON pointers. It also includes typeguards to validate JSON Patch operations. ```typescript declare function parsePath(document: T, path: JSONPointer): JSONPath declare function parseFrom(path: JSONPointer): JSONPath declare function parseJSONPointer (pointer: JSONPointer) : JSONPath declare function compileJSONPointer (path: JSONPath) : JSONPointer declare function compileJSONPointerProp (pathProp: string | number) : JSONPointer declare function appendToJSONPointer (pointer: JSONPointer, pathProp: string | number) : JSONPointer declare function startsWithJSONPointer (pointer: JSONPointer, searchPointer: JSONPointer) : boolean declare function isJSONPatchOperation(operation: unknown): operation is JSONPatchOperation declare function isJSONPatchAdd(operation: unknown): operation is JSONPatchAdd declare function isJSONPatchRemove(operation: unknown): operation is JSONPatchRemove declare function isJSONPatchReplace(operation: unknown): operation is JSONPatchReplace declare function isJSONPatchCopy(operation: unknown): operation is JSONPatchCopy declare function isJSONPatchMove(operation: unknown): operation is JSONPatchMove declare function isJSONPatchTest(operation: unknown): operation is JSONPatchTest ``` -------------------------------- ### immutableJSONPatch API Signature Source: https://github.com/josdejong/immutable-json-patch/blob/main/README.md TypeScript signature for the `immutableJSONPatch` function. It takes a document, an array of operations, and optional options, returning the updated document. ```typescript declare function immutableJSONPatch (document: T, operations: JSONPatchDocument, options?: JSONPatchOptions) : U ``` -------------------------------- ### Apply and Revert JSON Patch - JavaScript Source: https://github.com/josdejong/immutable-json-patch/blob/main/test-lib/apps/esmBrowserApp.html Applies a JSON patch to an object and then reverts the changes using immutable operations. It demonstrates the `immutableJSONPatch` and `revertJSONPatch` functions. ```javascript import { immutableJSONPatch, revertJSONPatch } from '../../lib/esm/index.js' const json = {} const operations = [ { op: 'add', path: '/hello', value: 'world' } ] const updatedJson = immutableJSONPatch(json, operations) document.body.appendChild(document.createTextNode('updated json:' + JSON.stringify(updatedJson))) document.body.appendChild(document.createElement('BR')) const reverseOperations = revertJSONPatch(json, operations) const revertedJson = immutableJSONPatch(updatedJson, reverseOperations) document.body.appendChild(document.createTextNode('reverted json:' + JSON.stringify(revertedJson))) document.body.appendChild(document.createElement('BR')) ``` -------------------------------- ### revertJSONPatch API Signature Source: https://github.com/josdejong/immutable-json-patch/blob/main/README.md TypeScript signature for the `revertJSONPatch` function. It accepts a document, operations, and optional options to generate reverse operations. ```typescript declare function revertJSONPatch (document: T, operations: JSONPatchDocument, options?: RevertJSONPatchOptions) : JSONPatchDocument ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.