diff options
| author | Ritesh Ghosh <[email protected]> | 2024-03-25 19:02:36 +0530 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-03-25 19:02:36 +0530 |
| commit | 176a7a4bd722ff421a0e3836fa444bd912bcbe4e (patch) | |
| tree | b06e873dc3eca4752a29d6356c2a5b936f5cd40f /src/utils/methods.ts | |
| parent | e136af03bf174c0ea46a5c3eec9f3599ad8cd2be (diff) | |
| parent | 79d0bdf05f86c5d5411f9473889442000786322f (diff) | |
| download | aniwatch-api-176a7a4bd722ff421a0e3836fa444bd912bcbe4e.tar.xz aniwatch-api-176a7a4bd722ff421a0e3836fa444bd912bcbe4e.zip | |
Merge pull request #34 from ghoshRitesh12/advanced-search
Add advanced search feature
Diffstat (limited to 'src/utils/methods.ts')
| -rw-r--r-- | src/utils/methods.ts | 85 |
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); |
