Skip to content

Commit

Permalink
🐛 Refactor getDeepKeys to improve key parsing logic
Browse files Browse the repository at this point in the history
Closes #1852
  • Loading branch information
baptisteArno committed Oct 21, 2024
1 parent b3c43b1 commit 4fdc80f
Showing 1 changed file with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const getDeepKeys = (obj: any): string[] => {
let keys: string[] = [];
for (const key in obj) {
if (typeof obj[key] === "object" && !Array.isArray(obj[key])) {
const subkeys = getDeepKeys(obj[key]);
keys = keys.concat(subkeys.map((subkey) => key + parseKey(subkey)));
keys = keys.concat(
subkeys.map(
(subkey) => `${parsePrevKey(key, subkey)}${parseNextKey(subkey)}`,
),
);
} else if (Array.isArray(obj[key])) {
if (obj[key].length === 0) continue;

Expand All @@ -20,12 +23,16 @@ export const getDeepKeys = (obj: any): string[] => {
if (obj[key].length > 1) {
keys = keys.concat(
subkeys.map(
(subkey) => `${key}.flatMap(item => item${parseKey(subkey)})`,
(subkey) =>
`${key}.flatMap(item => ${parsePrevKey("item", subkey)}${parseNextKey(subkey)})`,
),
);
}
keys = keys.concat(
subkeys.map((subkey) => `${key}[0]${parseKey(subkey)}`),
subkeys.map(
(subkey) =>
`${key}${parsePrevKey("0", subkey)}${parseNextKey(subkey)}`,
),
);
} else {
keys.push(key);
Expand All @@ -34,7 +41,18 @@ export const getDeepKeys = (obj: any): string[] => {
return keys;
};

const parseKey = (key: string) => {
const parsePrevKey = (key: string, suffix: string) => {
if (
key.includes(" ") ||
key.includes("-") ||
!isNaN(key as unknown as number)
) {
return `['${key}'].`;
}
return suffix.startsWith("[") ? key : `${key}.`;
};

const parseNextKey = (key: string) => {
if (
(key.includes(" ") ||
key.includes("-") ||
Expand All @@ -45,5 +63,5 @@ const parseKey = (key: string) => {
) {
return `['${key}']`;
}
return `.${key}`;
return `${key}`;
};

0 comments on commit 4fdc80f

Please sign in to comment.