From c757d598845158c06b95cf4e63e05d0876df1198 Mon Sep 17 00:00:00 2001 From: Greg Smith Date: Thu, 12 Jun 2014 11:36:40 -0500 Subject: [PATCH] Add type definitions for Fuse.js --- fuse/fuse-tests.ts | 73 ++++++++++++++++++++++++++++++++++++++++++++++ fuse/fuse.d.ts | 28 ++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 fuse/fuse-tests.ts create mode 100644 fuse/fuse.d.ts diff --git a/fuse/fuse-tests.ts b/fuse/fuse-tests.ts new file mode 100644 index 0000000000..3aa5576fe7 --- /dev/null +++ b/fuse/fuse-tests.ts @@ -0,0 +1,73 @@ +/// + +function test_fuse_find_identifiers() { + var books = [{ + id: 1, + title: 'The Great Gatsby', + author: 'F. Scott Fitzgerald' + }, { + id: 2, + title: 'The DaVinci Code', + author: 'Dan Brown' + }, { + id: 3, + title: 'Angels & Demons', + author: 'Dan Brown' + }]; + var options = { + keys: ['author', 'title'], // keys to search in + id: 'id' // return a list of identifiers only + }; + var f = new Fuse(books, options); + var result = f.search('brwn'); // Fuzzy-search for pattern 'brwn' +} + +function test_fuse_find_records() { + var books = [{ + id: 1, + title: 'The Great Gatsby', + author: 'F. Scott Fitzgerald' + }, { + id: 2, + title: 'The DaVinci Code', + author: 'Dan Brown' + }, { + id: 3, + title: 'Angels & Demons', + author: 'Dan Brown' + }]; + var options = { + keys: ['author', 'title'] + }; + var f = new Fuse(books, options); + var result = f.search('brwn'); +} + +function test_fuse_flat_array() { + var books = ["Old Man's War", "The Lock Artist", "HTML5", "Right Ho Jeeves", "The Code of the Wooster", "Thank You Jeeves", "The DaVinci Code", "Angels & Demons", "The Silmarillion", "Syrup", "The Lost Symbol", "The Book of Lies", "Lamb", "Fool", "Incompetence", "Fat", "Colony", "Backwards, Red Dwarf", "The Grand Design", "The Book of Samson", "The Preservationist", "Fallen", "Monster 1959"]; + var f = new Fuse(books); + var result = f.search('Falen'); +} + +function test_fuse_deep_key_search() { + 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: ['author.firstName'] + } + var f = new Fuse(books, options); + var result = f.search('brwn'); +} diff --git a/fuse/fuse.d.ts b/fuse/fuse.d.ts new file mode 100644 index 0000000000..bf175bff6c --- /dev/null +++ b/fuse/fuse.d.ts @@ -0,0 +1,28 @@ +// Type definitions for Fuse.js 1.1.5 +// Project: https://github.com/krisk/Fuse +// Definitions by: Greg Smith +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare class Fuse { + constructor(list: any[], options?: fuse.IFuseOptions); + search(pattern: string): any[]; +} + +declare module fuse { + interface IFuseOptions extends ISearchOptions { + caseSensitive?: boolean; + includeScore?: boolean; + shouldSort?: boolean; + searchFn?: any; + sortFn?: (a: {score: number}, b: {score: number}) => number; + getFn?: (obj: any, path: string) => any; + keys?: string[]; + } + + interface ISearchOptions { + location?: number; + distance?: number; + threshold?: number; + maxPatternLength?: number; + } +}