Jelajahi Sumber

feat: temporary ?disableFuzzy parameter

Sv443 1 tahun lalu
induk
melakukan
ab6b55c740
3 mengubah file dengan 16 tambahan dan 2 penghapusan
  1. 4 2
      src/routes/search.ts
  2. 11 0
      src/songData.ts
  3. 1 0
      src/types.ts

+ 4 - 2
src/routes/search.ts

@@ -5,7 +5,7 @@ import { getMeta } from "../songData";
 export function initSearchRoutes(router: Router) {
   router.get("/search", async (req, res) => {
     try {
-      const { q, artist, song, format: fmt, threshold: thr } = req.query;
+      const { q, artist, song, format: fmt, threshold: thr, disableFuzzy } = req.query;
 
       const format: string = fmt ? String(fmt) : "json";
       const threshold = isNaN(Number(thr)) ? undefined : Number(thr);
@@ -19,6 +19,7 @@ export function initSearchRoutes(router: Router) {
             song: String(song),
           }),
           threshold,
+          disableFuzzy: typeof disableFuzzy === "string",
         });
 
         if(!meta || meta.all.length < 1)
@@ -39,7 +40,7 @@ export function initSearchRoutes(router: Router) {
 
   router.get("/search/top", async (req, res) => {
     try {
-      const { q, artist, song, format: fmt, threshold: thr } = req.query;
+      const { q, artist, song, format: fmt, threshold: thr, disableFuzzy } = req.query;
 
       const format: string = fmt ? String(fmt) : "json";
       const threshold = isNaN(Number(thr)) ? undefined : Number(thr);
@@ -53,6 +54,7 @@ export function initSearchRoutes(router: Router) {
             song: String(song),
           }),
           threshold,
+          disableFuzzy: typeof disableFuzzy === "string",
         });
 
         if(!meta || !meta.top)

+ 11 - 0
src/songData.ts

@@ -19,6 +19,7 @@ export async function getMeta({
   artist,
   song,
   threshold,
+  disableFuzzy,
 }: GetMetaArgs): Promise<GetMetaResult | null>
 {
   const query = q ? q : `${artist} ${song}`;
@@ -73,6 +74,16 @@ export async function getMeta({
         id: result.id ?? null,
       }));
 
+    if(disableFuzzy === true) {
+      if(hits.length === 0)
+        return null;
+
+      return {
+        top: hits[0]!,
+        all: hits.slice(0, 10),
+      };
+    }
+
     const scoreMap: Record<string, number> = {};
 
     hits = hits.map(h => {

+ 1 - 0
src/types.ts

@@ -53,6 +53,7 @@ export interface GetMetaArgs {
   artist?: string;
   song?: string;
   threshold?: number;
+  disableFuzzy?: boolean;
 }
 
 export type SearchFilterArgs = GetMetaArgs;