From 12d5d9f9ee60be088c0d333f343c52e84a8bb143 Mon Sep 17 00:00:00 2001 From: Markus Ahlstrand Date: Thu, 1 Aug 2024 13:45:43 +0200 Subject: [PATCH] skip recursion for unflatten --- .changeset/few-bears-move.md | 5 +++ packages/kysely/src/flatten.ts | 56 ++++++---------------------- packages/kysely/test/flatten.spec.ts | 33 +++++++++++++--- 3 files changed, 44 insertions(+), 50 deletions(-) create mode 100644 .changeset/few-bears-move.md diff --git a/.changeset/few-bears-move.md b/.changeset/few-bears-move.md new file mode 100644 index 0000000..72604c8 --- /dev/null +++ b/.changeset/few-bears-move.md @@ -0,0 +1,5 @@ +--- +"@authhero/kysely-adapter": patch +--- + +Skip recursion for unflatten diff --git a/packages/kysely/src/flatten.ts b/packages/kysely/src/flatten.ts index 4233cd3..ed9e7e1 100644 --- a/packages/kysely/src/flatten.ts +++ b/packages/kysely/src/flatten.ts @@ -37,55 +37,23 @@ export function flattenObject(obj, prefix = "", res = {}) { export function unflattenObject( flatObj: { [key: string]: any }, - prefixes?: string[], // Make prefixes optional + prefixes: string[], ): { [key: string]: any } { const result: { [key: string]: any } = {}; - // Ensure prefixes is a valid array - if (!Array.isArray(prefixes)) { - prefixes = []; - } - for (const [key, value] of Object.entries(flatObj)) { - let target = result; - const parts = key.split("_"); - let isPrefixed = false; - - parts.forEach((part, i) => { - const prefix = parts.slice(0, i + 1).join("_"); - - if (prefixes.includes(prefix) && i < parts.length - 1) { - isPrefixed = true; - if (!target[part]) { - target[part] = {}; - } - target = target[part]; - } else { - if (i === parts.length - 1) { - target[part] = value; - } else { - if (!target[part]) { - target[part] = {}; - } - target = target[part]; - } - } - }); + const matchingPrefix = prefixes.find((prefix) => + key.startsWith(`${prefix}_`), + ); - // Handle case where no prefix matched but it's not the last part - if (!isPrefixed && parts.length > 1) { - let current = result; - - parts.forEach((part, index) => { - if (index === parts.length - 1) { - current[part] = value; - } else { - if (!current[part]) { - current[part] = {}; - } - current = current[part]; - } - }); + if (!matchingPrefix) { + result[key] = value; + } else { + const newKey = key.slice(matchingPrefix.length + 1); + result[matchingPrefix] = { + ...result[matchingPrefix], + [newKey]: value, + }; } } diff --git a/packages/kysely/test/flatten.spec.ts b/packages/kysely/test/flatten.spec.ts index a469d04..cc129ac 100644 --- a/packages/kysely/test/flatten.spec.ts +++ b/packages/kysely/test/flatten.spec.ts @@ -29,16 +29,37 @@ describe("flatten", () => { }); it("should unflatten a object with null, undefined and integers", () => { - const unflattened = unflattenObject({ - a: "a", - b_integer: 1, - b_null: null, - b_undefined: undefined, - }); + const unflattened = unflattenObject( + { + a: "a", + b_integer: 1, + b_null: null, + b_undefined: undefined, + }, + ["b"], + ); expect(unflattened).toEqual({ a: "a", b: { null: null, undefined: undefined, integer: 1 }, }); }); + + it("should unflatten the authParams", () => { + const unflattened = unflattenObject( + { + authParams_client_id: "client_id", + authParams_expires_at: "2021-01-01", + created_at: "2021-01-01", + }, + ["authParams"], + ); + expect(unflattened).toEqual({ + authParams: { + client_id: "client_id", + expires_at: "2021-01-01", + }, + created_at: "2021-01-01", + }); + }); }); });