diff --git a/fuse/fuse-tests.ts b/fuse/fuse-tests.ts index bb6d845785..801829d78d 100644 --- a/fuse/fuse-tests.ts +++ b/fuse/fuse-tests.ts @@ -67,7 +67,64 @@ function test_fuse_deep_key_search() { var options = { keys: ['author.firstName'] - } + }; var f = new Fuse(books, options); var result = f.search('brwn'); } + +interface Book { + id?: number; + title?: string; + author?: {firstName: string; lastName: string}; +} + +function test_generic_fuse() { + var books: Book[] = [{ + id: 1, + title: 'The Great Gatsby', + author: { + firstName: 'F. Scott', + lastName: 'Fitzgerald' + } + }, { + title: 'The DaVinci Code', + author: { + firstName: 'Dan', + lastName: 'Brown' + } + }]; + + var options = { + keys: ["title", "author.firstName"] + }; + var f = new Fuse(books, options); + var result = f.search('brwn'); +} + +function test_getFn() { + var books = [{ + id: 1, + title: 'The Great Gatsby', + author: { + firstName: 'F. Scott', + lastName: 'Fitzgerald' + } + }, { + title: 'The DaVinci Code', + author: { + firstName: 'Dan', + lastName: 'Brown' + } + }]; + + var options = { + keys: ["title", "author.firstName"], + getFn: (obj: Book, path: string): string => { + if (!obj) { + return null; + } + return obj.author.firstName; + } + }; + var f = new Fuse(books, options); +} diff --git a/fuse/index.d.ts b/fuse/index.d.ts index 5376144e22..996eaacba2 100644 --- a/fuse/index.d.ts +++ b/fuse/index.d.ts @@ -3,22 +3,41 @@ // Definitions by: Greg Smith // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -declare class Fuse { - constructor(list: any[], options?: fuse.IFuseOptions); - search(pattern: string): any[]; +declare class Fuse { + constructor(list: T[], options?: Fuse.Options); + search(pattern: string): T[]; } -declare namespace fuse { - interface IFuseOptions { +declare namespace Fuse { + interface SearchFnConstructor { + new (pattern: string, options?: SearchOptions): SearchFn; + } + + interface SearchFn { + search(text: string): SearchResult; + } + + interface SearchResult { + readonly isMatch: boolean; + readonly score: number; + } + + interface SearchOptions { + location?: number; + distance?: number; + threshold?: number; + maxPatternLength?: number; + } + + interface Options { id?: string; caseSensitive?: boolean; include?: string[]; shouldSort?: boolean; - searchFn?: any; + searchFn?: SearchFnConstructor; sortFn?: (a: {score: number}, b: {score: number}) => number; - getFn?: (obj: any, path: string) => any; keys?: string[] | { name:string; weight:number} []; - verbose?:boolean; + verbose?:boolean; tokenize?: boolean; tokenSeparator? : RegExp; matchAllTokens?: boolean; @@ -26,5 +45,10 @@ declare namespace fuse { distance?: number; threshold?: number; maxPatternLength?: number; + includeScore?: boolean; + getFn?: (obj: T, path: string) => string; } } + +export = Fuse; +export as namespace Fuse;