krisk / Fuse

Lightweight fuzzy-search, in JavaScript
https://fusejs.io/
Apache License 2.0
17.77k stars 754 forks source link

Invalid typescript definition for FuseSortFunctionMatch #613

Closed hoonoh closed 2 years ago

hoonoh commented 2 years ago

Describe the bug

Typescript definition for FuseSortFunctionMatch seems invalid / outdated.

// src/index.d.ts
  export type FuseSortFunctionMatch = {
    score: number
    key: string // <-
    value: string
    indices: ReadonlyArray<number>[]
  }

For my use case the type definitions had to patched to:

  export type FuseSortFunctionMatch = {
    score: number
    key: {                  // <- is not string
      path: string[]
      id: string
      weight: number
      src: string
    }
    norm: number        // <- also missing
    value: string
    indices: ReadonlyArray<number>[]
  }

Version

6.5.3

🔬Minimal Reproduction

The setup I used:

const fuse = new Fuse(html, {
  keys: [
    {
      name: 'breadcrumb',
      weight: 2,
    },
    {
      name: 'text',
      weight: 1,
    },
  ],
  includeScore: true,
  ignoreLocation: true,
  useExtendedSearch: true,
  includeMatches: true,
  sortFn: (a, b) => {
    console.log(JSON.stringify(a.matches, null, 2));
  }
);

/*
outputs:
...
[
  {
    "score": 0,
    "key": {
      "path": [
        "breadcrumb"
      ],
      "id": "breadcrumb",
      "weight": 2,
      "src": "breadcrumb"
    },
    "value": "some value",
    "idx": 0,
    "norm": 0.577,
    "indices": [
      [
        3,
        5
      ],
      [
        7,
        10
      ],
      [
        7,
        10
      ]
    ]
  }
]
*/

Additional context

I was trying to add custom sort function which needs to check the key value and found out it was not string but an object. I am not sure if the sortFn argument's key can be a string in a different setup.

github-actions[bot] commented 2 years ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

hoonoh commented 2 years ago

bump.

hoonoh commented 2 years ago

I don't think the stale bot is functioning as expected. I would be happy to add a PR with types changed as:

  export type FuseSortFunctionMatch = {
    score: number
    key: string | { // <- changes
      path: string[]
      id: string
      weight: number
      src: string
    }
    norm: number // <- changes
    value: string
    indices: ReadonlyArray<number>[]
  }

...but I am not sure if this would introduce any side effects. Any thoughts?