Skip to content

Diff Reference

Coverage License NPM Version Open Issues Size

↔️ Deep diffing utility.

Usage

📦 Node

Install @lou.codes/diff as a dependency:

Terminal window
pnpm add @lou.codes/diff
# or
npm install @lou.codes/diff
# or
yarn add @lou.codes/diff

Import it and use it:

import { compare } from "@lou.codes/diff";
compare({ left: "Lou", right: "Nope" }); // [{ kind: "UPDATE", left: "Lou", right: "Nope", path: [] }]

🦕 Deno

Import @lou.codes/diff using the npm: prefix, and use it directly:

import { compare } from "npm:@lou.codes/diff";
compare({ left: "Lou", right: "Nope" }); // [{ kind: "UPDATE", left: "Lou", right: "Nope", path: [] }]

🌎 Browser

Import @lou.codes/diff using esm.sh, and use it directly:

<script type="module">
import { compare } from "https://esm.sh/@lou.codes/diff";
compare({ left: "Lou", right: "Nope" }); // [{ kind: "UPDATE", left: "Lou", right: "Nope", path: [] }]
</script>

Difference

CreateDifference

Ƭ CreateDifference<Right>: Difference<typeof CREATE> & { right: Right }

Object that represents a creation difference (a new value or property).

Example

const createDifference: CreateDifference<string> = {
kind: "CREATE",
path: ["foo", "bar"],
right: "Lou",
};

Type parameters

NameTypeDescription
RightunknownType of the new value.

View source


DeleteDifference

Ƭ DeleteDifference<Left>: Difference<typeof DELETE> & { left: Left }

Object that represents a deletion difference (a value or property was removed).

Example

const deleteDifference: DeleteDifference<string> = {
kind: "DELETE",
path: ["foo", "bar"],
left: "Lou",
};

Type parameters

NameTypeDescription
LeftunknownType of the removed value.

View source


Difference

Ƭ Difference<Kind>: Object

Object that represents a difference between two values.

Example

const difference: Difference<"CREATE"> = {
kind: "CREATE",
path: ["foo", "bar"],
};

Type parameters

NameTypeDescription
Kindextends DifferenceKindType of diff.

Type declaration

NameTypeDescription
kindKindType of diff.
pathReadOnlyArray<PropertyKey>Path of diff property.

View source


DifferenceKind

Ƭ DifferenceKind: typeof CREATE | typeof DELETE | typeof UPDATE

Kinds of differences (create, delete or update).

Example

const create: DifferenceKind = "CREATE";
const delete: DifferenceKind = "DELETE";
const update: DifferenceKind = "UPDATE";

View source


UpdateDifference

Ƭ UpdateDifference<Left, Right>: Difference<typeof UPDATE> & { left: Left ; right: Right }

Object that represents an update difference (a value or property changed).

Example

const updateDifference: UpdateDifference<string, number> = {
kind: "UPDATE",
path: ["foo", "bar"],
left: "Lou",
right: 42,
};

Type parameters

NameTypeDescription
LeftunknownType of the new value.
RightunknownType of the original value.

View source


compare

compare(values): Generator<CreateDifference | DeleteDifference | UpdateDifference, any, unknown>

Function to compare a left and a right value.

Parameters

NameTypeDescription
valuesObjectValues to compare.
values.left (optional)unknownLeft value to compare.
values.right (optional)unknownRight value to compare.

Returns

Generator<CreateDifference | DeleteDifference | UpdateDifference, any, unknown>

Remarks

This does a deep comparison and yields the differences found with a descriptive object.

Example

compare({ left: "🟢", right: "🟢" }); // []
compare({ left: "🟢", right: "" }); // [{ kind: UPDATE, left: "🟢", right: "❌", path: [] }]
compare({ left: { foo: "🟢" }, right: { foo: "" } }); // [{ kind: UPDATE, left: "🟢", right: "❌", path: ["foo"] }]

See

View source

Other

CREATE

Const CREATE: "CREATE"

Create difference kind.

View source


DELETE

Const DELETE: "DELETE"

Delete difference kind.

View source


UPDATE

Const UPDATE: "UPDATE"

Update difference kind.

View source