Skip to main content
This release is 1 version behind 0.3.3 — the latest version of @coven/compare. Jump to latest

Built and signed on GitHub Actions

⚖️ Minimalist diffing.

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
2 months ago (0.3.2)
Coven Engineering Compare logo

JSR JSR Score

⚖️ 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:

  • Create: Missing left and existing right.
  • Update: Different left and right values.
  • Delete: Existing left and missing right.

Plain update

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",
});

Object update

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"],
});

Object delete

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"],
});

Object create

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"],
});

Multiple differences

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"] });
Important

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 IterableIterators 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.

Built and signed on
GitHub Actions
View transparency log

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";