@@ -5,6 +5,36 @@ export const CIRCULAR_REFERENCE_SENTINEL = "$@circular((";
55
66const DEFAULT_MAX_DEPTH = 128 ;
77
8+ function escapeKey ( key : string ) : string {
9+ return key . replace ( / \\ / g, "\\\\" ) . replace ( / \. / g, "\\." ) ;
10+ }
11+
12+ function unescapeKey ( key : string ) : string {
13+ return key . replace ( / \\ \. / g, "." ) . replace ( / \\ \\ / g, "\\" ) ;
14+ }
15+
16+ function splitKey ( key : string ) : string [ ] {
17+ const parts : string [ ] = [ ] ;
18+ let currentPart = "" ;
19+ for ( let i = 0 ; i < key . length ; i ++ ) {
20+ if ( key [ i ] === "\\" && i + 1 < key . length ) {
21+ if ( key [ i + 1 ] === "." || key [ i + 1 ] === "\\" ) {
22+ currentPart += key [ i + 1 ] ;
23+ i ++ ;
24+ } else {
25+ currentPart += key [ i ] ;
26+ }
27+ } else if ( key [ i ] === "." ) {
28+ parts . push ( currentPart ) ;
29+ currentPart = "" ;
30+ } else {
31+ currentPart += key [ i ] ;
32+ }
33+ }
34+ parts . push ( currentPart ) ;
35+ return parts ;
36+ }
37+
838export function flattenAttributes (
939 obj : unknown ,
1040 prefix ?: string ,
@@ -24,7 +54,7 @@ class AttributeFlattener {
2454 constructor (
2555 private maxAttributeCount ?: number ,
2656 private maxDepth : number = DEFAULT_MAX_DEPTH
27- ) { }
57+ ) { }
2858
2959 get attributes ( ) : Attributes {
3060 return this . result ;
@@ -200,7 +230,8 @@ class AttributeFlattener {
200230 break ;
201231 }
202232
203- const newPrefix = `${ prefix ? `${ prefix } .` : "" } ${ Array . isArray ( obj ) ? `[${ key } ]` : key } ` ;
233+ const escapedKey = Array . isArray ( obj ) ? `[${ key } ]` : escapeKey ( key ) ;
234+ const newPrefix = `${ prefix ? `${ prefix } .` : "" } ${ escapedKey } ` ;
204235
205236 if ( Array . isArray ( value ) ) {
206237 for ( let i = 0 ; i < value . length ; i ++ ) {
@@ -278,9 +309,9 @@ export function unflattenAttributes(
278309 continue ;
279310 }
280311
281- const parts = key . split ( "." ) . reduce (
312+ const parts = splitKey ( key ) . reduce (
282313 ( acc , part ) => {
283- if ( part . startsWith ( "[" ) && part . endsWith ( "]" ) ) {
314+ if ( typeof part === "string" && part . startsWith ( "[" ) && part . endsWith ( "]" ) ) {
284315 // Handle array indices more precisely
285316 const match = part . match ( / ^ \[ ( \d + ) \] $ / ) ;
286317 if ( match && match [ 1 ] ) {
0 commit comments