From 4bbe321dfee9cac2e8bde81895bf851ef47e56d0 Mon Sep 17 00:00:00 2001 From: Miodec Date: Wed, 7 Feb 2024 17:09:02 +0100 Subject: [PATCH] impr: add function to merge partial config onto default config --- frontend/__tests__/test/config.spec.ts | 39 ++++++++++++++++++++++++++ frontend/src/ts/config.ts | 15 +++++++++- 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 frontend/__tests__/test/config.spec.ts diff --git a/frontend/__tests__/test/config.spec.ts b/frontend/__tests__/test/config.spec.ts new file mode 100644 index 000000000..0a2ac230f --- /dev/null +++ b/frontend/__tests__/test/config.spec.ts @@ -0,0 +1,39 @@ +import { mergeWithDefaultConfig } from "../../src/ts/config"; +import DefaultConfig from "../../src/ts/constants/default-config"; + +describe("config.ts", () => { + describe("mergeWithDefaultConfig", () => { + it("should carry over properties from the default config", () => { + const partialConfig = {} as Partial; + + const result = mergeWithDefaultConfig(partialConfig); + expect(result).toEqual(expect.objectContaining(DefaultConfig)); + for (const [key, value] of Object.entries(DefaultConfig)) { + expect(result).toHaveProperty(key, value); + } + }); + it("should not merge properties which are not in the default config (legacy properties)", () => { + const partialConfig = { + legacy: true, + } as Partial; + + const result = mergeWithDefaultConfig(partialConfig); + expect(result).toEqual(expect.objectContaining(DefaultConfig)); + expect(result).not.toHaveProperty("legacy"); + }); + it("should correctly merge properties of various types", () => { + const partialConfig = { + mode: "quote", + hideExtraLetters: true, + time: 120, + accountChart: ["off", "off", "off", "off"], + } as Partial; + + const result = mergeWithDefaultConfig(partialConfig); + expect(result.mode).toEqual("quote"); + expect(result.hideExtraLetters).toEqual(true); + expect(result.time).toEqual(120); + expect(result.accountChart).toEqual(["off", "off", "off", "off"]); + }); + }); +}); diff --git a/frontend/src/ts/config.ts b/frontend/src/ts/config.ts index b517a8ecd..edcdbfa2b 100644 --- a/frontend/src/ts/config.ts +++ b/frontend/src/ts/config.ts @@ -15,7 +15,7 @@ import { canSetConfigWithCurrentFunboxes, canSetFunboxWithConfig, } from "./test/funbox/funbox-validation"; -import { reloadAfter } from "./utils/misc"; +import { reloadAfter, typedKeys } from "./utils/misc"; export let localStorageConfig: MonkeyTypes.Config; @@ -2026,6 +2026,19 @@ export function getConfigChanges(): MonkeyTypes.PresetConfig { return configChanges; } +export function mergeWithDefaultConfig( + config: Partial +): MonkeyTypes.Config { + const mergedConfig = {} as MonkeyTypes.Config; + for (const key of typedKeys(DefaultConfig)) { + const newValue = + config[key] ?? (DefaultConfig[key] as MonkeyTypes.ConfigValue); + //@ts-ignore cant be bothered to deal with this + mergedConfig[key] = newValue; + } + return mergedConfig; +} + export const loadPromise = new Promise((v) => { loadDone = v; });