Fixes #193: Allowing plug overrides
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
import { assertEquals } from "../../test_deps.ts";
|
||||
import { deepEqual, deepObjectMerge, expandPropertyNames } from "./json.ts";
|
||||
|
||||
Deno.test("utils", () => {
|
||||
assertEquals(deepEqual({ a: 1 }, { a: 1 }), true);
|
||||
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 },
|
||||
});
|
||||
assertEquals(expandPropertyNames({ "a.b": 1 }), { a: { b: 1 } });
|
||||
assertEquals(expandPropertyNames({ a: { "a.b": 1 } }), {
|
||||
a: { a: { b: 1 } },
|
||||
});
|
||||
assertEquals(expandPropertyNames({ a: [{ "a.b": 1 }] }), {
|
||||
a: [{ a: { b: 1 } }],
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,86 @@
|
||||
// Compares two objects deeply
|
||||
export function deepEqual(a: any, b: any): boolean {
|
||||
if (a === b) {
|
||||
return true;
|
||||
}
|
||||
if (typeof a !== typeof b) {
|
||||
return false;
|
||||
}
|
||||
if (typeof a === "object") {
|
||||
if (Array.isArray(a) && Array.isArray(b)) {
|
||||
if (a.length !== b.length) {
|
||||
return false;
|
||||
}
|
||||
for (let i = 0; i < a.length; i++) {
|
||||
if (!deepEqual(a[i], b[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
const aKeys = Object.keys(a);
|
||||
const bKeys = Object.keys(b);
|
||||
if (aKeys.length !== bKeys.length) {
|
||||
return false;
|
||||
}
|
||||
for (const key of aKeys) {
|
||||
if (!deepEqual(a[key], b[key])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Expands property names in an object containing a .-separated path
|
||||
export function expandPropertyNames(a: any): any {
|
||||
if (!a) {
|
||||
return a;
|
||||
}
|
||||
if (typeof a !== "object") {
|
||||
return a;
|
||||
}
|
||||
if (Array.isArray(a)) {
|
||||
return a.map(expandPropertyNames);
|
||||
}
|
||||
const expanded: any = {};
|
||||
for (const key of Object.keys(a)) {
|
||||
const parts = key.split(".");
|
||||
let target = expanded;
|
||||
for (let i = 0; i < parts.length - 1; i++) {
|
||||
const part = parts[i];
|
||||
if (!target[part]) {
|
||||
target[part] = {};
|
||||
}
|
||||
target = target[part];
|
||||
}
|
||||
target[parts[parts.length - 1]] = expandPropertyNames(a[key]);
|
||||
}
|
||||
return expanded;
|
||||
}
|
||||
|
||||
export function deepObjectMerge(a: any, b: any): any {
|
||||
if (typeof a !== typeof b) {
|
||||
return b;
|
||||
}
|
||||
if (typeof a === "object") {
|
||||
if (Array.isArray(a) && Array.isArray(b)) {
|
||||
return [...a, ...b];
|
||||
} else {
|
||||
const aKeys = Object.keys(a);
|
||||
const bKeys = Object.keys(b);
|
||||
const merged = { ...a };
|
||||
for (const key of bKeys) {
|
||||
if (aKeys.includes(key)) {
|
||||
merged[key] = deepObjectMerge(a[key], b[key]);
|
||||
} else {
|
||||
merged[key] = b[key];
|
||||
}
|
||||
}
|
||||
return merged;
|
||||
}
|
||||
}
|
||||
return b;
|
||||
}
|
||||
Reference in New Issue
Block a user