diff --git a/src/object-diff.ts b/src/object-diff.ts index 6c2af89..532a394 100644 --- a/src/object-diff.ts +++ b/src/object-diff.ts @@ -5,7 +5,7 @@ import { STATUS, Subproperties, } from "./model"; -import { hasNestedValues, isEqual } from "./utils"; +import { isObject, isEqual } from "./utils"; function getObjectStatus(diff: ObjectDiff["diff"]): DiffStatus { return diff.some((property) => property.status !== STATUS.EQUAL) @@ -26,7 +26,7 @@ function formatSingleObjectDiff( } const diff: ObjectDiff["diff"] = []; Object.entries(data).forEach(([property, value]) => { - if (hasNestedValues(value)) { + if (isObject(value)) { const subPropertiesDiff: Subproperties[] = []; Object.entries(value).forEach(([subProperty, subValue]) => { subPropertiesDiff.push({ @@ -79,7 +79,7 @@ export function getObjectDiff( Object.entries(nextData).forEach(([nextProperty, nextValue]) => { const previousValue = prevData[nextProperty]; - if (hasNestedValues(nextValue)) { + if (isObject(nextValue)) { const prevSubValues = previousValue ? Object.entries(previousValue) : null; diff --git a/src/utils.ts b/src/utils.ts index 098f5f1..55681d5 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,7 +1,7 @@ export function isEqual(a: any, b: any): boolean { - if (typeof a !== typeof b) return true; + if (typeof a !== typeof b) return false; if (Array.isArray(a)) { - return a.toString() === b.toString(); + return a.every((v, i) => JSON.stringify(v) === JSON.stringify(b[i])); } if (typeof a === "object") { return JSON.stringify(a) === JSON.stringify(b); @@ -9,6 +9,6 @@ export function isEqual(a: any, b: any): boolean { return a === b; } -export function hasNestedValues(value: any): value is Record { - return typeof value === "object" && !Array.isArray(value); +export function isObject(value: any): value is Record { + return !!value && typeof value === "object" && !Array.isArray(value); } diff --git a/test/utils.test.ts b/test/utils.test.ts index 0096f7f..b751386 100644 --- a/test/utils.test.ts +++ b/test/utils.test.ts @@ -1,10 +1,57 @@ -// import { isEqual, hasNestedValues } from "../src/utils"; +import { isEqual, isObject } from "../src/utils"; -// describe("isEqual", () => { -// it("", () => { -// expect(isEqual(null, null)).toStrictEqual(null); -// }); -// it("", () => { -// expect(hasNestedValues(null)).toStrictEqual(null); -// }); -// }); +describe("isEqual", () => { + it("return true if data are the same", () => { + expect(isEqual(null, null)).toBeTruthy(); + expect(isEqual(undefined, undefined)).toBeTruthy(); + expect(isEqual("hello", "hello")).toBeTruthy(); + expect(isEqual(57, 57)).toBeTruthy(); + expect(isEqual(["hello", "world"], ["hello", "world"])).toBeTruthy(); + expect( + isEqual( + [ + { name: "joe", age: 99 }, + { name: "nina", age: 23 }, + ], + [ + { name: "joe", age: 99 }, + { name: "nina", age: 23 }, + ] + ) + ).toBeTruthy(); + }); + it("return false if data are different", () => { + expect(isEqual(null, "hello")).toBeFalsy(); + expect(isEqual("hello", undefined)).toBeFalsy(); + expect(isEqual("hello", "howdy")).toBeFalsy(); + expect(isEqual(57, 51)).toBeFalsy(); + expect(isEqual(["hello", "world"], ["howdy", "world"])).toBeFalsy(); + expect( + isEqual( + [ + { name: "joe", age: 99 }, + { name: "nina", age: 23 }, + ], + [ + { name: "joe", age: 98 }, + { name: "nina", age: 23 }, + ] + ) + ).toBeFalsy(); + }); +}); + +describe("isObject", () => { + it("return true if the value has nested values", () => { + expect(isObject({ name: "joe" })).toBeTruthy(); + expect(isObject({ user: { name: "joe" } })).toBeTruthy(); + }); + it("return false if the value doesn't have nested values", () => { + expect(isObject("joe")).toBeFalsy(); + expect(isObject(56)).toBeFalsy(); + expect(isObject(true)).toBeFalsy(); + expect(isObject(null)).toBeFalsy(); + expect(isObject(undefined)).toBeFalsy(); + expect(isObject(["hello", "world"])).toBeFalsy(); + }); +});