From b8279ebddcc4d5659ec61b9de98877b5908466f7 Mon Sep 17 00:00:00 2001 From: Thomas Wilkerling Date: Sun, 7 Jan 2024 23:46:06 +0100 Subject: [PATCH] add index.d.ts from @types/flexsearch --- index.d.ts | 428 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + 2 files changed, 429 insertions(+) create mode 100644 index.d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..9b321db --- /dev/null +++ b/index.d.ts @@ -0,0 +1,428 @@ +declare module "flexsearch" { + // Type definitions for flexsearch 0.7 + // Project: https://github.com/nextapps-de/flexsearch/ + // Definitions by: LOSSES Don + // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + + /************************************/ + /* Utils */ + /************************************/ + export type Id = number | string; + export type Limit = number; + export type ExportHandler = (id: string | number, value: T) => void; + export type AsyncCallback = T extends undefined + ? () => void + : (result: T) => void; + export type UnknownFunction = (...x: unknown[]) => unknown; + + export type StoreOption = boolean | string | string[]; + export type EnrichStoreOption = true | string | string[]; + + /************************************/ + /* Common Options */ + /************************************/ + + /** + * **Document:** + * * Presets: https://github.com/nextapps-de/flexsearch#presets + */ + export type Preset = "memory" | "performance" | "match" | "score" | "default"; + + /** + * **Document:** + * * Tokenizer: https://github.com/nextapps-de/flexsearch#tokenizer-prefix-search + * * Add custom tokenizer: https://github.com/nextapps-de/flexsearch#add-custom-tokenizer + */ + export type Tokenizer = + | "strict" + | "forward" + | "reverse" + | "full" + | ((x: string) => string[]); + + /** + * **Document:** + * * Encoders: https://github.com/nextapps-de/flexsearch#encoders + */ + export type Encoders = + | false + | "default" + | "simple" + | "balance" + | "advanced" + | "extra" + | ((x: string) => string[]); + + /** + * **Document:** + * * Contextual search: https://github.com/nextapps-de/flexsearch#contextual + */ + export interface ContextOptions { + resolution: number; + depth: false | number; + bidirectional: boolean; + } + + /** + * **Document:** + * * Search options: https://github.com/nextapps-de/flexsearch#search-options + */ + export interface SearchOptions { + query?: string; + limit?: number; + offset?: number; + suggest?: boolean; + } + + /** + * **Document:** + * * The document descriptor: https://github.com/nextapps-de/flexsearch#the-document-descriptor + */ + export interface Descriptor { + id: string | number; + field: string[] | Array>; + } + + /** + * **Document:** + * * Context Options: https://github.com/nextapps-de/flexsearch#context-options + */ + export interface ContextOptions { + resolution: number; + depth: false | number; + bidirectional: boolean; + } + + /** + * **Document:** + * * Charset options: https://github.com/nextapps-de/flexsearch#charset-options + */ + export interface CharsetOptions { + split: false | string | RegExp; + rtl: boolean; + encode: (x: string) => string[]; + } + + export interface Stemmer { + [key: string]: string; + } + + export interface Matcher { + [key: string]: string; + } + + export type FilterFunction = (x: string) => boolean; + export type FilterArray = string[]; + + /** + * **Document:** + * * Language Options: https://github.com/nextapps-de/flexsearch#language-options + * * Language: https://github.com/nextapps-de/flexsearch#languages + */ + export interface LanguageOptions { + stemmer: false | string | Stemmer | UnknownFunction; + filter: false | string | FilterArray | FilterFunction; + matcher: false | string | Matcher | UnknownFunction; + } + + /** + * These options will determine how the documents be indexed. + * + * **Generic type parameters:** + * + * @template T The type of the document. + * @template Store If store is enabled. + * + * **Document:** + * * Index options: https://github.com/nextapps-de/flexsearch#index-options + * * Language: https://github.com/nextapps-de/flexsearch#languages + */ + export interface IndexOptions { + preset?: Preset; + tokenize?: Tokenizer; + cache?: boolean | number; + resolution?: number; + context?: boolean | IndexOptions | ContextOptions; + optimize?: boolean; + boost?: (words: string[], term: string, index: number) => number; + + // Language-specific Options and Encoding + charset?: CharsetOptions | string; + language?: LanguageOptions | string; + encode?: Encoders; + stemmer?: LanguageOptions["stemmer"]; + filter?: LanguageOptions["filter"]; + matcher?: LanguageOptions["matcher"]; + } + + /************************************/ + /* Index Search */ + /************************************/ + + export type IndexSearchResult = Id[]; + + /** + * **Document:** + * * Basic usage and variants: https://github.com/nextapps-de/flexsearch#basic-usage-and-variants + * * API overview: https://github.com/nextapps-de/flexsearch#api-overview + * * Usage: https://github.com/nextapps-de/flexsearch#usage + */ + + export class Index { + constructor(x?: Preset | IndexOptions); + add(id: Id, item: string): this; + append(id: Id, item: string): this; + update(id: Id, item: string): this; + remove(target: Id): this; + search(query: string, options?: Limit | SearchOptions): IndexSearchResult; + search( + query: string, + limit: number, + options: SearchOptions + ): IndexSearchResult; + search(options: SearchOptions): IndexSearchResult; + + // https://github.com/nextapps-de/flexsearch#check-existence-of-already-indexed-ids + contain(id: Id): boolean; + + export(handler: ExportHandler): Promise; + import(id: Id, item: string): Promise; + + // Async Methods + addAsync( + id: Id, + item: string, + callback?: AsyncCallback + ): Promise; + appendAsync( + id: Id, + item: string, + callback?: AsyncCallback + ): Promise; + updateAsync( + id: Id, + item: string, + callback?: AsyncCallback + ): Promise; + removeAsync(target: Id, callback?: AsyncCallback): Promise; + searchAsync( + query: string, + options?: Limit | SearchOptions, + callback?: AsyncCallback + ): Promise; + searchAsync( + query: string, + limit: number, + options?: Limit | SearchOptions + ): IndexSearchResult; + searchAsync(options: SearchOptions): Promise; + } + + /** + * **Document:** + * * Basic usage and variants: https://github.com/nextapps-de/flexsearch#basic-usage-and-variants + * * API overview: https://github.com/nextapps-de/flexsearch#api-overview + * * Worker index: https://github.com/nextapps-de/flexsearch#worker-index + */ + export class Worker { + constructor(x?: Preset | IndexOptions); + + add(id: Id, item: string, callback?: AsyncCallback): Promise; + append(id: Id, item: string, callback?: AsyncCallback): Promise; + update(id: Id, item: string, callback?: AsyncCallback): Promise; + remove(target: Id, callback?: AsyncCallback): Promise; + search( + query: string, + options?: Limit | SearchOptions, + callback?: AsyncCallback + ): Promise; + search( + query: string, + limit: number, + options?: Limit | SearchOptions + ): IndexSearchResult; + search(options: SearchOptions): Promise; + + // Async Methods + addAsync( + id: Id, + item: string, + callback?: AsyncCallback + ): Promise; + appendAsync( + id: Id, + item: string, + callback?: AsyncCallback + ): Promise; + updateAsync( + id: Id, + item: string, + callback?: AsyncCallback + ): Promise; + removeAsync(target: Id, callback?: AsyncCallback): Promise; + searchAsync( + query: string, + options?: Limit | SearchOptions, + callback?: AsyncCallback + ): Promise; + searchAsync( + query: string, + limit: number, + options?: Limit | SearchOptions + ): IndexSearchResult; + searchAsync(options: SearchOptions): Promise; + } + + /************************************/ + /* Document Search */ + /************************************/ + + /* + * **Document:** + * * Document options: https://github.com/nextapps-de/flexsearch#document-options + */ + export interface DocumentOptions { + id: string; + tag?: false | string; + index: + | string + | string[] + | Array & { field: string }>; + store?: Store; + } + + /* + * **Document:** + * * Index options: https://github.com/nextapps-de/flexsearch#index-options + */ + export interface IndexOptionsForDocumentSearch< + T, + Store extends StoreOption = false + > extends IndexOptions { + // Additional Options for Document Indexes + worker?: boolean; + document?: DocumentOptions | Descriptor; + } + + export interface SimpleDocumentSearchResultSetUnit { + field: string; + result: Id[]; + } + + export interface EnrichedDocumentSearchResultSetUnitResultUnit { + id: Id[]; + doc: T; + } + + export interface EnrichedDocumentSearchResultSetUnit { + field: string; + result: Array>; + } + + /** + * # Document Search Result + * + * To make your result return the full document: + * * set `store` to `true` while creating the document; + * * set `enrich` to `true` while searching. + * + * If neither of these conditions is met, then the returned result will be a `ISimpleDocumentSearchResult`. + */ + export type DocumentSearchResult< + T, + Store extends StoreOption = false, + Enrich extends boolean = false + > = [Store, Enrich] extends [EnrichStoreOption, true] + ? Array> + : SimpleDocumentSearchResultSetUnit[]; + + /** + * **Document:** + * * Document search options: https://github.com/nextapps-de/flexsearch#document-search-options + */ + export interface DocumentSearchOptions extends SearchOptions { + index?: string | string[] | SearchOptions[]; + tag?: string | string[]; + enrich?: T; + bool?: "and" | "or"; + } + + /** + * **Document:** + * * Basic usage and variants: https://github.com/nextapps-de/flexsearch#basic-usage-and-variants + * * API overview: https://github.com/nextapps-de/flexsearch#api-overview + * * Document store: https://github.com/nextapps-de/flexsearch#document-store + */ + export class Document { + constructor( + options: IndexOptionsForDocumentSearch, + typeHack?: T + ); + add(document: T): this; + add(id: Id, document: T): this; + append(document: T): this; + append(id: Id, document: T): this; + update(document: T): this; + update(id: Id, document: T): this; + remove(target: Id | T): this; + search(query: string, limit?: number): SimpleDocumentSearchResultSetUnit[]; + + // https://github.com/nextapps-de/flexsearch#field-search + search( + query: string, + options: string[] | Partial> + ): SimpleDocumentSearchResultSetUnit[]; + + search( + query: string, + limit?: number, + options?: Partial> + ): DocumentSearchResult; + search( + options: Partial> + ): SimpleDocumentSearchResultSetUnit[]; + export(handler: ExportHandler): Promise; + import(id: Id, document: T): Promise; + + // Async Methods + addAsync(id: Id, document: T, callback?: AsyncCallback): Promise; + appendAsync(id: Id, document: T, callback?: AsyncCallback): Promise; + updateAsync(id: Id, document: T, callback?: AsyncCallback): Promise; + removeAsync(target: Id | T, callback?: AsyncCallback): Promise; + searchAsync( + query: string, + options: string[] | Partial> + ): Promise>; + searchAsync( + query: string, + limit?: number + ): Promise; + searchAsync( + query: string, + limit: number, + callback: AsyncCallback + ): Promise; + searchAsync( + query: string, + options: Partial>, + callback: AsyncCallback> + ): Promise; + searchAsync( + options: Partial> + ): Promise>; + searchAsync( + options: Partial>, + callback: AsyncCallback> + ): Promise; + } + + /************************************/ + /* Miscellaneous */ + /************************************/ + export function create(options: IndexOptions): Index; + export function registerCharset(name: string, charset: CharsetOptions): void; + export function registerLanguage( + name: string, + language: LanguageOptions + ): void; +} \ No newline at end of file diff --git a/package.json b/package.json index 850050b..6f804c1 100644 --- a/package.json +++ b/package.json @@ -61,6 +61,7 @@ "dist/**", "src/**", "task/**", + "index.d.ts", "README.md", "CHANGELOG.md", "LICENSE"