Skip to content

Commit 653e0b5

Browse files
committed
fix unflattening of array indices
1 parent 7f9091f commit 653e0b5

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

packages/core/src/v3/utils/flattenAttributes.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,26 @@ export function unflattenAttributes(
9595
const result: Record<string, unknown> = {};
9696

9797
for (const [key, value] of Object.entries(obj)) {
98-
const parts = key.split(".").reduce((acc, part) => {
99-
if (part.includes("[")) {
100-
// Handling nested array indices
101-
const subparts = part.split(/\[|\]/).filter((p) => p !== "");
102-
acc.push(...subparts);
103-
} else {
104-
acc.push(part);
105-
}
106-
return acc;
107-
}, [] as string[]);
98+
const parts = key.split(".").reduce(
99+
(acc, part) => {
100+
if (part.includes("[")) {
101+
// Handling nested array indices
102+
const match = part.match(/^\[(\d+)\]$/);
103+
const number = match?.[1];
104+
105+
if (number) {
106+
acc.push(parseInt(number));
107+
return acc;
108+
}
109+
110+
acc.push(part.replace("[", "").replace("]", ""));
111+
} else {
112+
acc.push(part);
113+
}
114+
return acc;
115+
},
116+
[] as (string | number)[]
117+
);
108118

109119
let current: any = result;
110120
for (let i = 0; i < parts.length - 1; i++) {
@@ -115,7 +125,7 @@ export function unflattenAttributes(
115125
}
116126

117127
const nextPart = parts[i + 1];
118-
const isArray = nextPart && /^\d+$/.test(nextPart);
128+
const isArray = typeof nextPart === "number";
119129
if (isArray && !Array.isArray(current[part])) {
120130
current[part] = [];
121131
} else if (!isArray && current[part] === undefined) {

0 commit comments

Comments
 (0)