A self hosted chat application with end-to-end encrypted messaging.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.3 KiB

4 years ago
  1. import { SearchService } from "./SearchService";
  2. // import * as Fuse from 'fuse.js';
  3. import Fuse = require('fuse.js');
  4. export class FuseSearchService<T> implements SearchService<T> {
  5. private readonly _fuse: Fuse<T, Object>;
  6. private readonly _params: Fuse.FuseOptions<T>;
  7. constructor(keys: string[], params: Fuse.FuseOptions<T> = {
  8. shouldSort: true,
  9. threshold: 0.01,
  10. location: 0,
  11. distance: 100,
  12. maxPatternLength: 32,
  13. minMatchCharLength: 1,
  14. // keys: [
  15. // "userName",
  16. // ]
  17. }) {
  18. this._params = params;
  19. this._params.keys = keys;
  20. this._fuse = new Fuse([], this._params);
  21. }
  22. search(list: T[], searchTerm: string): T[] {
  23. // const fuse = new Fuse<T, Fuse.FuseOptions<any>>(list, this._params);
  24. this._fuse.setCollection(list);
  25. // fuse.search(searchTerm);
  26. return this._fuse.search(searchTerm);
  27. }
  28. /**
  29. *
  30. *
  31. * @static
  32. * @param keys
  33. * The keys that must be searched for in the given list
  34. * @template G
  35. * @returns {SearchService<G>}
  36. * @memberof FuseSearchService
  37. */
  38. static getInstance<G>(keys: string[]): SearchService<G> {
  39. return new FuseSearchService<G>(keys);
  40. }
  41. }