From 4acabb686f9a1a95adc766bc2fb387c0ce0b477c Mon Sep 17 00:00:00 2001 From: Ben Jenkinson <59176+BenJenkinson@users.noreply.github.com> Date: Thu, 5 May 2022 14:01:09 +0100 Subject: [PATCH] fix(typescript): type definition for `FuseOptionKeyObject, fixes #655 - Make `FuseOptionKeyObject.weight` property optional (and add description taken from docs) - Add `FuseOptionKeyObject.getFn` property (and add description taken from docs) - Add `FuseOptionKeyObjectGetFunction` type to match existing `FuseGetFunction` type - Make `FuseOptionKeyObject` a generic, to allow typing the `getFn` method argument - Make `FuseOptionKey` a generic, to allow making `FuseOptionKeyObject` a generic - Convert some existing inline comments `// foo` to jsdoc comments `/** foo */` so they will be included in intellisense --- src/index.d.ts | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/src/index.d.ts b/src/index.d.ts index 5eed32fdd..9707b56a8 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -86,7 +86,7 @@ declare class Fuse { * @returns An indexed list */ static createIndex( - keys: Array, + keys: Array>, list: ReadonlyArray, options?: Fuse.FuseIndexOptions ): Fuse.FuseIndex @@ -178,8 +178,10 @@ declare namespace Fuse { // 'n': 0.5773502691896258 // } type RecordEntryObject = { - v: string // The text value - n: number // The field-length norm + /** The text value */ + v: string + /** The field-length norm */ + n: number } // 'author.tags.name': [{ @@ -205,7 +207,8 @@ declare namespace Fuse { // } // } type FuseIndexObjectRecord = { - i: number // The index of the record in the source list + /** The index of the record in the source list */ + i: number $: RecordEntry } @@ -218,25 +221,36 @@ declare namespace Fuse { // ] // } type FuseIndexStringRecord = { - i: number // The index of the record in the source list - v: string // The text value - n: number // The field-length norm + /** The index of the record in the source list */ + i: number + /** The text value */ + v: string + /** The field-length norm */ + n: number } type FuseIndexRecords = | ReadonlyArray | ReadonlyArray + + type FuseOptionKeyObjectGetFunction = ( + obj: T, + ) => ReadonlyArray | string // { // name: 'title', - // weight: 0.7 + // weight: 0.7, + // getFn: (book) => book.title // } - export type FuseOptionKeyObject = { - name: string | string[] - weight: number + export type FuseOptionKeyObject = { + name: string | string[] + /** Adjust the weight of each key to give them higher (or lower) values in search results. The `weight` value must be greater than zero. If undefined, it will default to `1`. Internally, Fuse will normalize weights to be within `0` and `1` exclusive. */ + weight?: number + /** The function to use to retrieve an object's value */ + getFn?: FuseOptionKeyObjectGetFunction } - export type FuseOptionKey = FuseOptionKeyObject | string | string[] + export type FuseOptionKey = FuseOptionKeyObject | string | string[] export interface IFuseOptions { /** Indicates whether comparisons should be case sensitive. */ @@ -258,7 +272,7 @@ declare namespace Fuse { /** Whether the score should be included in the result set. A score of `0`indicates a perfect match, while a score of `1` indicates a complete mismatch. */ includeScore?: boolean /** List of keys that will be searched. This supports nested paths, weighted search, searching in arrays of `strings` and `objects`. */ - keys?: Array + keys?: Array> /** Determines approximately where in the text is the pattern expected to be found. */ location?: number /** Only the matches whose length exceeds this value will be returned. (For instance, if you want to ignore single character matches in the result, set it to `2`). */