diff --git a/README.md b/README.md index 42e99cc7..fbd5840f 100644 --- a/README.md +++ b/README.md @@ -98,9 +98,23 @@ wrap("Hello world", "he") ### options In all the above functions, you can pass an optional object with the following keys - * `maxResults` - The maximum numbers of results to return. - * `usePathScoring` - * `pathSeparator` +```typescript +{ + /** only for `filter` function */ + maxResults?: number + + /** @default false */ + allowErrors?: boolean + + /** @default true */ + usePathScoring?: boolean + + /** @default false */ + useExtensionBonus?: boolean + + pathSeparator?: '/' | '\\' | string +} +``` ### New() Initializes the native binding diff --git a/package.json b/package.json index d7f7ca34..2ec5fb57 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "1.0.1", "description": "Fuzzaldrin plus - fast using native c bindings", "main": "fuzzaldrin-dist.js", + "types": "./types/fuzzaldrin-plus-fast.d.ts", "scripts": { "native:clean": "shx rm -rf build prebuilds", "native:build": "node-gyp-build", diff --git a/types/fuzzaldrin-plus-fast.d.ts b/types/fuzzaldrin-plus-fast.d.ts new file mode 100644 index 00000000..7e6c05c8 --- /dev/null +++ b/types/fuzzaldrin-plus-fast.d.ts @@ -0,0 +1,61 @@ +export interface IOptions { + + /** @default false */ + allowErrors?: boolean + + /** @default true */ + usePathScoring?: boolean + + /** @default false */ + useExtensionBonus?: boolean + + pathSeparator?: '/' | '\\' | string + + // TODO not implemented? + // optCharRegEx?: RegExp + + // TODO not implemented? + // wrap?: { tagOpen?: string; tagClass?: string; tagClose?: string } + + /** @deprecated: there is no major benefit by precomputing something just for the query. */ + preparedQuery?: {} +} + +export type IFilterOptions = IOptions & { + + // TODO not implemented? + // key?: T extends string ? never : keyof T + + /** The maximum numbers of results to return */ + maxResults?: number + + // TODO not implemented + // maxInners?: number +} + +/** Sort and filter the given candidates by matching them against the given query. +* @param candidates An array of strings or objects. +* @param query A string query to match each candidate against. +* @return returns an array of candidates sorted by best match against the query. + */ +export function filter( + data: T[], + query: string, + options?: IFilterOptions +): T[] + +/** Score the given string against the given query. +* @param str The string the score. +* @param query The query to score the string against. +* @param options options +*/ +export function score(str: string, query: string, options?: IOptions): number + +/** Gives an array of indices at which the query matches the given string */ +export function match(str: string, query: string, options?: IOptions): number[] + +/** Gives an HTML/Markdown string that highlights the range for which the match happens */ +export function wrap(str: string, query: string, options?: IOptions): string + +/** @deprecated: there is no major benefit by precomputing something just for the query. */ +export function prepareQuery(query: string, options?: IOptions): {}