Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"editor.tabSize": 2,
"editor.insertSpaces": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
"editor.formatOnSave": true,
"files.insertFinalNewline": true
}
25 changes: 7 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,5 @@
"typescript": "^5.5.3",
"vitest": "^2.1.4"
},
"dependencies": {
"es-toolkit": "^1.27.0"
}
}
"dependencies": {}
}
2 changes: 1 addition & 1 deletion src/comparator/ArrayComparator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { JsonArray } from "../types/json";
import { isEqual } from "../utils/isEqual";
import { ComparisonOperators } from "./ComparisonOperators";
import { isEqual } from "es-toolkit";

// equal arrays, that is, arrays of the same length where each element of
// the first array is equal to the corresponding element of the second array, or
Expand Down
2 changes: 1 addition & 1 deletion src/comparator/ObjectComparator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isEqual } from "es-toolkit";
import { JsonObject } from "../types/json";
import { isEqual } from "../utils/isEqual";
import { ComparisonOperators } from "./ComparisonOperators";

// equal objects with no duplicate names, that is, where:
Expand Down
61 changes: 61 additions & 0 deletions src/utils/isEqual.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { JsonValue } from "../types/json";

/**
* Check the deep equality of two JSON values.
* @param a - The first JSON value.
* @param b - The second JSON value.
* @returns `true` if the two JSON values are equal, `false` otherwise.
*/
export function isEqual(a: JsonValue, b: JsonValue): boolean {
return isEqualImpl(a, b);
}

function isEqualImpl(
a: JsonValue,
b: JsonValue,
visited: WeakMap<object, unknown> = new WeakMap(),
): boolean {
if (a === b) {
return true;
}

if (typeof a !== typeof b) {
return false;
}

if (a === null || b === null) {
return false;
}

if (Array.isArray(a) && Array.isArray(b)) {
if (a.length !== b.length) {
return false;
}

return a.every((value, index) => isEqualImpl(value, b[index], visited));
}

if (Array.isArray(a) || Array.isArray(b)) {
return false;
}

if (typeof a === "object" && typeof b === "object") {
// Check circular references
if (visited.has(a)) {
return visited.get(a) === b;
}

visited.set(a, b);

const keysA = Object.keys(a);
const keysB = Object.keys(b);

if (keysA.length !== keysB.length) {
return false;
}

return keysA.every((key) => isEqualImpl(a[key], b[key], visited));
}

return false;
}
72 changes: 72 additions & 0 deletions tests/utils/isEqual.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { describe, expect, test } from "vitest";
import { isEqual } from "../../src/utils/isEqual";

describe("isEqual", () => {
test("should return true for equal primitive values", () => {
expect(isEqual(1, 1)).toBe(true);
expect(isEqual(1.2, 1.2)).toBe(true);
expect(isEqual("a", "a")).toBe(true);
expect(isEqual(true, true)).toBe(true);
expect(isEqual(null, null)).toBe(true);
});

test("should return false for different primitive values", () => {
expect(isEqual(1, 2)).toBe(false);
expect(isEqual(1.2, 2.1)).toBe(false);
expect(isEqual("a", "b")).toBe(false);
expect(isEqual(true, false)).toBe(false);
expect(isEqual(null, 1)).toBe(false);
});

test("should return true for equal arrays", () => {
expect(isEqual([], [])).toBe(true);
expect(isEqual([1, 2, 3], [1, 2, 3])).toBe(true);
expect(isEqual([1, 2, [3, 4]], [1, 2, [3, 4]])).toBe(true);
});

test("should return false for different arrays", () => {
expect(isEqual([], [1])).toBe(false);
expect(isEqual([1, 2, 3], [1, 2, 4])).toBe(false);
expect(isEqual([1, 2, [3, 4]], [1, 2, [3, 5]])).toBe(false);
});

test("should return true for equal objects", () => {
expect(isEqual({}, {})).toBe(true);
expect(isEqual({ a: 1, b: 2 }, { a: 1, b: 2 })).toBe(true);
});

test("should return true for nested equal objects", () => {
expect(isEqual({ a: 1, b: { c: "2" } }, { a: 1, b: { c: "2" } })).toBe(
true,
);
});

test("should return false for different objects", () => {
expect(isEqual({}, { a: 1 })).toBe(false);
expect(isEqual({ a: 1, b: 2 }, { a: 1, b: 3 })).toBe(false);
});

test("should return true for object that has circular dependency", () => {
const a: any = { foo: "bar" };
const b: any = { foo: "bar" };

a.self = a;
b.self = b;

expect(isEqual(a, b)).toBe(true);
});

test("should return false for different object that has circular dependency", () => {
const aChild: any = { baz: "qux" };
const bChild: any = { baz: "quux" };

aChild.self = aChild;
bChild.self = bChild;

const a = { foo: "bar", child: aChild };
const b = { foo: "bar", child: bChild };

expect(isEqual(a, b)).toBe(false);
});
});