aboutsummaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
authorRitesh Ghosh <[email protected]>2024-03-25 19:00:52 +0530
committerRitesh Ghosh <[email protected]>2024-03-25 19:00:52 +0530
commit79d0bdf05f86c5d5411f9473889442000786322f (patch)
treeb06e873dc3eca4752a29d6356c2a5b936f5cd40f /src/utils
parentbe672c542262b4ea494b960d05d8dc0447885997 (diff)
downloadaniwatch-api-79d0bdf05f86c5d5411f9473889442000786322f.tar.xz
aniwatch-api-79d0bdf05f86c5d5411f9473889442000786322f.zip
feat(advancedSearch): add utility methods related to advanced search
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/methods.ts85
1 files changed, 85 insertions, 0 deletions
diff --git a/src/utils/methods.ts b/src/utils/methods.ts
index 2e062d7..9d89822 100644
--- a/src/utils/methods.ts
+++ b/src/utils/methods.ts
@@ -5,6 +5,17 @@ import type {
Top10AnimeTimePeriod,
} from "../types/anime.js";
import type { CheerioAPI, SelectorType } from "cheerio";
+import {
+ genresIdMap,
+ languageIdMap,
+ ratedIdMap,
+ scoreIdMap,
+ seasonIdMap,
+ sortIdMap,
+ statusIdMap,
+ typeIdMap,
+} from "./constants.js";
+import { type FilterKeys } from "../types/controllers/animeSearch.js";
import createHttpError, { HttpError } from "http-errors";
export const extractAnimes = (
@@ -192,6 +203,80 @@ export function retrieveServerId(
);
}
+function getGenresFilterVal(genreNames: string[]): string | undefined {
+ if (genreNames.length < 1) {
+ return undefined;
+ }
+ return genreNames.map((name) => genresIdMap[name]).join(",");
+}
+
+export function getSearchFilterValue(
+ key: FilterKeys,
+ rawValue: string
+): string | undefined {
+ rawValue = rawValue.trim();
+ if (!rawValue) return undefined;
+
+ switch (key) {
+ case "genres": {
+ return getGenresFilterVal(rawValue.split(","));
+ }
+ case "type": {
+ const val = typeIdMap[rawValue] ?? 0;
+ return val === 0 ? undefined : `${val}`;
+ }
+ case "status": {
+ const val = statusIdMap[rawValue] ?? 0;
+ return val === 0 ? undefined : `${val}`;
+ }
+ case "rated": {
+ const val = ratedIdMap[rawValue] ?? 0;
+ return val === 0 ? undefined : `${val}`;
+ }
+ case "score": {
+ const val = scoreIdMap[rawValue] ?? 0;
+ return val === 0 ? undefined : `${val}`;
+ }
+ case "season": {
+ const val = seasonIdMap[rawValue] ?? 0;
+ return val === 0 ? undefined : `${val}`;
+ }
+ case "language": {
+ const val = languageIdMap[rawValue] ?? 0;
+ return val === 0 ? undefined : `${val}`;
+ }
+ case "sort": {
+ return sortIdMap[rawValue] ?? undefined;
+ }
+ default:
+ return undefined;
+ }
+}
+
+// this fn tackles both start_date and end_date
+export function getSearchDateFilterValue(
+ isStartDate: boolean,
+ rawValue: string
+): string[] | undefined {
+ rawValue = rawValue.trim();
+ if (!rawValue) return undefined;
+
+ const dateRegex = /^\d{4}-([0-9]|1[0-2])-([0-9]|[12][0-9]|3[01])$/;
+ const dateCategory = isStartDate ? "s" : "e";
+ const [year, month, date] = rawValue.split("-");
+
+ if (!dateRegex.test(rawValue)) {
+ return undefined;
+ }
+
+ // sample return -> [sy=2023, sm=10, sd=11]
+ return [
+ Number(year) > 0 ? `${dateCategory}y=${year}` : "",
+ Number(month) > 0 ? `${dateCategory}m=${month}` : "",
+ Number(date) > 0 ? `${dateCategory}d=${date}` : "",
+ ].filter((d) => Boolean(d));
+}
+
export function substringAfter(str: string, toFind: string) {
const index = str.indexOf(toFind);
return index == -1 ? "" : str.substring(index + toFind.length);