@@ -97,17 +97,15 @@ export function unflattenAttributes(
97
97
for ( const [ key , value ] of Object . entries ( obj ) ) {
98
98
const parts = key . split ( "." ) . reduce (
99
99
( acc , part ) => {
100
- if ( part . includes ( "[" ) ) {
101
- // Handling nested array indices
100
+ if ( part . startsWith ( "[" ) && part . endsWith ( "] ") ) {
101
+ // Handle array indices more precisely
102
102
const match = part . match ( / ^ \[ ( \d + ) \] $ / ) ;
103
- const number = match ?. [ 1 ] ;
104
-
105
- if ( number ) {
106
- acc . push ( parseInt ( number ) ) ;
107
- return acc ;
103
+ if ( match && match [ 1 ] ) {
104
+ acc . push ( parseInt ( match [ 1 ] ) ) ;
105
+ } else {
106
+ // Remove brackets for non-numeric array keys
107
+ acc . push ( part . slice ( 1 , - 1 ) ) ;
108
108
}
109
-
110
- acc . push ( part . replace ( "[" , "" ) . replace ( "]" , "" ) ) ;
111
109
} else {
112
110
acc . push ( part ) ;
113
111
}
@@ -119,22 +117,25 @@ export function unflattenAttributes(
119
117
let current : any = result ;
120
118
for ( let i = 0 ; i < parts . length - 1 ; i ++ ) {
121
119
const part = parts [ i ] ;
120
+ const nextPart = parts [ i + 1 ] ;
122
121
123
- if ( ! part ) {
122
+ if ( ! part && part !== 0 ) {
124
123
continue ;
125
124
}
126
125
127
- const nextPart = parts [ i + 1 ] ;
128
- const isArray = typeof nextPart === "number" ;
129
- if ( isArray && ! Array . isArray ( current [ part ] ) ) {
130
- current [ part ] = [ ] ;
131
- } else if ( ! isArray && current [ part ] === undefined ) {
126
+ if ( typeof nextPart === "number" ) {
127
+ // Ensure we create an array for numeric indices
128
+ current [ part ] = Array . isArray ( current [ part ] ) ? current [ part ] : [ ] ;
129
+ } else if ( current [ part ] === undefined ) {
130
+ // Create an object for non-numeric paths
132
131
current [ part ] = { } ;
133
132
}
133
+
134
134
current = current [ part ] ;
135
135
}
136
+
136
137
const lastPart = parts [ parts . length - 1 ] ;
137
- if ( lastPart ) {
138
+ if ( lastPart !== undefined ) {
138
139
current [ lastPart ] = rehydrateNull ( value ) ;
139
140
}
140
141
}
0 commit comments