question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fuse.search doesn't return only T[]

See original GitHub issue

Current TS definition says,

search(pattern: string): T[]

But with includeScore, the search returns: search(pattern: string): T[] | { item: T, score: number }[];

For now, I am ignoring the return type with // @ts-ignore.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:11
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
william-lohancommented, Nov 14, 2018

There is more:

with id search(pattern: string): string[];

with includeMatches search(pattern: string): { item: T; matches: any; }[];

with id and includeMatches search(pattern: string): { item: string; matches: any; }[];

its really ugly but modifying the definition file like this seems to work

export = Fuse;
export as namespace Fuse;

declare class Fuse<T, O extends Fuse.FuseOptions<T> = Fuse.FuseOptions<T>> {
  constructor(list: ReadonlyArray<T>, options?: O)
  search(pattern: string): O extends { id: keyof T } ?
    O extends ({ includeMatches: true; } | { includeScore: true; }) ? Fuse.FuseResult<string>[] : string[] :
    O extends ({ includeMatches: true; } | { includeScore: true; }) ? Fuse.FuseResult<T>[] : T[];

  setCollection(list: ReadonlyArray<T>): ReadonlyArray<T>;
}

declare namespace Fuse {
  export interface FuseResult<T> {
    item: T,
    matches?: any;
    score?: number;
  }
  export interface FuseOptions<T> {
    id?: keyof T;
    caseSensitive?: boolean;
    includeMatches?: boolean;
    includeScore?: boolean;
    shouldSort?: boolean;
    sortFn?: (a: { score: number }, b: { score: number }) => number;
    getFn?: (obj: any, path: string) => any;
    keys?: (keyof T)[] | { name: keyof T; weight: number }[];
    verbose?: boolean;
    tokenize?: boolean;
    tokenSeparator?: RegExp;
    matchAllTokens?: boolean;
    location?: number;
    distance?: number;
    threshold?: number;
    maxPatternLength?: number;
    minMatchCharLength?: number;
    findAllMatches?: boolean;
  }
}
2reactions
zhouzicommented, Oct 29, 2019

@william-lohan thank you! 👍 Following your example, I got it working like so:

interface User {
  email: string;
}
const users: User[] = [
  {
    email: 'jane@doe.com'
  },
  {
    email: 'john@doe.com'
  }
];
const matchingUsers: User[] =
  new Fuse(users, {
    includeScore: true,
    shouldSort: false,
    keys: ['email']
  })
    .search('jane')
    .map(result => result.item);

There are two important limitations: Fuse.FuseOptions cannot be used and the options object has to be inlined. For example, the following doesn’t work:

interface User {
  email: string;
}
const users: User[] = [
  {
    email: 'jane@doe.com'
  },
  {
    email: 'john@doe.com'
  }
];
const FUSE_OPTIONS = {
  includeScore: true,
  shouldSort: false,
  keys: ['email']
};
const matchingUsers: User[] =
  new Fuse(users, FUSE_OPTIONS)
    .search('jane')
    .map(result => result.item);

Anyway, I believe the initial issue has been fixed and can be closed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Not getting any results back from Fuse.js with Vue
my keys are populated properly and the only issue is that results doesn't return anything.
Read more >
Options | Fuse.js
The default options only search the first 60 characters. This should suffice if it is reasonably expected that the match is within this...
Read more >
How to Add Search to a React App with Fuse.js - freeCodeCamp
Step 0: Bootstrapping our app; Step 1: Installing Fuse.js; Step 2: Creating a new Fuse search instance; Step 3: Setting up dynamic search ......
Read more >
Autocomplete with fuzzy search and Fuse.js | Academy
A simple way to add autocomplete to an input element is to use a <datalist> . This doesn't even require any JavaScript, just...
Read more >
CS135 FUSE Documentation
If you aren't using file handles, this function should just check for existence and permissions and return either success or an error code....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found