Skip to content

Commit

Permalink
feat: temporary ?disableFuzzy parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Sv443 committed Sep 19, 2023
1 parent 102c234 commit ab6b55c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/routes/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -19,6 +19,7 @@ export function initSearchRoutes(router: Router) {
song: String(song),
}),
threshold,
disableFuzzy: typeof disableFuzzy === "string",
});

if(!meta || meta.all.length < 1)
Expand All @@ -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);
Expand All @@ -53,6 +54,7 @@ export function initSearchRoutes(router: Router) {
song: String(song),
}),
threshold,
disableFuzzy: typeof disableFuzzy === "string",
});

if(!meta || !meta.top)
Expand Down
11 changes: 11 additions & 0 deletions src/songData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export async function getMeta({
artist,
song,
threshold,
disableFuzzy,
}: GetMetaArgs): Promise<GetMetaResult | null>
{
const query = q ? q : `${artist} ${song}`;
Expand Down Expand Up @@ -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 => {
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export interface GetMetaArgs {
artist?: string;
song?: string;
threshold?: number;
disableFuzzy?: boolean;
}

export type SearchFilterArgs = GetMetaArgs;
Expand Down

0 comments on commit ab6b55c

Please sign in to comment.