LCOV - code coverage report
Current view: top level - src/utils - fuseUtil.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 96.3 % 81 78
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type { FuseOptionKey, FuseSearchOptions, IFuseOptions } from 'fuse.js'
       2              : import Fuse from 'fuse.js'
       3              : 
       4              : export type SearchAuxScore = number[]
       5              : 
       6              : export interface FuseFilterWithValue<T, O = string> {
       7              :   filterDef: FuseFilter<T, O>
       8              :   value: O
       9              : }
      10              : 
      11         2356 : export class FuseFilter<T, O = string> {
      12              :   public readonly fuseSearch: FuseSearch<O>
      13              :   /** The unique identifier for the filter. */
      14              :   public readonly id: string
      15              :   /** The name of the filter for display purposes. */
      16              :   public readonly name: string
      17              :   /** The sequence of characters to invoke the filter. */
      18              :   public readonly invokeSequence: string
      19              :   /** A function that returns the options for the filter. */
      20              :   public readonly getItemOptions: (item: T) => O[]
      21              : 
      22              :   constructor(
      23              :     data: T[],
      24              :     options: {
      25              :       id: string
      26              :       name: string
      27              :       invokeSequence: string
      28              :       getItemOptions: (item: T) => O[]
      29              :       fuseOptions?: IFuseOptions<O>
      30              :     }
      31              :   ) {
      32        23852 :     this.id = options.id
      33        23852 :     this.name = options.name
      34        23852 :     this.invokeSequence = options.invokeSequence
      35        23852 :     this.getItemOptions = options.getItemOptions
      36              : 
      37        23852 :     this.fuseSearch = new FuseSearch(this.getAllNodeOptions(data), {
      38              :       fuseOptions: options.fuseOptions
      39              :     })
      40              :   }
      41              : 
      42              :   public getAllNodeOptions(data: T[]): O[] {
      43        23852 :     const options = new Set<O>()
      44        23852 :     for (const item of data) {
      45     24673344 :       for (const option of this.getItemOptions(item)) {
      46     39300294 :         options.add(option)
      47              :       }
      48              :     }
      49        23852 :     return Array.from(options)
      50              :   }
      51              : 
      52              :   public matches(
      53              :     item: T,
      54              :     value: O,
      55              :     extraOptions: {
      56              :       wildcard?: O
      57              :     } = {}
      58              :   ): boolean {
      59       321707 :     const { wildcard } = extraOptions
      60              : 
      61       321707 :     if (wildcard && value === wildcard) {
      62            0 :       return true
      63              :     }
      64       321707 :     const options = this.getItemOptions(item)
      65       321707 :     if (wildcard) return options.some((option) => option === wildcard)
      66       321707 :     if (typeof value !== 'string' || !value.includes(','))
      67       165674 :       return options.includes(value)
      68              :     const values = value.split(',')
      69              :     //Alas, typescript doesn't understand string satisfies O
      70            0 :     return values.some((v) => options.includes(v as O))
      71              :   }
      72              : }
      73              : 
      74              : export interface FuseSearchable {
      75              :   postProcessSearchScores: (scores: SearchAuxScore) => SearchAuxScore
      76              : }
      77              : 
      78      2502496 : function isFuseSearchable(item: unknown): item is FuseSearchable {
      79      2502496 :   return (
      80              :     typeof item === 'object' &&
      81              :     item !== null &&
      82              :     'postProcessSearchScores' in item
      83              :   )
      84              : }
      85              : 
      86              : /**
      87              :  * A wrapper around Fuse.js that provides a more type-safe API.
      88              :  */
      89         2356 : export class FuseSearch<T> {
      90              :   public readonly fuse: Fuse<T>
      91              :   public readonly keys: FuseOptionKey<T>[]
      92              :   public readonly data: T[]
      93              :   public readonly advancedScoring: boolean
      94              : 
      95              :   constructor(
      96              :     data: T[],
      97              :     options: {
      98              :       fuseOptions?: IFuseOptions<T>
      99              :       createIndex?: boolean
     100              :       advancedScoring?: boolean
     101              :     }
     102              :   ) {
     103        29815 :     const { fuseOptions, createIndex = true, advancedScoring = false } = options
     104              : 
     105        29815 :     this.data = data
     106        29815 :     this.keys = fuseOptions?.keys ?? []
     107        29815 :     this.advancedScoring = advancedScoring
     108        29815 :     const index =
     109              :       createIndex && this.keys.length
     110              :         ? Fuse.createIndex(this.keys, data)
     111              :         : undefined
     112        29815 :     this.fuse = new Fuse(data, fuseOptions, index)
     113              :   }
     114              : 
     115              :   public search(query: string, options?: FuseSearchOptions): T[] {
     116         2858 :     const fuseResult = !query
     117              :       ? this.data.map((x) => ({ item: x, score: 0 }))
     118              :       : this.fuse.search(query, options)
     119              : 
     120         2858 :     if (!this.advancedScoring) {
     121            9 :       return fuseResult.map((x) => x.item)
     122              :     }
     123              : 
     124              :     const aux = fuseResult
     125              :       .map((x) => ({
     126              :         item: x.item,
     127              :         scores: this.calcAuxScores(
     128              :           query.toLocaleLowerCase(),
     129              :           x.item,
     130              :           x.score ?? 0
     131              :         )
     132              :       }))
     133              :       .sort((a, b) => this.compareAux(a.scores, b.scores))
     134              : 
     135         2849 :     return aux.map((x) => x.item)
     136              :   }
     137              : 
     138              :   public calcAuxScores(query: string, entry: T, score: number): SearchAuxScore {
     139      2502496 :     let values: string[] = []
     140      2502496 :     if (typeof entry === 'string') {
     141            0 :       values = [entry]
     142      2502496 :     } else if (typeof entry === 'object' && entry !== null) {
     143      2502496 :       values = this.keys
     144              :         .map((x) => entry[x as keyof T])
     145              :         .filter((x) => typeof x === 'string')
     146              :     }
     147              :     const scores = values.map((x) => this.calcAuxSingle(query, x, score))
     148      2502496 :     let result = scores.sort(this.compareAux)[0]
     149              : 
     150      2502496 :     const deprecated = values.some((x) =>
     151              :       x.toLocaleLowerCase().includes('deprecated')
     152              :     )
     153      2502496 :     result[0] += deprecated && result[0] !== 0 ? 5 : 0
     154      2502496 :     if (isFuseSearchable(entry)) {
     155      2502496 :       result = entry.postProcessSearchScores(result)
     156              :     }
     157      2502496 :     return result
     158              :   }
     159              : 
     160              :   public calcAuxSingle(
     161              :     query: string,
     162              :     item: string,
     163              :     score: number
     164              :   ): SearchAuxScore {
     165      5004992 :     const itemWords = item
     166              :       .split(/ |\b|(?<=[a-z])(?=[A-Z])|(?=[A-Z][a-z])/)
     167              :       .map((x) => x.toLocaleLowerCase())
     168      5004992 :     const queryParts = query.split(' ')
     169      5004992 :     item = item.toLocaleLowerCase()
     170              : 
     171      5004992 :     let main = 9
     172      5004992 :     let aux1 = 0
     173      5004992 :     let aux2 = 0
     174              : 
     175      5004992 :     if (item == query) {
     176          499 :       main = 0
     177      5004493 :     } else if (item.startsWith(query)) {
     178      4995838 :       main = 1
     179      4995838 :       aux2 = item.length
     180         8655 :     } else if (itemWords.includes(query)) {
     181           53 :       main = 2
     182           53 :       aux1 = item.indexOf(query) + item.length * 0.5
     183           53 :       aux2 = item.length
     184         8602 :     } else if (item.includes(query)) {
     185         1322 :       main = 3
     186         1322 :       aux1 = item.indexOf(query) + item.length * 0.5
     187         1322 :       aux2 = item.length
     188         7280 :     } else if (queryParts.every((x) => itemWords.includes(x))) {
     189          396 :       const indexes = queryParts.map((x) => itemWords.indexOf(x))
     190          396 :       const min = Math.min(...indexes)
     191          396 :       const max = Math.max(...indexes)
     192          396 :       main = 4
     193          396 :       aux1 = max - min + max * 0.5 + item.length * 0.5
     194          396 :       aux2 = item.length
     195         6884 :     } else if (queryParts.every((x) => item.includes(x))) {
     196           53 :       const min = Math.min(...queryParts.map((x) => item.indexOf(x)))
     197           53 :       const max = Math.max(...queryParts.map((x) => item.indexOf(x) + x.length))
     198           53 :       main = 5
     199           53 :       aux1 = max - min + max * 0.5 + item.length * 0.5
     200           53 :       aux2 = item.length
     201              :     }
     202              : 
     203      5004992 :     const lengthPenalty =
     204              :       0.2 *
     205              :       (1 -
     206              :         Math.min(item.length, query.length) /
     207              :           Math.max(item.length, query.length))
     208      5004992 :     return [main, aux1, aux2, score + lengthPenalty]
     209              :   }
     210              : 
     211              :   public compareAux(a: SearchAuxScore, b: SearchAuxScore) {
     212     22032461 :     for (let i = 0; i < Math.min(a.length, b.length); i++) {
     213     91190064 :       if (a[i] !== b[i]) {
     214     15593415 :         return a[i] - b[i]
     215              :       }
     216              :     }
     217      6439046 :     return a.length - b.length
     218              :   }
     219              : }
        

Generated by: LCOV version 2.0-1