@@ -5,7 +5,7 @@ const maxDepthSignal = { __stop__: true };
55 */
66export default function convert (
77 classObject : { [ key : string ] : any } ,
8- maxDepth : number = 10 ,
8+ maxDepth : number = 5 ,
99) : object {
1010 const visited = new WeakSet < any > ( ) ;
1111 visited . add ( classObject ) ;
@@ -14,19 +14,23 @@ export default function convert(
1414 const convertedObj : { [ key : string ] : any } = { } ;
1515 for ( const key in classObject ) {
1616 // Skip keys that cannot be converted
17- if ( shouldIgnoreValue ( classObject [ key ] , key ) ) {
18- continue ;
19- }
20- // Handle objects (potentially cyclical)
21- else if ( typeof classObject [ key ] === "object" ) {
22- const result = deepCloneClass ( classObject [ key ] , maxDepth , visited ) ;
23- if ( result !== maxDepthSignal ) {
24- convertedObj [ key ] = result ;
17+ try {
18+ if ( shouldIgnoreValue ( classObject [ key ] , key ) ) {
19+ continue ;
2520 }
26- }
27- // Handle simple types (non-cyclical)
28- else {
29- convertedObj [ key ] = classObject [ key ] ;
21+ // Handle objects (potentially cyclical)
22+ else if ( typeof classObject [ key ] === "object" ) {
23+ const result = deepCloneClass ( classObject [ key ] , maxDepth , visited ) ;
24+ if ( result !== maxDepthSignal ) {
25+ convertedObj [ key ] = result ;
26+ }
27+ }
28+ // Handle simple types (non-cyclical)
29+ else {
30+ convertedObj [ key ] = classObject [ key ] ;
31+ }
32+ } catch ( e ) {
33+ continue ;
3034 }
3135 }
3236
@@ -87,6 +91,11 @@ function deepCloneClass(
8791 return maxDepthSignal ;
8892 }
8993
94+ // Safety check: WeakSet only accepts objects (and not null)
95+ if ( ! x || typeof x !== "object" ) {
96+ return x ;
97+ }
98+
9099 if ( visited . has ( x ) ) {
91100 return maxDepthSignal ;
92101 }
@@ -152,20 +161,24 @@ function classToObject(
152161) : object {
153162 const result : { [ key : string ] : any } = { } ;
154163 for ( const key in x ) {
155- // Skip anything that should not be converted
156- if ( shouldIgnoreValue ( x [ key ] , key , x ) ) {
157- continue ;
158- }
159- // Add objects as a property if we haven't reached max depth
160- else if ( typeof x [ key ] === "object" ) {
161- const converted = deepCloneClass ( x [ key ] , maxDepth , visited ) ;
162- if ( converted !== maxDepthSignal ) {
163- result [ key ] = converted ;
164+ try {
165+ // Skip anything that should not be converted
166+ if ( shouldIgnoreValue ( x [ key ] , key , x ) ) {
167+ continue ;
164168 }
165- }
166- // Add plain values if not skippable
167- else {
168- result [ key ] = x [ key ] ;
169+ // Add objects as a property if we haven't reached max depth
170+ else if ( typeof x [ key ] === "object" ) {
171+ const converted = deepCloneClass ( x [ key ] , maxDepth , visited ) ;
172+ if ( converted !== maxDepthSignal ) {
173+ result [ key ] = converted ;
174+ }
175+ }
176+ // Add plain values if not skippable
177+ else {
178+ result [ key ] = x [ key ] ;
179+ }
180+ } catch ( e ) {
181+ continue ;
169182 }
170183 }
171184
0 commit comments