Skip to content

Instantly share code, notes, and snippets.

@kuuote
Created January 8, 2024 04:22
Show Gist options
  • Save kuuote/6c34063deb1da29c5290b953e6ae676d to your computer and use it in GitHub Desktop.
Save kuuote/6c34063deb1da29c5290b953e6ae676d to your computer and use it in GitHub Desktop.
fuzzyfinderのsorterを魔改造して、プロンプトがない時の表示順序をランダム化するとかも便利か……? by atusy
import {
BaseFilter,
FilterArguments,
} from "https://deno.land/x/[email protected]/base/filter.ts";
import { Context, DduItem } from "https://deno.land/x/[email protected]/types.ts";
type Never = Record<PropertyKey, never>;
export class Filter extends BaseFilter<Never> {
#cache = new WeakMap<Context, Map<string, number>>();
filter(args: FilterArguments<Never>): DduItem[] {
if (args.input !== "") {
return args.items;
}
const cache = this.#cache.get(args.context) ?? new Map();
this.#cache.set(args.context, cache);
return args.items.toSorted((a, b) => {
const ai = cache.get(a.word) ?? Math.random();
cache.set(a.word, ai);
const bi = cache.get(b.word) ?? Math.random();
cache.set(b.word, bi);
return ai - bi;
});
}
params(): Never {
return {};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment