Skip to content

Commit

Permalink
skip recursion for unflatten
Browse files Browse the repository at this point in the history
  • Loading branch information
markusahlstrand committed Aug 1, 2024
1 parent 9452aa8 commit 12d5d9f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 50 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-bears-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@authhero/kysely-adapter": patch
---

Skip recursion for unflatten
56 changes: 12 additions & 44 deletions packages/kysely/src/flatten.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
}

Expand Down
33 changes: 27 additions & 6 deletions packages/kysely/test/flatten.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
});
});
});
});

0 comments on commit 12d5d9f

Please sign in to comment.