@@ -88,16 +88,45 @@ export class Bookmark<L extends TItemLocation> {
8888 }
8989
9090 clone ( withHash ?: boolean ) :Bookmark < L > {
91- return new Bookmark ( this )
91+ return Object . create ( this )
9292 }
9393
9494 cloneWithLocation < L2 extends TItemLocation > ( withHash :boolean , location : L2 ) : Bookmark < L2 > {
95+ const newBookmark = Object . create ( this )
96+ newBookmark . location = location
97+ return newBookmark
98+ }
99+
100+ copy ( withHash ?: boolean ) :Bookmark < L > {
101+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
102+ // @ts -ignore
103+ return new Bookmark ( this . toJSON ( ) )
104+ }
105+
106+ copyWithLocation < L2 extends TItemLocation > ( withHash :boolean , location : L2 ) : Bookmark < L2 > {
107+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
108+ // @ts -ignore
95109 return new Bookmark ( {
96- ...this ,
110+ ...this . toJSON ( ) ,
97111 location,
98112 } )
99113 }
100114
115+ toJSON ( ) {
116+ // Flatten inherited properties for serialization
117+ const result = { }
118+ let obj = this
119+ while ( obj ) {
120+ Object . entries ( obj ) . forEach ( ( [ key , value ] ) => {
121+ if ( ! ( key in result ) ) {
122+ result [ key ] = value
123+ }
124+ } )
125+ obj = Object . getPrototypeOf ( obj )
126+ }
127+ return result
128+ }
129+
101130 createIndex ( ) :any {
102131 return { [ this . id ] : this }
103132 }
@@ -309,23 +338,64 @@ export class Folder<L extends TItemLocation> {
309338 return this . hashValue [ String ( preserveOrder ) ]
310339 }
311340
312- clone ( withHash ?:boolean ) :Folder < L > {
341+ copy ( withHash ?:boolean ) :Folder < L > {
342+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
343+ // @ts -ignore
313344 return new Folder ( {
314- ...this ,
345+ ...this . toJSON ( ) ,
315346 ...( ! withHash && { hashValue : { } } ) ,
316- children : this . children . map ( child => child . clone ( withHash ) )
347+ children : this . children . map ( child => child . copy ( withHash ) )
317348 } )
318349 }
319350
320- cloneWithLocation < L2 extends TItemLocation > ( withHash :boolean , location : L2 ) :Folder < L2 > {
351+ copyWithLocation < L2 extends TItemLocation > ( withHash :boolean , location : L2 ) :Folder < L2 > {
352+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
353+ // @ts -ignore
321354 return new Folder ( {
322- ...this ,
355+ ...this . toJSON ( ) ,
323356 location,
324357 ...( ! withHash && { hashValue : { } } ) ,
325- children : this . children . map ( child => child . cloneWithLocation ( withHash , location ) )
358+ children : this . children . map ( child => child . copyWithLocation ( withHash , location ) )
326359 } )
327360 }
328361
362+ clone ( withHash ?:boolean ) :Folder < L > {
363+ const newFolder = Object . create ( this )
364+ newFolder . index = null
365+ if ( ! withHash ) {
366+ newFolder . hashValue = { }
367+ }
368+ newFolder . children = this . children . map ( child => child . clone ( withHash ) )
369+ return newFolder
370+ }
371+
372+ cloneWithLocation < L2 extends TItemLocation > ( withHash :boolean , location : L2 ) :Folder < L2 > {
373+ const newFolder = Object . create ( this )
374+ if ( ! withHash ) {
375+ newFolder . hashValue = { }
376+ }
377+ newFolder . index = null
378+ newFolder . location = location
379+ newFolder . children = this . children . map ( child => child . cloneWithLocation ( withHash , location ) )
380+ return newFolder
381+ }
382+
383+ toJSON ( ) : Folder < L > {
384+ // Flatten inherited properties for serialization
385+ const result : Folder < L > = { } as any as Folder < L >
386+ let obj = this
387+ while ( obj ) {
388+ Object . entries ( obj ) . forEach ( ( [ key , value ] ) => {
389+ if ( key === 'index' ) return
390+ if ( ! ( key in result ) ) {
391+ result [ key ] = value
392+ }
393+ } )
394+ obj = Object . getPrototypeOf ( obj )
395+ }
396+ return result
397+ }
398+
329399 count ( ) :number {
330400 if ( ! this . index ) {
331401 this . createIndex ( )
0 commit comments