2024-07-30 23:24:17 +08:00
|
|
|
import { assertEquals } from "@std/assert";
|
2024-08-22 02:13:40 +08:00
|
|
|
import { cleanupJSON, deepClone, deepEqual, deepObjectMerge } from "./json.ts";
|
2024-02-29 22:25:28 +08:00
|
|
|
|
2024-08-02 22:47:36 +08:00
|
|
|
Deno.test("JSON utils", () => {
|
2024-02-29 22:25:28 +08:00
|
|
|
assertEquals(deepEqual({ a: 1 }, { a: 1 }), true);
|
2024-08-03 17:54:32 +08:00
|
|
|
assertEquals(deepEqual({ a: null }, { a: null }), true);
|
|
|
|
assertEquals(deepEqual({ a: {} }, { a: null }), false);
|
|
|
|
assertEquals(deepEqual({ a: {} }, { a: undefined }), false);
|
|
|
|
assertEquals(deepEqual({ a: null }, { a: {} }), false);
|
2024-02-29 22:25:28 +08:00
|
|
|
assertEquals(deepObjectMerge({ a: 1 }, { a: 2 }), { a: 2 });
|
|
|
|
assertEquals(
|
|
|
|
deepObjectMerge({ list: [1, 2, 3] }, { list: [4, 5, 6] }),
|
|
|
|
{ list: [1, 2, 3, 4, 5, 6] },
|
|
|
|
);
|
|
|
|
assertEquals(deepObjectMerge({ a: { b: 1 } }, { a: { c: 2 } }), {
|
|
|
|
a: { b: 1, c: 2 },
|
|
|
|
});
|
2024-07-06 21:07:40 +08:00
|
|
|
assertEquals(cleanupJSON({ "a.b": 1 }), { a: { b: 1 } });
|
|
|
|
assertEquals(cleanupJSON({ a: { "a.b": 1 } }), {
|
2024-02-29 22:25:28 +08:00
|
|
|
a: { a: { b: 1 } },
|
|
|
|
});
|
2024-07-06 21:07:40 +08:00
|
|
|
assertEquals(cleanupJSON({ a: [{ "a.b": 1 }] }), {
|
2024-02-29 22:25:28 +08:00
|
|
|
a: [{ a: { b: 1 } }],
|
|
|
|
});
|
2024-08-18 19:08:52 +08:00
|
|
|
|
2024-07-07 03:52:03 +08:00
|
|
|
assertEquals(cleanupJSON(new Date("2023-05-03T00:00:00Z")), "2023-05-03");
|
2024-02-29 22:25:28 +08:00
|
|
|
});
|
2024-08-22 02:13:40 +08:00
|
|
|
|
|
|
|
Deno.test("JSON utils - deepObjectMerge", () => {
|
|
|
|
// Tests for deepClone
|
|
|
|
const obj1 = { a: 1, b: { c: 2, d: [3, 4] }, e: new Date("2023-08-21") };
|
|
|
|
const clone1 = deepClone(obj1);
|
|
|
|
assertEquals(clone1, obj1);
|
|
|
|
assertEquals(clone1 === obj1, false); // Ensuring deep clone, not shallow
|
|
|
|
assertEquals(clone1.b === obj1.b, false); // Nested object should be different reference
|
|
|
|
assertEquals(clone1.e === obj1.e, false); // Date object should be different reference
|
|
|
|
|
|
|
|
const arrayTest = [1, 2, { a: 3, b: [4, 5] }];
|
|
|
|
const cloneArray = deepClone(arrayTest);
|
|
|
|
assertEquals(cloneArray, arrayTest);
|
|
|
|
assertEquals(cloneArray === arrayTest, false); // Array itself should be different reference
|
|
|
|
assertEquals(cloneArray[2] === arrayTest[2], false); // Nested object in array should be different reference
|
|
|
|
|
|
|
|
const nullTest = { a: null, b: undefined, c: { d: null } };
|
|
|
|
const cloneNullTest = deepClone(nullTest);
|
|
|
|
assertEquals(cloneNullTest, nullTest);
|
|
|
|
assertEquals(cloneNullTest === nullTest, false); // Ensure it's a deep clone
|
|
|
|
assertEquals(cloneNullTest.c === nullTest.c, false); // Nested object should be different reference
|
|
|
|
|
|
|
|
const dateTest = new Date();
|
|
|
|
const cloneDateTest = deepClone(dateTest);
|
|
|
|
assertEquals(cloneDateTest.getTime(), dateTest.getTime());
|
|
|
|
assertEquals(cloneDateTest === dateTest, false); // Date instance should be different reference
|
|
|
|
});
|