@@ -88,16 +88,49 @@ export class Bookmark<L extends TItemLocation> {
8888 }
8989
9090 clone ( withHash ?: boolean ) :Bookmark < L > {
91- return new Bookmark ( this )
91+ const bookmark = Object . create ( this )
92+ if ( ! withHash ) {
93+ bookmark . hashValue = { }
94+ }
95+ return bookmark
9296 }
9397
9498 cloneWithLocation < L2 extends TItemLocation > ( withHash :boolean , location : L2 ) : Bookmark < L2 > {
99+ const newBookmark = Object . create ( this )
100+ newBookmark . location = location
101+ return newBookmark
102+ }
103+
104+ copy ( withHash ?: boolean ) :Bookmark < L > {
105+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
106+ // @ts -ignore
107+ return new Bookmark ( this . toJSON ( ) )
108+ }
109+
110+ copyWithLocation < L2 extends TItemLocation > ( withHash :boolean , location : L2 ) : Bookmark < L2 > {
111+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
112+ // @ts -ignore
95113 return new Bookmark ( {
96- ...this ,
114+ ...this . toJSON ( ) ,
97115 location,
98116 } )
99117 }
100118
119+ toJSON ( ) {
120+ // Flatten inherited properties for serialization
121+ const result = { }
122+ let obj = this
123+ while ( obj ) {
124+ Object . entries ( obj ) . forEach ( ( [ key , value ] ) => {
125+ if ( ! ( key in result ) ) {
126+ result [ key ] = value
127+ }
128+ } )
129+ obj = Object . getPrototypeOf ( obj )
130+ }
131+ return result
132+ }
133+
101134 createIndex ( ) :any {
102135 return { [ this . id ] : this }
103136 }
@@ -309,23 +342,64 @@ export class Folder<L extends TItemLocation> {
309342 return this . hashValue [ String ( preserveOrder ) ]
310343 }
311344
312- clone ( withHash ?:boolean ) :Folder < L > {
345+ copy ( withHash ?:boolean ) :Folder < L > {
346+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
347+ // @ts -ignore
313348 return new Folder ( {
314- ...this ,
349+ ...this . toJSON ( ) ,
315350 ...( ! withHash && { hashValue : { } } ) ,
316- children : this . children . map ( child => child . clone ( withHash ) )
351+ children : this . children . map ( child => child . copy ( withHash ) )
317352 } )
318353 }
319354
320- cloneWithLocation < L2 extends TItemLocation > ( withHash :boolean , location : L2 ) :Folder < L2 > {
355+ copyWithLocation < L2 extends TItemLocation > ( withHash :boolean , location : L2 ) :Folder < L2 > {
356+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
357+ // @ts -ignore
321358 return new Folder ( {
322- ...this ,
359+ ...this . toJSON ( ) ,
323360 location,
324361 ...( ! withHash && { hashValue : { } } ) ,
325- children : this . children . map ( child => child . cloneWithLocation ( withHash , location ) )
362+ children : this . children . map ( child => child . copyWithLocation ( withHash , location ) )
326363 } )
327364 }
328365
366+ clone ( withHash ?:boolean ) :Folder < L > {
367+ const newFolder = Object . create ( this )
368+ newFolder . index = null
369+ if ( ! withHash ) {
370+ newFolder . hashValue = { }
371+ }
372+ newFolder . children = this . children . map ( child => child . clone ( withHash ) )
373+ return newFolder
374+ }
375+
376+ cloneWithLocation < L2 extends TItemLocation > ( withHash :boolean , location : L2 ) :Folder < L2 > {
377+ const newFolder = Object . create ( this )
378+ if ( ! withHash ) {
379+ newFolder . hashValue = { }
380+ }
381+ newFolder . index = null
382+ newFolder . location = location
383+ newFolder . children = this . children . map ( child => child . cloneWithLocation ( withHash , location ) )
384+ return newFolder
385+ }
386+
387+ toJSON ( ) : Folder < L > {
388+ // Flatten inherited properties for serialization
389+ const result : Folder < L > = { } as any as Folder < L >
390+ let obj = this
391+ while ( obj ) {
392+ Object . entries ( obj ) . forEach ( ( [ key , value ] ) => {
393+ if ( key === 'index' ) return
394+ if ( ! ( key in result ) ) {
395+ result [ key ] = value
396+ }
397+ } )
398+ obj = Object . getPrototypeOf ( obj )
399+ }
400+ return result
401+ }
402+
329403 count ( ) :number {
330404 if ( ! this . index ) {
331405 this . createIndex ( )
0 commit comments