fuse.d.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // Type definitions for Fuse.js v6.6.2
  2. // TypeScript v4.5.4
  3. export default Fuse
  4. export as namespace Fuse
  5. declare class Fuse<T> {
  6. constructor(
  7. list: ReadonlyArray<T>,
  8. options?: Fuse.IFuseOptions<T>,
  9. index?: Fuse.FuseIndex<T>
  10. )
  11. /**
  12. * Search function for the Fuse instance.
  13. *
  14. * ```typescript
  15. * const list: MyType[] = [myType1, myType2, etc...]
  16. * const options: Fuse.IFuseOptions<MyType> = {
  17. * keys: ['key1', 'key2']
  18. * }
  19. *
  20. * const myFuse = new Fuse(list, options)
  21. * let result = myFuse.search('pattern')
  22. * ```
  23. *
  24. * @param pattern The pattern to search
  25. * @param options `Fuse.FuseSearchOptions`
  26. * @returns An array of search results
  27. */
  28. search<R = T>(
  29. pattern: string | Fuse.Expression,
  30. options?: Fuse.FuseSearchOptions
  31. ): Fuse.FuseResult<R>[]
  32. setCollection(docs: ReadonlyArray<T>, index?: Fuse.FuseIndex<T>): void
  33. /**
  34. * Adds a doc to the end the list.
  35. */
  36. add(doc: T): void
  37. /**
  38. * Removes all documents from the list which the predicate returns truthy for,
  39. * and returns an array of the removed docs.
  40. * The predicate is invoked with two arguments: (doc, index).
  41. */
  42. remove(predicate: (doc: T, idx: number) => boolean): T[]
  43. /**
  44. * Removes the doc at the specified index.
  45. */
  46. removeAt(idx: number): void
  47. /**
  48. * Returns the generated Fuse index
  49. */
  50. getIndex(): Fuse.FuseIndex<T>
  51. /**
  52. * Return the current version.
  53. */
  54. static version: string
  55. /**
  56. * Use this method to pre-generate the index from the list, and pass it
  57. * directly into the Fuse instance.
  58. *
  59. * _Note that Fuse will automatically index the table if one isn't provided
  60. * during instantiation._
  61. *
  62. * ```typescript
  63. * const list: MyType[] = [myType1, myType2, etc...]
  64. *
  65. * const index = Fuse.createIndex<MyType>(
  66. * keys: ['key1', 'key2']
  67. * list: list
  68. * )
  69. *
  70. * const options: Fuse.IFuseOptions<MyType> = {
  71. * keys: ['key1', 'key2']
  72. * }
  73. *
  74. * const myFuse = new Fuse(list, options, index)
  75. * ```
  76. * @param keys The keys to index
  77. * @param list The list from which to create an index
  78. * @param options?
  79. * @returns An indexed list
  80. */
  81. static createIndex<U>(
  82. keys: Array<Fuse.FuseOptionKey<U>>,
  83. list: ReadonlyArray<U>,
  84. options?: Fuse.FuseIndexOptions<U>
  85. ): Fuse.FuseIndex<U>
  86. static parseIndex<U>(
  87. index: any,
  88. options?: Fuse.FuseIndexOptions<U>
  89. ): Fuse.FuseIndex<U>
  90. }
  91. declare namespace Fuse {
  92. export class FuseIndex<T> {
  93. constructor(options?: FuseIndexOptions<T>)
  94. setSources(docs: ReadonlyArray<T>): void
  95. setKeys(keys: ReadonlyArray<string>): void
  96. setIndexRecords(records: FuseIndexRecords): void
  97. create(): void
  98. add(doc: T): void
  99. toJSON(): {
  100. keys: ReadonlyArray<string>
  101. records: FuseIndexRecords
  102. }
  103. }
  104. type FuseGetFunction<T> = (
  105. obj: T,
  106. path: string | string[]
  107. ) => ReadonlyArray<string> | string
  108. export type FuseIndexOptions<T> = {
  109. getFn: FuseGetFunction<T>
  110. }
  111. // {
  112. // title: { '$': "Old Man's War" },
  113. // 'author.firstName': { '$': 'Codenar' }
  114. // }
  115. //
  116. // OR
  117. //
  118. // {
  119. // tags: [
  120. // { $: 'nonfiction', idx: 0 },
  121. // { $: 'web development', idx: 1 },
  122. // ]
  123. // }
  124. export type FuseSortFunctionItem = {
  125. [key: string]: { $: string } | { $: string; idx: number }[]
  126. }
  127. // {
  128. // score: 0.001,
  129. // key: 'author.firstName',
  130. // value: 'Codenar',
  131. // indices: [ [ 0, 3 ] ]
  132. // }
  133. export type FuseSortFunctionMatch = {
  134. score: number
  135. key: string
  136. value: string
  137. indices: ReadonlyArray<number>[]
  138. }
  139. // {
  140. // score: 0,
  141. // key: 'tags',
  142. // value: 'nonfiction',
  143. // idx: 1,
  144. // indices: [ [ 0, 9 ] ]
  145. // }
  146. export type FuseSortFunctionMatchList = FuseSortFunctionMatch & {
  147. idx: number
  148. }
  149. export type FuseSortFunctionArg = {
  150. idx: number
  151. item: FuseSortFunctionItem
  152. score: number
  153. matches?: (FuseSortFunctionMatch | FuseSortFunctionMatchList)[]
  154. }
  155. export type FuseSortFunction = (
  156. a: FuseSortFunctionArg,
  157. b: FuseSortFunctionArg
  158. ) => number
  159. // title: {
  160. // '$': "Old Man's War",
  161. // 'n': 0.5773502691896258
  162. // }
  163. type RecordEntryObject = {
  164. v: string // The text value
  165. n: number // The field-length norm
  166. }
  167. // 'author.tags.name': [{
  168. // 'v': 'pizza lover',
  169. // 'i': 2,
  170. // 'n: 0.7071067811865475
  171. // }
  172. type RecordEntryArrayItem = ReadonlyArray<RecordEntryObject & { i: number }>
  173. // TODO: this makes it difficult to infer the type. Need to think more about this
  174. type RecordEntry = { [key: string]: RecordEntryObject | RecordEntryArrayItem }
  175. // {
  176. // i: 0,
  177. // '$': {
  178. // '0': { v: "Old Man's War", n: 0.5773502691896258 },
  179. // '1': { v: 'Codenar', n: 1 },
  180. // '2': [
  181. // { v: 'pizza lover', i: 2, n: 0.7071067811865475 },
  182. // { v: 'helo wold', i: 1, n: 0.7071067811865475 },
  183. // { v: 'hello world', i: 0, n: 0.7071067811865475 }
  184. // ]
  185. // }
  186. // }
  187. type FuseIndexObjectRecord = {
  188. i: number // The index of the record in the source list
  189. $: RecordEntry
  190. }
  191. // {
  192. // keys: null,
  193. // list: [
  194. // { v: 'one', i: 0, n: 1 },
  195. // { v: 'two', i: 1, n: 1 },
  196. // { v: 'three', i: 2, n: 1 }
  197. // ]
  198. // }
  199. type FuseIndexStringRecord = {
  200. i: number // The index of the record in the source list
  201. v: string // The text value
  202. n: number // The field-length norm
  203. }
  204. type FuseIndexRecords =
  205. | ReadonlyArray<FuseIndexObjectRecord>
  206. | ReadonlyArray<FuseIndexStringRecord>
  207. // {
  208. // name: 'title',
  209. // weight: 0.7
  210. // }
  211. export type FuseOptionKeyObject<T> = {
  212. name: string | string[]
  213. weight?: number
  214. getFn?: (obj: T) => ReadonlyArray<string> | string
  215. }
  216. export type FuseOptionKey<T> = FuseOptionKeyObject<T> | string | string[]
  217. export interface IFuseOptions<T> {
  218. /** Indicates whether comparisons should be case sensitive. */
  219. isCaseSensitive?: boolean
  220. /** Determines how close the match must be to the fuzzy location (specified by `location`). An exact letter match which is `distance` characters away from the fuzzy location would score as a complete mismatch. A `distance` of `0` requires the match be at the exact `location` specified. A distance of `1000` would require a perfect match to be within `800` characters of the `location` to be found using a `threshold` of `0.8`. */
  221. distance?: number
  222. /** When true, the matching function will continue to the end of a search pattern even if a perfect match has already been located in the string. */
  223. findAllMatches?: boolean
  224. /** The function to use to retrieve an object's value at the provided path. The default will also search nested paths. */
  225. getFn?: FuseGetFunction<T>
  226. /** When `true`, search will ignore `location` and `distance`, so it won't matter where in the string the pattern appears. */
  227. ignoreLocation?: boolean
  228. /** When `true`, the calculation for the relevance score (used for sorting) will ignore the `field-length norm`. */
  229. ignoreFieldNorm?: boolean
  230. /** Determines how much the `field-length norm` affects scoring. A value of `0` is equivalent to ignoring the field-length norm. A value of `0.5` will greatly reduce the effect of field-length norm, while a value of `2.0` will greatly increase it. */
  231. fieldNormWeight?: number
  232. /** Whether the matches should be included in the result set. When `true`, each record in the result set will include the indices of the matched characters. These can consequently be used for highlighting purposes. */
  233. includeMatches?: boolean
  234. /** 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. */
  235. includeScore?: boolean
  236. /** List of keys that will be searched. This supports nested paths, weighted search, searching in arrays of `strings` and `objects`. */
  237. keys?: Array<FuseOptionKey<T>>
  238. /** Determines approximately where in the text is the pattern expected to be found. */
  239. location?: number
  240. /** 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`). */
  241. minMatchCharLength?: number
  242. /** Whether to sort the result list, by score. */
  243. shouldSort?: boolean
  244. /** The function to use to sort all the results. The default will sort by ascending relevance score, ascending index. */
  245. sortFn?: FuseSortFunction
  246. /** At what point does the match algorithm give up. A threshold of `0.0` requires a perfect match (of both letters and location), a threshold of `1.0` would match anything. */
  247. threshold?: number
  248. /** When `true`, it enables the use of unix-like search commands. See [example](/examples.html#extended-search). */
  249. useExtendedSearch?: boolean
  250. }
  251. // Denotes the start/end indices of a match
  252. // start end
  253. // ↓ ↓
  254. type RangeTuple = [number, number]
  255. export type FuseResultMatch = {
  256. indices: ReadonlyArray<RangeTuple>
  257. key?: string
  258. refIndex?: number
  259. value?: string
  260. }
  261. export type FuseSearchOptions = {
  262. limit: number
  263. }
  264. export type FuseResult<T> = {
  265. item: T
  266. refIndex: number
  267. score?: number
  268. matches?: ReadonlyArray<FuseResultMatch>
  269. }
  270. export type Expression =
  271. | { [key: string]: string }
  272. | {
  273. $path: ReadonlyArray<string>
  274. $val: string
  275. }
  276. | { $and?: Expression[] }
  277. | { $or?: Expression[] }
  278. export const config: Required<IFuseOptions<any>>
  279. }