@@ -74,6 +74,10 @@ export class Bookmark<L extends TItemLocation> {
7474 return false
7575 }
7676
77+ childrenSimilarity < L2 extends TItemLocation > ( otherItem : TItem < L2 > ) : number {
78+ return 0
79+ }
80+
7781 async hash ( ) :Promise < string > {
7882 if ( ! this . hashValue ) {
7983 this . hashValue = await Crypto . sha256 (
@@ -107,7 +111,7 @@ export class Bookmark<L extends TItemLocation> {
107111 }
108112
109113 // TODO: Make this return the correct type based on the type param
110- findItemFilter ( type :TItemType , fn :( Item ) => boolean ) :TItem < L > | null {
114+ findItemFilter ( type :TItemType , fn :( item : TItem < L > ) => boolean , prefer : ( item : TItem < L > ) => number = ( ) => 1 ) :TItem < L > | null {
111115 if ( type === ItemType . BOOKMARK && fn ( this ) ) {
112116 return this
113117 }
@@ -185,11 +189,13 @@ export class Folder<L extends TItemLocation> {
185189 }
186190
187191 // eslint-disable-next-line no-use-before-define
188- findItemFilter ( type :TItemType , fn :( Item ) => boolean ) :TItem < L > | null {
192+ findItemFilter ( type :TItemType , fn :( Item ) => boolean , prefer : ( Item ) => number = ( ) => 1 ) :TItem < L > | null {
189193 if ( ! this . index ) {
190194 this . createIndex ( )
191195 }
192- return Object . values ( this . index [ type ] ) . find ( fn )
196+ const candidates = Object . values ( this . index [ type ] ) . filter ( fn )
197+ // return the preferred match based on a preference measure
198+ return candidates . sort ( ( a , b ) => prefer ( a ) - prefer ( b ) ) . pop ( )
193199 }
194200
195201 findFolder ( id :string | number ) : Folder < L > {
@@ -256,6 +262,17 @@ export class Folder<L extends TItemLocation> {
256262 return false
257263 }
258264
265+ childrenSimilarity < L2 extends TItemLocation > ( otherItem : TItem < L2 > ) : number {
266+ if ( otherItem instanceof Folder ) {
267+ return this . children . reduce (
268+ ( count , item ) =>
269+ otherItem . children . filter ( i => i . title === item . title ) . length > 0 ? count + 1 : count ,
270+ 0
271+ ) / Math . max ( this . children . length , otherItem . children . length )
272+ }
273+ return 0
274+ }
275+
259276 async hash ( preserveOrder = false ) : Promise < string > {
260277 if ( this . hashValue && this . hashValue [ String ( preserveOrder ) ] ) {
261278 return this . hashValue [ String ( preserveOrder ) ]
0 commit comments