⚖️ Minimalist diffing.
This library takes 2 values in a curried fashion, a left
or "original" value
first and then a right
or "new" value, finally returning an iterator with all
the differences between said values. The yielded differences are represented by
3 kinds:
left
and existing right
.left
and right
values.left
and missing right
.If we compare the string 2 different strings, we do it like this:
import { compare } from "@coven/compare"; compare("Coven")("Engineering");
And we only get a single yielded "update", that looks like this:
({ kind: "UPDATE", left: "Coven", path: [], // A generator that will yield nothing because is the root value right: "Engineering", });
If we compare two objects like this:
import { compare } from "@coven/compare"; compare({ example: 13 })({ example: 42 });
The yielded update will have values yielded from its path
:
({ kind: "UPDATE", left: 13, right: 42, path: ["example"], });
Let's say we update that same object to an empty one:
import { compare } from "@coven/compare"; compare({ example: 13 })({});
Yields this:
({ kind: "DELETE", left: 13, path: ["example"], });
Let's say we update that same object from an empty one:
import { compare } from "@coven/compare"; compare({})({ example: 42 });
Yields this:
({ kind: "CREATE", right: 42, path: ["example"], });
Now let's see what's yielded if we compare two objects with the 3 type of differences:
import { compare } from "@coven/compare"; compare({ original: "old", updated: "old", })({ created: "new", updated: "new", });
That yields all 3 kind of differences:
({ kind: "DELETE", left: "old", path: ["original"] }); ({ kind: "UPDATE", left: "old", right: "new", path: ["updated"] }); ({ kind: "CREATE", right: "new", path: ["created"] });
In all examples the path
property is represented as an array, but remember
path
is an IterableIterator
like the output of compare
is. The included
flat
function can be used to flatten all IterableIterator
s to arrays.
Like all Coven Engineering libraries, it has 100% test coverage and it's built in top of modern tech compatible with all JavaScript runtimes.
Add Package
deno add jsr:@coven/compare
Import symbol
import * as compare from "@coven/compare";
---- OR ----
Import directly with a jsr specifier
import * as compare from "jsr:@coven/compare";
Add Package
npx jsr add @coven/compare
Import symbol
import * as compare from "@coven/compare";
Add Package
yarn dlx jsr add @coven/compare
Import symbol
import * as compare from "@coven/compare";
Add Package
pnpm dlx jsr add @coven/compare
Import symbol
import * as compare from "@coven/compare";
Add Package
bunx jsr add @coven/compare
Import symbol
import * as compare from "@coven/compare";