Skip to content

Commit

Permalink
Merge pull request #16 from Lordfirespeed/main
Browse files Browse the repository at this point in the history
Only merge obviously record-like objects
  • Loading branch information
alexmarqs authored Nov 3, 2024
2 parents a82c9cc + 24c3e66 commit 8f88a7a
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 12 deletions.
32 changes: 21 additions & 11 deletions src/lib/adapters/utils.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
export const filterByPrefixKey = (
data: { [key: string]: any } | undefined | null,
data: unknown,
prefixKey: string,
) => {
if (!data) {
return {};
}
if (data == null) return {}
if (!isMergeableObject(data)) throw new TypeError(`Cannot filter ${data} by prefix key as it is not a record-like object`)

return Object.keys(data)
.filter((key) => key.startsWith(prefixKey))
.reduce<{ [key: string]: any }>((acc, key) => {
.reduce<Partial<Record<string, unknown>>>((acc, key) => {
acc[key] = data[key];

return acc;
}, {});
};

export function deepMerge(target: any, ...sources: any[]) {
export function deepMerge(
target: Partial<Record<string, unknown>>,
...sources: unknown[]
): Partial<Record<string, unknown>> {
if (!sources.length) {
return target;
}
Expand All @@ -35,17 +37,25 @@ export function deepMerge(target: any, ...sources: any[]) {
return;
}

if (!target[key]) {
target[key] = {};
const subTarget = target[key]
if (!isMergeableObject(subTarget)) {
target[key] = deepMerge({}, source[key])
return
}

deepMerge(target[key], source[key]);
deepMerge(subTarget, source[key]);
});
}

return deepMerge(target, ...sources);
}

export function isMergeableObject(item: any) {
return item && typeof item === "object" && !Array.isArray(item);
export function isMergeableObject(item: unknown): item is Partial<Record<string, unknown>> {
if (!item) return false
if (typeof item !== "object") return false
// ES6 class instances, Maps, Sets, Arrays, etc. are not considered records
if (Object.getPrototypeOf(item) === Object.prototype) return true
// Some library/Node.js functions return records with null prototype
if (Object.getPrototypeOf(item) === null) return true
return false
}
63 changes: 63 additions & 0 deletions tests/script-adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,66 @@ describe.each([
expect(config.PORT).toBe(expected.PORT);
});
});

describe("combining multiple script adapters", () => {
const testFilePath1 = path.join(__dirname, "test-multiple-script-adapter1.ts");
const testFilePath2 = path.join(__dirname, "test-multiple-script-adapter2.ts");

beforeAll(async () => {
const fileContent1 = `export default {
HOST: "app name",
PORT: "1111",
TEST_MAP: new Map([["key", "value"], ["key2", "value2"]]),
TEST_RECORD: { key: "key", value: "value1" }
}`;
const fileContent2 = `export default {
HOST: "app name2",
PORT: "1234",
TEST_MAP: new Map([["key", "value2"]]),
TEST_RECORD: { key: "key2" }
}`;

await Promise.all([
writeFile(testFilePath1, fileContent1),
writeFile(testFilePath2, fileContent2),
]);
});

afterAll(async () => {
await Promise.all([
unlink(testFilePath1),
unlink(testFilePath2),
]);
});

it("should successfully parse, merge and return type-safe data", async () => {
// given
const schema = z.object({
HOST: z.string(),
PORT: z.string().regex(/^\d+$/),
TEST_RECORD: z.object({
key: z.string(),
value: z.string()
}),
TEST_MAP: z.map(z.string(), z.string()).optional(),
});

// when
const config = await loadConfig({
schema,
adapters: [
scriptAdapter({ path: testFilePath1 }),
scriptAdapter({ path: testFilePath2 }),
],
});

// then
expect(config.HOST).toBe("app name2"); // second adapter overrides the first one
expect(config.PORT).toBe("1234"); // second adapter overrides the first one
expect(config.TEST_MAP).toEqual(new Map([["key", "value2"]])); // MAP is not mergeable so only the last one is loaded
expect(config.TEST_RECORD).toEqual({
key: "key2", // from second adapter
value: "value1" // preserved from first adapter
}); // records are merged between adapters
});
});
25 changes: 24 additions & 1 deletion tests/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { deepMerge, filterByPrefixKey } from "../src/lib/adapters/utils";
import { deepMerge, filterByPrefixKey, isMergeableObject } from "../src/lib/adapters/utils";
import { describe, it, expect } from "vitest";

describe("filterByPrefixKey", () => {
Expand Down Expand Up @@ -93,3 +93,26 @@ describe("deepMerge", () => {
expect(result).toEqual({ a: null, b: 2, c: [] });
});
});

describe("isMergeableObject", () => {
it("should return true for plain objects", () => {
expect(isMergeableObject({})).toBe(true);
expect(isMergeableObject({ a: 1 })).toBe(true);
});

it("should return false for non-mergeable values", () => {
// null and undefined
expect(isMergeableObject(null)).toBe(false);
expect(isMergeableObject(undefined)).toBe(false);

// primitives
expect(isMergeableObject(10)).toBe(false);
expect(isMergeableObject("string")).toBe(false);
expect(isMergeableObject(true)).toBe(false);

// built-in objects and collections
expect(isMergeableObject([])).toBe(false);
expect(isMergeableObject(new Map())).toBe(false);
expect(isMergeableObject(new Set())).toBe(false);
});
});

0 comments on commit 8f88a7a

Please sign in to comment.