All files / src/utils jumpinChoiceSearch.ts

90.9% Statements 30/33
80.95% Branches 17/21
100% Functions 7/7
90.62% Lines 29/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91                          1x 1x 1x 1x         1x       1x             1x 1x                     3x 3x 1x   2x 2x 2x 2x                   2x     2x 2x 2x 2x         1x 1x 1x 1x   1x           2x     2x 2x 2x    
/**
 * MucSelect searches through Choices, which keeps the first four hits in fuse
 * rank order. An exact id is often past that cutoff, so typing "1" never
 * shows service 1. Ranking that id first happens before the cutoff.
 */
import { promoteExactIdResults } from "@/utils/jumpinCatalog";
 
type RankedHit = { item: { label?: string }; rank: number };
 
type ChoiceSearcher = {
  search: (query: string) => RankedHit[];
};
 
const scheduled = new WeakSet<object>();
const patchedSearchers = new WeakSet<object>();
let installed = false;
let originalBind: typeof Function.prototype.bind | null = null;
 
function isChoicesInstance(
  value: unknown
): value is { _searcher?: ChoiceSearcher } {
  Iif (!value || typeof value !== "object") {
    return false;
  }
  const config = (
    value as {
      config?: {
        searchResultLimit?: unknown;
        classNames?: { inputCloned?: unknown };
      };
    }
  ).config;
  const inputCloned = config?.classNames?.inputCloned;
  return (
    config?.searchResultLimit === 4 &&
    Array.isArray(inputCloned) &&
    inputCloned.includes("choices__input--cloned")
  );
}
 
/** Re-rank one Choices search so an exact id is the first hit. */
export function patchChoicesSearcher(instance: {
  _searcher?: ChoiceSearcher;
}): void {
  const searcher = instance._searcher;
  if (!searcher || patchedSearchers.has(searcher)) {
    return;
  }
  patchedSearchers.add(searcher);
  const original = searcher.search;
  searcher.search = function (query: string) {
    return promoteExactIdResults(original.call(searcher, query), query);
  };
}
 
/**
 * Choices is constructed inside MucSelect and is not configurable from here.
 * Its methods are bound during construction, which is the one moment the
 * instance is visible. The search index itself is created just after that.
 */
export function installJumpinExactIdSearch(): void {
  Iif (installed) {
    return;
  }
  installed = true;
  originalBind = Function.prototype.bind;
  const bind = originalBind;
  Function.prototype.bind = function (
    this: (...args: unknown[]) => unknown,
    thisArg: unknown,
    ...boundArgs: unknown[]
  ) {
    const bound = bind.apply(this, [thisArg, ...boundArgs]);
    Eif (isChoicesInstance(thisArg) && !scheduled.has(thisArg)) {
      scheduled.add(thisArg);
      queueMicrotask(() => patchChoicesSearcher(thisArg));
    }
    return bound;
  } as typeof Function.prototype.bind;
}
 
/** Puts method binding back the way the page had it. */
export function uninstallJumpinExactIdSearch(): void {
  Iif (!installed || !originalBind) {
    return;
  }
  Function.prototype.bind = originalBind;
  originalBind = null;
  installed = false;
}