dev1/backend/utils/fuzzyCareerLookup.js

24 lines
739 B
JavaScript

import path from "path";
import fs from "node:fs";
import { fileURLToPath } from "url";
import Fuse from "fuse.js";
/* resolve …/backend/utils → …/public/careers_with_ratings.json */
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const careersPath = path.join(__dirname, "..", "..", "public", "careers_with_ratings.json");
const CAREERS = JSON.parse(fs.readFileSync(careersPath, "utf-8"));
const fuse = new Fuse(CAREERS, {
keys : ["title"],
threshold : 0.30,
ignoreLocation: true,
});
export function fuzzyCareerLookup(label = "") {
if (!label.trim()) return null;
const [hit] = fuse.search(label.trim(), { limit: 1 });
return hit?.item ?? null;
}