Skip to content

Commit 4acabb6

Browse files
committed
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
1 parent 6afcfaf commit 4acabb6

1 file changed

Lines changed: 27 additions & 13 deletions

File tree

src/index.d.ts

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ declare class Fuse<T> {
8686
* @returns An indexed list
8787
*/
8888
static createIndex<U>(
89-
keys: Array<Fuse.FuseOptionKey>,
89+
keys: Array<Fuse.FuseOptionKey<U>>,
9090
list: ReadonlyArray<U>,
9191
options?: Fuse.FuseIndexOptions<U>
9292
): Fuse.FuseIndex<U>
@@ -178,8 +178,10 @@ declare namespace Fuse {
178178
// 'n': 0.5773502691896258
179179
// }
180180
type RecordEntryObject = {
181-
v: string // The text value
182-
n: number // The field-length norm
181+
/** The text value */
182+
v: string
183+
/** The field-length norm */
184+
n: number
183185
}
184186

185187
// 'author.tags.name': [{
@@ -205,7 +207,8 @@ declare namespace Fuse {
205207
// }
206208
// }
207209
type FuseIndexObjectRecord = {
208-
i: number // The index of the record in the source list
210+
/** The index of the record in the source list */
211+
i: number
209212
$: RecordEntry
210213
}
211214

@@ -218,25 +221,36 @@ declare namespace Fuse {
218221
// ]
219222
// }
220223
type FuseIndexStringRecord = {
221-
i: number // The index of the record in the source list
222-
v: string // The text value
223-
n: number // The field-length norm
224+
/** The index of the record in the source list */
225+
i: number
226+
/** The text value */
227+
v: string
228+
/** The field-length norm */
229+
n: number
224230
}
225231

226232
type FuseIndexRecords =
227233
| ReadonlyArray<FuseIndexObjectRecord>
228234
| ReadonlyArray<FuseIndexStringRecord>
235+
236+
type FuseOptionKeyObjectGetFunction<T> = (
237+
obj: T,
238+
) => ReadonlyArray<string> | string
229239

230240
// {
231241
// name: 'title',
232-
// weight: 0.7
242+
// weight: 0.7,
243+
// getFn: (book) => book.title
233244
// }
234-
export type FuseOptionKeyObject = {
235-
name: string | string[]
236-
weight: number
245+
export type FuseOptionKeyObject<T> = {
246+
name: string | string[]
247+
/** 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. */
248+
weight?: number
249+
/** The function to use to retrieve an object's value */
250+
getFn?: FuseOptionKeyObjectGetFunction<T>
237251
}
238252

239-
export type FuseOptionKey = FuseOptionKeyObject | string | string[]
253+
export type FuseOptionKey<T> = FuseOptionKeyObject<T> | string | string[]
240254

241255
export interface IFuseOptions<T> {
242256
/** Indicates whether comparisons should be case sensitive. */
@@ -258,7 +272,7 @@ declare namespace Fuse {
258272
/** 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. */
259273
includeScore?: boolean
260274
/** List of keys that will be searched. This supports nested paths, weighted search, searching in arrays of `strings` and `objects`. */
261-
keys?: Array<FuseOptionKey>
275+
keys?: Array<FuseOptionKey<T>>
262276
/** Determines approximately where in the text is the pattern expected to be found. */
263277
location?: number
264278
/** 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`). */

0 commit comments

Comments
 (0)