### Install fast-json-patch using npm Source: https://github.com/starcounter-jack/json-patch/blob/master/README.md Instructions for installing the fast-json-patch library using the npm package manager and saving it as a project dependency. ```Shell npm install fast-json-patch --save ``` -------------------------------- ### Install Dependencies Shell Source: https://github.com/starcounter-jack/json-patch/blob/master/CONTRIBUTING.md Installs all project dependencies, including development dependencies, as specified in the package.json file. This is necessary before running tests or building. ```Shell npm install ``` -------------------------------- ### Install TypeScript Compiler Shell Source: https://github.com/starcounter-jack/json-patch/blob/master/CONTRIBUTING.md Installs the TypeScript compiler globally on your system using npm. This is required to build the TypeScript source files. ```Shell npm install -g typescript ``` -------------------------------- ### Applying Single JSON Patch Operation with applyOperation (JavaScript) Source: https://github.com/starcounter-jack/json-patch/blob/master/README.md Demonstrates how to use the `applyOperation` function to apply a single JSON Patch operation to a document. The example shows a 'replace' operation changing a property value and illustrates the resulting document state. ```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: [] }} ``` -------------------------------- ### Applying Multiple JSON Patch Operations with applyReducer and Array.reduce (JavaScript) Source: https://github.com/starcounter-jack/json-patch/blob/master/README.md Illustrates the use of `applyReducer` in conjunction with JavaScript's `Array.reduce` method to apply a sequence of JSON Patch operations (a patch array) to a document. The example includes 'replace' and 'add' operations. ```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"} ] } }; ``` -------------------------------- ### Publish NPM Package Shell Source: https://github.com/starcounter-jack/json-patch/blob/master/CONTRIBUTING.md Publishes the current version of the package to the npm registry. This makes the new version available for others to install. ```Shell npm publish ``` -------------------------------- ### Run All Node.js Benchmarks Shell Source: https://github.com/starcounter-jack/json-patch/blob/master/CONTRIBUTING.md Executes the main benchmark script defined in package.json, running performance tests for the entire project in Node.js. ```Shell npm run bench ``` -------------------------------- ### Run Build Process Shell Source: https://github.com/starcounter-jack/json-patch/blob/master/CONTRIBUTING.md Executes the build script defined in package.json. This typically involves transpiling, bundling, and minifying the code for distribution. ```Shell npm run build ``` -------------------------------- ### Include fast-json-patch in Browser (Script Tag) Source: https://github.com/starcounter-jack/json-patch/blob/master/README.md How to include the fast-json-patch library in a web browser using a standard script tag pointing to the minified distribution file. ```HTML ``` -------------------------------- ### Run All Node.js Tests Shell Source: https://github.com/starcounter-jack/json-patch/blob/master/CONTRIBUTING.md Executes the main test script defined in package.json, which runs all Node.js unit tests for the project. ```Shell npm run test ``` -------------------------------- ### Generating JSON Patch from Observation (JavaScript) Source: https://github.com/starcounter-jack/json-patch/blob/master/README.md Demonstrates how to use `jsonpatch.observe` to track changes on an object and then `jsonpatch.generate` to produce a JSON Patch array representing those changes. ```JavaScript 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"}} // ]; ``` -------------------------------- ### Verify NPM Publish Shell Source: https://github.com/starcounter-jack/json-patch/blob/master/CONTRIBUTING.md Checks the distribution tags for the 'fast-json-patch' package on the npm registry to verify that the new version was successfully published. ```Shell npm view fast-json-patch dist-tags ``` -------------------------------- ### Include fast-json-patch in Browser (ES Module) Source: https://github.com/starcounter-jack/json-patch/blob/master/README.md How to import the fast-json-patch library as an ECMAScript module in browsers that support ES modules, demonstrating both namespace and named imports. ```JavaScript import * as jsonpatch from 'fast-json-patch/index.mjs'; import { applyOperation } from 'fast-json-patch/index.mjs'; ``` -------------------------------- ### Apply JSON Patch using applyPatch (JavaScript) Source: https://github.com/starcounter-jack/json-patch/blob/master/README.md Demonstrates how to use the `applyPatch` function to modify a JavaScript object based on a JSON Patch array, showing the initial document, the patch operations, and the resulting document state. ```JavaScript 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"}] } }; ``` -------------------------------- ### Run TypeScript Compilation Shell Source: https://github.com/starcounter-jack/json-patch/blob/master/CONTRIBUTING.md Executes the TypeScript compilation script defined in the package.json file. This command compiles the TypeScript source files into JavaScript. ```Shell npm run tsc ``` -------------------------------- ### Comparing Documents and Generating JSON Patch (JavaScript) Source: https://github.com/starcounter-jack/json-patch/blob/master/README.md Illustrates using `jsonpatch.compare` in JavaScript to find the differences between two distinct object structures and generate a patch to transform the first into the second. ```JavaScript 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"}] ``` -------------------------------- ### Run Core Node.js Benchmarks Shell Source: https://github.com/starcounter-jack/json-patch/blob/master/CONTRIBUTING.md Executes the specific benchmark script for the 'core' module in Node.js. Useful for evaluating performance changes in the core functionality. ```Shell npm run bench-core ``` -------------------------------- ### Comparing Documents and Generating Invertible JSON Patch (JavaScript) Source: https://github.com/starcounter-jack/json-patch/blob/master/README.md Demonstrates using `jsonpatch.compare` with the `invertible` flag set to `true` to generate a patch that includes `test` operations for the original values in the first document before applying changes. ```JavaScript 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"} // ]; ``` -------------------------------- ### Generating Invertible JSON Patch from Observation (JavaScript) Source: https://github.com/starcounter-jack/json-patch/blob/master/README.md Shows how to use `jsonpatch.generate` with the optional `invertible` parameter set to `true` to include `test` operations in the generated patch, verifying the original value before applying a change. ```JavaScript 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"}} // ]; ``` -------------------------------- ### Run Core Node.js Tests Shell Source: https://github.com/starcounter-jack/json-patch/blob/master/CONTRIBUTING.md Executes the specific test script for the 'core' module in Node.js. Useful for testing changes only affecting the core functionality. ```Shell npm run test-core ``` -------------------------------- ### Push Git Tags Shell Source: https://github.com/starcounter-jack/json-patch/blob/master/CONTRIBUTING.md Pushes the local git tags (created by 'npm version') to the remote repository. This makes the version tag available remotely. ```Shell git push --tags ``` -------------------------------- ### Run Duplex Node.js Benchmarks Shell Source: https://github.com/starcounter-jack/json-patch/blob/master/CONTRIBUTING.md Executes the specific benchmark script for the 'duplex' module in Node.js. Useful for evaluating performance changes in duplex operations. ```Shell npm run bench-duplex ``` -------------------------------- ### Bump NPM Version Shell Source: https://github.com/starcounter-jack/json-patch/blob/master/CONTRIBUTING.md Updates the package version in package.json and creates a git tag. Replace the placeholder with the desired version increment type (major, minor, patch, etc.). ```Shell npm version [ major | minor | patch | premajor | preminor | prepatch | prerelease] ``` -------------------------------- ### Require fast-json-patch in Node.js (CommonJS) Source: https://github.com/starcounter-jack/json-patch/blob/master/README.md How to require the fast-json-patch library as a CommonJS module in standard Node.js environments, showing two common syntax variations. ```JavaScript const { applyOperation } = require('fast-json-patch'); const applyOperation = require('fast-json-patch').applyOperation; ``` -------------------------------- ### Validating JSON Patch Sequence with jsonpatch.validate (JavaScript) Source: https://github.com/starcounter-jack/json-patch/blob/master/README.md This snippet demonstrates how to use the `jsonpatch.validate` function to check if a sequence of JSON Patch operations is valid, optionally against a specific document. It shows how to iterate through the results and log errors if validation fails. ```javascript 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 && errors.length === 0) { //there are no errors! } else if (errors) { 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]); } } } ``` -------------------------------- ### Import fast-json-patch in Bundlers (ES Module) Source: https://github.com/starcounter-jack/json-patch/blob/master/README.md How to import the fast-json-patch library as an ECMAScript module when using bundlers like Webpack. ```JavaScript import * as jsonpatch from 'fast-json-patch'; import { applyOperation } from 'fast-json-patch'; ``` -------------------------------- ### Run Duplex Node.js Tests Shell Source: https://github.com/starcounter-jack/json-patch/blob/master/CONTRIBUTING.md Executes the specific test script for the 'duplex' module in Node.js. Useful for testing changes only affecting duplex operations. ```Shell npm run test-duplex ``` -------------------------------- ### Import fast-json-patch in Node.js (ES Module) Source: https://github.com/starcounter-jack/json-patch/blob/master/README.md How to import the fast-json-patch library as an ECMAScript module in Node.js environments supporting ES modules (e.g., Node 12+ with --experimental-modules). ```JavaScript import * as jsonpatch from 'fast-json-patch/index.mjs'; import { applyOperation } from 'fast-json-patch/index.mjs'; ``` -------------------------------- ### Comparing Documents and Generating JSON Patch (TypeScript) Source: https://github.com/starcounter-jack/json-patch/blob/master/README.md Provides the TypeScript signature for the `jsonpatch.compare` function, which calculates the difference between two JSON-like objects and returns it as a JSON Patch array. It includes relevant type definitions. ```TypeScript jsonpatch.compare(document1: Jsonable, document2: Jsonable, invertible = false): Operation[] type JsonableObj = { [key:string]: Jsonable }; type JsonableArr = Jsonable[]; type Jsonable = JsonableArr | JsonableObj | string | number | boolean | null; ``` -------------------------------- ### Push Git Commits Shell Source: https://github.com/starcounter-jack/json-patch/blob/master/CONTRIBUTING.md Pushes the local commits on the current branch (expected to be 'master' for releases) to the remote repository. ```Shell git push ``` -------------------------------- ### Stopping JSON Patch Observation (TypeScript) Source: https://github.com/starcounter-jack/json-patch/blob/master/README.md Provides the TypeScript signature for the `jsonpatch.unobserve` function, which is used to stop tracking changes on a document that was previously observed. It also includes relevant type definitions. ```TypeScript jsonpatch.unobserve(document: any, observer: Observer): void type JsonableObj = { [key:string]: Jsonable }; type JsonableArr = Jsonable[]; type Jsonable = JsonableArr | JsonableObj | string | number | boolean | null; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.