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
1
pnpm add @lou.codes/diff
2
# or
3
npm install @lou.codes/diff
4
# or
5
yarn add @lou.codes/diff

Import it and use it:

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

🦕 Deno

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

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

🌎 Browser

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

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

Difference

CreateDifference<Right>

1
type CreateDifference<Right>: Difference<typeof CREATE> & object;

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

Example

1
const createDifference: CreateDifference<string> = {
2
kind: 1,
3
path: ["foo", "bar"],
4
right: "Lou",
5
};

Type declaration

MemberTypeDescription
rightRightNew value.

Type parameters

Type parameterValueDescription
RightunknownType of the new value.

View source


DeleteDifference<Left>

1
type DeleteDifference<Left>: Difference<typeof DELETE> & object;

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

Example

1
const deleteDifference: DeleteDifference<string> = {
2
kind: 2,
3
path: ["foo", "bar"],
4
left: "Lou",
5
};

Type declaration

MemberTypeDescription
leftLeftOriginal value.

Type parameters

Type parameterValueDescription
LeftunknownType of the removed value.

View source


Difference<Kind>

1
type Difference<Kind>: object;

Object that represents a difference between two values.

Example

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

Type parameters

Type parameterDescription
Kind extends DifferenceKindType of diff.

Type declaration

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

View source


DifferenceKind

1
type DifferenceKind: typeof CREATE | typeof DELETE | typeof UPDATE;

Kinds of differences (create, delete or update).

Example

1
const create: DifferenceKind = 1;
2
const delete: DifferenceKind = 2;
3
const update: DifferenceKind = 4;

View source


UpdateDifference<Left, Right>

1
type UpdateDifference<Left, Right>: Difference<typeof UPDATE> & object;

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

Example

1
const updateDifference: UpdateDifference<string, number> = {
2
kind: 4,
3
path: ["foo", "bar"],
4
left: "Lou",
5
right: 42,
6
};

Type declaration

MemberTypeDescription
leftLeftOriginal value.
rightRightNew value.

Type parameters

Type parameterValueDescription
LeftunknownType of the new value.
RightunknownType of the original value.

View source


compare()

1
function compare(
2
values: object,
3
): Generator<
4
CreateDifference | DeleteDifference | UpdateDifference,
5
any,
6
unknown
7
>;

Function to compare a left and a right value.

Parameters

ParameterTypeDescription
valuesobjectValues to compare.
values.left?unknownLeft value to compare.
values.right?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

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

See

View source

Other

CREATE

1
const CREATE: 1 = 1;

Create difference kind.

View source


DELETE

1
const DELETE: 2 = 2;

Delete difference kind.

View source


UPDATE

1
const UPDATE: 4 = 4;

Update difference kind.

View source