From 471878d7d7cb716376aebc6e795fbc7729665314 Mon Sep 17 00:00:00 2001 From: Rik Date: Fri, 7 Jun 2019 13:38:57 +0100 Subject: [PATCH 1/2] Beginning of Typescript Definition minimal typescript declaration --- index.d.ts | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 index.d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..696463b --- /dev/null +++ b/index.d.ts @@ -0,0 +1,89 @@ +declare module "flexsearch" { + interface Index { + //TODO: Chaining + readonly id: string; + readonly index: string; + readonly length: number; + + add(id: number, o: T); + search(query: string, options: number | SearchOptions, callback: (results: SearchResults) => void): void; + search(query: string, options?: number | SearchOptions): Promise>; + search(options: SearchOptions & {query: string}, callback: (results: SearchResults) => void): void; + search(options: SearchOptions & {query: string}): Promise>; + update(id: number, o: T); + remove(id: number); + clear(); + destroy(); + addMatcher(matcher: Matcher); + where(whereFn: (o: T) => boolean): SearchResult[]; + where(whereObj: {[key: string]: string}); + encode(str: string): string; + export(): string; + import(exported: string); + } + + interface SearchOptions { + limit?: number, + suggest?: boolean, + where?: {[key: string]: string}, + field?: string | string[], + bool?: "and" | "or" | "not" + page?: boolean | Cursor; + //TODO: Sorting + } + + interface SearchResults { + page?: Cursor, + next?: Cursor, + result: SearchResult[] + } + + type SearchResult = number; + + export type CreateOptions = { + profile?: IndexProfile; + tokenize?: DefaultTokenizer | TokenizerFn; + split?: RegExp; + encode?: DefaultEncoder | EncoderFn | false; + cache?: boolean | number; + async?: boolean; + worker?: false | number; + depth?: false | number; + threshold?: false | number; + resolution?: number; + stemmer?: Stemmer | string | false; + filter?: FilterFn | string | false; + rtl?: boolean; + }; + +// limit number Sets the limit of results. +// suggest true, false Enables suggestions in results. +// where object Use a where-clause for non-indexed fields. +// field string, Array Sets the document fields which should be searched. When no field is set, all fields will be searched. Custom options per field are also supported. +// bool "and", "or" Sets the used logical operator when searching through multiple fields. +// page true, false, cursor Enables paginated results. + + type IndexProfile = "memory" | "speed" | "match" | "score" | "balance" | "fast"; + type DefaultTokenizer = "strict" | "forward" | "reverse" | "full"; + type TokenizerFn = (str: string) => string[]; + type DefaultEncoder = "icase" | "simple" | "advanced" | "extra" | "balance"; + type EncoderFn = (str: string) => string; + type Stemmer = {[key: string]: string}; + type Matcher = {[key: string]: string}; + type FilterFn = (str: string) => boolean; + type Cursor = string; + + export default class FlexSearch { + static create(options?: Options): Index; + static registerMatcher(matcher: Matcher); + static registerEncoder(name: string, encoder: EncoderFn); + static registerLanguage(lang: string, options: { stemmer?: Stemmer; filter?: string[] }); + static encode(name: string, str: string); + } +} + +// FlexSearch.create() +// FlexSearch.registerMatcher({KEY: VALUE}) +// FlexSearch.registerEncoder(name, encoder) +// FlexSearch.registerLanguage(lang, {stemmer:{}, filter:[]}) +// FlexSearch.encode(name, string) From be8a9c496d27ab80a8e77275c38264b59a94a0d8 Mon Sep 17 00:00:00 2001 From: Rik Date: Fri, 7 Jun 2019 13:45:07 +0100 Subject: [PATCH 2/2] fix: Include missing CreateOptions --- index.d.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 696463b..0aadfa3 100644 --- a/index.d.ts +++ b/index.d.ts @@ -5,6 +5,8 @@ declare module "flexsearch" { readonly index: string; readonly length: number; + init(); + init(options: CreateOptions); add(id: number, o: T); search(query: string, options: number | SearchOptions, callback: (results: SearchResults) => void): void; search(query: string, options?: number | SearchOptions): Promise>; @@ -74,7 +76,7 @@ declare module "flexsearch" { type Cursor = string; export default class FlexSearch { - static create(options?: Options): Index; + static create(options?: CreateOptions): Index; static registerMatcher(matcher: Matcher); static registerEncoder(name: string, encoder: EncoderFn); static registerLanguage(lang: string, options: { stemmer?: Stemmer; filter?: string[] });