feat: searching from api

This commit is contained in:
Zamitto
2024-09-12 11:12:36 -03:00
parent 262e95e4db
commit 2e82c29f4c
7 changed files with 83 additions and 62 deletions

View File

@@ -1,36 +1,15 @@
import { SteamGame } from "@types";
import { orderBy, slice } from "lodash-es";
import flexSearch from "flexsearch";
import { slice } from "lodash-es";
import fs from "node:fs";
import { formatName } from "@shared";
import { workerData } from "node:worker_threads";
const steamGamesIndex = new flexSearch.Index({
tokenize: "reverse",
});
const { steamGamesPath } = workerData;
const data = fs.readFileSync(steamGamesPath, "utf-8");
const steamGames = JSON.parse(data) as SteamGame[];
for (let i = 0; i < steamGames.length; i++) {
const steamGame = steamGames[i];
const formattedName = formatName(steamGame.name);
steamGamesIndex.add(i, formattedName);
}
export const search = (options: flexSearch.SearchOptions) => {
const results = steamGamesIndex.search(options);
const games = results.map((index) => steamGames[index]);
return orderBy(games, ["name"], ["asc"]);
};
export const getById = (id: number) =>
steamGames.find((game) => game.id === id);