@@ -95,36 +95,47 @@ export function unflattenAttributes(
95
95
const result : Record < string , unknown > = { } ;
96
96
97
97
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 . startsWith ( "[" ) && part . endsWith ( "]" ) ) {
101
+ // Handle array indices more precisely
102
+ const match = part . match ( / ^ \[ ( \d + ) \] $ / ) ;
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
+ }
109
+ } else {
110
+ acc . push ( part ) ;
111
+ }
112
+ return acc ;
113
+ } ,
114
+ [ ] as ( string | number ) [ ]
115
+ ) ;
108
116
109
117
let current : any = result ;
110
118
for ( let i = 0 ; i < parts . length - 1 ; i ++ ) {
111
119
const part = parts [ i ] ;
120
+ const nextPart = parts [ i + 1 ] ;
112
121
113
- if ( ! part ) {
122
+ if ( ! part && part !== 0 ) {
114
123
continue ;
115
124
}
116
125
117
- const nextPart = parts [ i + 1 ] ;
118
- const isArray = nextPart && / ^ \d + $ / . test ( nextPart ) ;
119
- if ( isArray && ! Array . isArray ( current [ part ] ) ) {
120
- current [ part ] = [ ] ;
121
- } 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
122
131
current [ part ] = { } ;
123
132
}
133
+
124
134
current = current [ part ] ;
125
135
}
136
+
126
137
const lastPart = parts [ parts . length - 1 ] ;
127
- if ( lastPart ) {
138
+ if ( lastPart !== undefined ) {
128
139
current [ lastPart ] = rehydrateNull ( value ) ;
129
140
}
130
141
}
0 commit comments