Skip to content

Commit e5be66f

Browse files
Update flattenAttributes.ts
1 parent d0529a3 commit e5be66f

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

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

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -118,26 +118,21 @@ export function unflattenAttributes(
118118
const result: Record<string, unknown> = {};
119119

120120
for (const [key, value] of Object.entries(obj)) {
121-
const parts = key
122-
.split(/(?<!\\)\./) // Split by unescaped dots
123-
.map(unescapeKey) // Unescape keys
124-
.reduce((acc, part) => {
125-
if (part.startsWith("[") && part.endsWith("]")) {
126-
// Handle array indices more precisely
127-
const match = part.match(/^\[(\d+)\]$/);
128-
if (match && match[1]) {
129-
acc.push(parseInt(match[1]));
130-
} else {
131-
// Remove brackets for non-numeric array keys
132-
acc.push(part.slice(1, -1));
133-
}
134-
} else {
135-
acc.push(part);
136-
}
137-
return acc;
138-
},
139-
[] as (string | number)[]
140-
);
121+
const parts = key
122+
.split('.') // Split by all dots
123+
.reduce((acc, part, i, arr) => {
124+
// If the previous part ends with an odd number of backslashes,
125+
// the dot is escaped and should be part of the last segment.
126+
if (i > 0 && arr[i - 1].match(/\\+$/)?.[0]?.length % 2 === 1) {
127+
acc[acc.length - 1] += '.' + part;
128+
} else {
129+
acc.push(part);
130+
}
131+
return acc;
132+
}, [] as (string | number)[])
133+
.map(unescapeKey); // Unescape keys
134+
}
135+
);
141136

142137
let current: any = result;
143138
for (let i = 0; i < parts.length - 1; i++) {

0 commit comments

Comments
 (0)