Add type definitions for Fuse.js

This commit is contained in:
Greg Smith 2014-06-12 11:36:40 -05:00
parent e9be9177f6
commit c757d59884
2 changed files with 101 additions and 0 deletions

73
fuse/fuse-tests.ts Normal file
View File

@ -0,0 +1,73 @@
/// <reference path="fuse.d.ts" />
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');
}

28
fuse/fuse.d.ts vendored Normal file
View File

@ -0,0 +1,28 @@
// Type definitions for Fuse.js 1.1.5
// Project: https://github.com/krisk/Fuse
// Definitions by: Greg Smith <https://github.com/smrq/>
// 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;
}
}