/micropatch

Primary LanguageJavaScriptMIT LicenseMIT

Micropatch Logo

Micropatch is a Microdiff compatible patching library that is small (<1kb minified) and simple to use.

Minizipped Size (from Bundlephobia) License dependency Count

Get started

First, install Micropatch and Microdiff if you want to generate patch statements

npm i micropatch
npm i microdiff <- only do this if you want diffing to generate patch statements

After you install it, simply import it and run it on two objects.

import patch from "micropatch";
import diff from "microdiff";
const obj1 = {
	originalProperty: true,
};
const obj2 = {
	originalProperty: false,
	newProperty: "new"
};
console.log(patch(obj1,[{type: "CREATE", path: ["newProperty"], value: "new"}])); // using diffs from other sources
/* {
	originalProperty: true,
	newProperty: "new"
} */
console.log(patch(obj1,diff(obj1,obj2))) // using Microdiff
/* {
	originalProperty: true,
	newProperty: "new"
} */

If you are using CommonJS, you can import it like this:

const diff = require("micropatch").default;

Micropatch takes two parameters, the original/target object and an array of Microdiff diffs. It returns the new object with the diffs applied.

⚠️ Warning: The original object is mutated. If you wish for it to be immutable, you can pass a clone of the object. However, the mutations are unreliable, so you should rely on the return value rather than the mutations.

Credits

@FluentCoding created the original implementation of this patch method.