Diff by Lou
↔️ Deep diffing utility.
Usage
📦 Node
Install @lou.codes/diff
as a dependency:
pnpm add @lou.codes/diff# ornpm install @lou.codes/diff# oryarn 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>
Useful links
- 📝 Documentation: TypeDoc generated documentation.
- ⏳ Changelog: List of changes between versions.
- ✅ Tests Coverage: Coveralls page with tests coverage.
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
Name | Type | Description |
---|---|---|
Right | unknown | Type of the new value. |
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
Name | Type | Description |
---|---|---|
Left | unknown | Type of the removed value. |
Difference
Ƭ Difference<Kind
>: Object
Object that represents a difference between two values.
Example
const difference: Difference<"CREATE"> = { kind: "CREATE", path: ["foo", "bar"],};
Type parameters
Name | Type | Description |
---|---|---|
Kind | extends DifferenceKind | Type of diff. |
Type declaration
Name | Type | Description |
---|---|---|
kind | Kind | Type of diff. |
path | ReadOnlyArray <PropertyKey > | Path of diff property. |
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";
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
Name | Type | Description |
---|---|---|
Left | unknown | Type of the new value. |
Right | unknown | Type of the original value. |
compare
▸ compare(values
):
Generator
<CreateDifference
|
DeleteDifference
|
UpdateDifference
, any
, unknown
>
Function to compare a left
and a right
value.
Parameters
Name | Type | Description |
---|---|---|
values | Object | Values to compare. |
values.left (optional) | unknown | Left value to compare. |
values.right (optional) | unknown | Right 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
Other
CREATE
• Const
CREATE: "CREATE"
Create difference kind.
DELETE
• Const
DELETE: "DELETE"
Delete difference kind.
UPDATE
• Const
UPDATE: "UPDATE"
Update difference kind.