1
0
mirror of https://github.com/nextapps-de/flexsearch.git synced 2025-09-25 12:58:59 +02:00

Add return type annotations in index.d.ts

This commit is contained in:
hasparus
2020-09-14 22:33:44 +02:00
parent f5eb0b0573
commit 5dfb3de415
2 changed files with 83 additions and 50 deletions

107
index.d.ts vendored
View File

@@ -1,55 +1,80 @@
declare module "flexsearch" { declare module "flexsearch" {
interface Index<T> { export interface Index<T> {
//TODO: Chaining
readonly id: string; readonly id: string;
readonly index: string; readonly index: string;
readonly length: number; readonly length: number;
init(); init(options?: CreateOptions): this;
init(options: CreateOptions); info(): {
info(); id: any;
add(o: T); items: any;
add(id: number, o: string); cache: any;
matcher: number;
worker: any;
threshold: any;
depth: any;
resolution: any;
contextual: boolean;
};
add(o: T): this;
add(id: number, o: string): this;
// Result without pagination -> T[] // Result without pagination -> T[]
search(query: string, options: number | SearchOptions, callback: (results: T[]) => void): void; search(
query: string,
options: number | SearchOptions,
callback: (results: T[]) => void
): void;
search(query: string, options?: number | SearchOptions): Promise<T[]>; search(query: string, options?: number | SearchOptions): Promise<T[]>;
search(options: SearchOptions & {query: string}, callback: (results: T[]) => void): void; search(
options: SearchOptions & { query: string },
callback: (results: T[]) => void
): void;
search(options: SearchOptions & { query: string }): Promise<T[]>; search(options: SearchOptions & { query: string }): Promise<T[]>;
// Result with pagination -> SearchResults<T> // Result with pagination -> SearchResults<T>
search(query: string, options: number | SearchOptions & { page?: boolean | Cursor}, callback: (results: SearchResults<T>) => void): void; search(
search(query: string, options?: number | SearchOptions & { page?: boolean | Cursor}): Promise<SearchResults<T>>; query: string,
search(options: SearchOptions & {query: string, page?: boolean | Cursor}, callback: (results: SearchResults<T>) => void): void; options: number | (SearchOptions & { page?: boolean | Cursor }),
search(options: SearchOptions & {query: string, page?: boolean | Cursor}): Promise<SearchResults<T>>; callback: (results: SearchResults<T>) => void
): void;
search(
query: string,
options?: number | (SearchOptions & { page?: boolean | Cursor })
): Promise<SearchResults<T>>;
search(
options: SearchOptions & { query: string; page?: boolean | Cursor },
callback: (results: SearchResults<T>) => void
): void;
search(
options: SearchOptions & { query: string; page?: boolean | Cursor }
): Promise<SearchResults<T>>;
update(id: number, o: T): this;
remove(id: number): this;
clear(): this;
destroy(): this;
addMatcher(matcher: Matcher): this;
update(id: number, o: T); where(whereObj: { [key: string]: string } | ((o: T) => boolean)): T[];
remove(id: number);
clear();
destroy();
addMatcher(matcher: Matcher);
where(whereFn: (o: T) => boolean): T[];
where(whereObj: {[key: string]: string});
encode(str: string): string; encode(str: string): string;
export(): string; export(): string;
import(exported: string); import(exported: string): this;
} }
interface SearchOptions { interface SearchOptions {
limit?: number, limit?: number;
suggest?: boolean, suggest?: boolean;
where?: {[key: string]: string}, where?: { [key: string]: string };
field?: string | string[], field?: string | string[];
bool?: "and" | "or" | "not" bool?: "and" | "or" | "not";
//TODO: Sorting //TODO: Sorting
} }
interface SearchResults<T> { interface SearchResults<T> {
page?: Cursor, page?: Cursor;
next?: Cursor, next?: Cursor;
result: T[] result: T[];
} }
interface Document { interface Document {
@@ -57,7 +82,6 @@ declare module "flexsearch" {
field: any; field: any;
} }
export type CreateOptions = { export type CreateOptions = {
profile?: IndexProfile; profile?: IndexProfile;
tokenize?: DefaultTokenizer | TokenizerFn; tokenize?: DefaultTokenizer | TokenizerFn;
@@ -70,7 +94,7 @@ declare module "flexsearch" {
threshold?: false | number; threshold?: false | number;
resolution?: number; resolution?: number;
stemmer?: Stemmer | string | false; stemmer?: Stemmer | string | false;
filter?: FilterFn | string | false; filter?: FilterFn | string | false;
rtl?: boolean; rtl?: boolean;
doc?: Document; doc?: Document;
}; };
@@ -82,7 +106,13 @@ declare module "flexsearch" {
// bool "and", "or" Sets the used logical operator when searching through multiple fields. // bool "and", "or" Sets the used logical operator when searching through multiple fields.
// page true, false, cursor Enables paginated results. // page true, false, cursor Enables paginated results.
type IndexProfile = "memory" | "speed" | "match" | "score" | "balance" | "fast"; type IndexProfile =
| "memory"
| "speed"
| "match"
| "score"
| "balance"
| "fast";
type DefaultTokenizer = "strict" | "forward" | "reverse" | "full"; type DefaultTokenizer = "strict" | "forward" | "reverse" | "full";
type TokenizerFn = (str: string) => string[]; type TokenizerFn = (str: string) => string[];
type DefaultEncoder = "icase" | "simple" | "advanced" | "extra" | "balance"; type DefaultEncoder = "icase" | "simple" | "advanced" | "extra" | "balance";
@@ -94,10 +124,13 @@ declare module "flexsearch" {
export default class FlexSearch { export default class FlexSearch {
static create<T>(options?: CreateOptions): Index<T>; static create<T>(options?: CreateOptions): Index<T>;
static registerMatcher(matcher: Matcher); static registerMatcher(matcher: Matcher): typeof FlexSearch;
static registerEncoder(name: string, encoder: EncoderFn); static registerEncoder(name: string, encoder: EncoderFn): typeof FlexSearch;
static registerLanguage(lang: string, options: { stemmer?: Stemmer; filter?: string[] }); static registerLanguage(
static encode(name: string, str: string); lang: string,
options: { stemmer?: Stemmer; filter?: string[] }
): typeof FlexSearch;
static encode(name: string, str: string): string;
} }
} }

View File

@@ -19,6 +19,7 @@
}, },
"main": "dist/flexsearch.node.js", "main": "dist/flexsearch.node.js",
"browser": "dist/flexsearch.min.js", "browser": "dist/flexsearch.min.js",
"types": "index.d.ts",
"preferGlobal": false, "preferGlobal": false,
"bin": {}, "bin": {},
"repository": { "repository": {
@@ -75,6 +76,5 @@
"mocha-phantomjs": "^4.1.0", "mocha-phantomjs": "^4.1.0",
"nyc": "^14.1.1", "nyc": "^14.1.1",
"phantomjs-prebuilt": "^2.1.16" "phantomjs-prebuilt": "^2.1.16"
}, }
"types": "index.d.ts"
} }