fuse.d.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // Type definitions for Fuse.js v6.4.3
  2. // TypeScript v3.9.5
  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?: 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?: 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(): 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>,
  83. list: ReadonlyArray<U>,
  84. options?: Fuse.FuseIndexOptions<U>
  85. ): FuseIndex<U>
  86. static parseIndex<U>(
  87. index: any,
  88. options?: Fuse.FuseIndexOptions<U>
  89. ): FuseIndex<U>
  90. }
  91. declare class FuseIndex<T> {
  92. constructor(options?: Fuse.FuseIndexOptions<T>)
  93. setSources(docs: ReadonlyArray<T>): void
  94. setKeys(keys: ReadonlyArray<string>): void
  95. setIndexRecords(records: Fuse.FuseIndexRecords): void
  96. create(): void
  97. add(doc: T): void
  98. toJSON(): {
  99. keys: ReadonlyArray<string>
  100. collection: Fuse.FuseIndexRecords
  101. }
  102. }
  103. declare namespace Fuse {
  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 = {
  212. name: string | string[]
  213. weight: number
  214. }
  215. export type FuseOptionKey = FuseOptionKeyObject | string | string[]
  216. export interface IFuseOptions<T> {
  217. isCaseSensitive?: boolean
  218. distance?: number
  219. findAllMatches?: boolean
  220. getFn?: FuseGetFunction<T>
  221. ignoreLocation?: boolean
  222. ignoreFieldNorm?: boolean
  223. includeMatches?: boolean
  224. includeScore?: boolean
  225. keys?: Array<FuseOptionKey>
  226. location?: number
  227. minMatchCharLength?: number
  228. shouldSort?: boolean
  229. sortFn?: FuseSortFunction
  230. threshold?: number
  231. useExtendedSearch?: boolean
  232. }
  233. // Denotes the start/end indices of a match
  234. // start end
  235. // ↓ ↓
  236. type RangeTuple = [number, number]
  237. export type FuseResultMatch = {
  238. indices: ReadonlyArray<RangeTuple>
  239. key?: string
  240. refIndex?: number
  241. value?: string
  242. }
  243. export type FuseSearchOptions = {
  244. limit: number
  245. }
  246. export type FuseResult<T> = {
  247. item: T
  248. refIndex: number
  249. score?: number
  250. matches?: ReadonlyArray<FuseResultMatch>
  251. }
  252. export type Expression =
  253. | { [key: string]: string }
  254. | {
  255. $path: ReadonlyArray<string>
  256. $val: string
  257. }
  258. | { $and?: Expression[] }
  259. | { $or?: Expression[] }
  260. }