-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(animegarden): js version of database filter
- Loading branch information
Showing
2 changed files
with
65 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import type { Resource } from '../types'; | ||
|
||
import type { FilterOptions } from './types'; | ||
|
||
import { parseSearchURL } from './url'; | ||
import { normalizeTitle } from './utils'; | ||
|
||
export function makeResourcesFilter( | ||
options: Omit<FilterOptions, 'page' | 'pageSize'> | ||
): (resource: Resource) => boolean { | ||
const resolved = parseSearchURL(new URLSearchParams(), options); | ||
const chains: Array<(resource: Resource) => boolean> = []; | ||
|
||
if (resolved.fansubId) { | ||
const fansubId = resolved.fansubId.map((id) => '' + id); | ||
chains.push((r) => (r.fansub ? fansubId.includes(r.fansub.id) : false)); | ||
} | ||
if (resolved.fansubName) { | ||
const fansubName = resolved.fansubName; | ||
chains.push((r) => (r.fansub ? fansubName.includes(r.fansub.name) : false)); | ||
} | ||
if (resolved.publisherId) { | ||
const publisherId = resolved.publisherId.map((id) => '' + id); | ||
chains.push((r) => publisherId.includes(r.publisher.id)); | ||
} | ||
if (resolved.type) { | ||
const type = resolved.type; | ||
chains.push((r) => r.type === type); | ||
} | ||
if (resolved.before) { | ||
const before = resolved.before.getTime(); | ||
chains.push((r) => new Date(r.createdAt).getTime() <= before); | ||
} | ||
if (resolved.after) { | ||
const after = resolved.after.getTime(); | ||
chains.push((r) => new Date(r.createdAt).getTime() >= after); | ||
} | ||
|
||
if (resolved.include || resolved.search) { | ||
const include = resolved.include ?? []; | ||
if (resolved.search) { | ||
include.push(resolved.search); | ||
} | ||
chains.push((r) => { | ||
// @ts-expect-error | ||
const titleAlt: string = r.titleAlt || normalizeTitle(r.title); | ||
return include.every((keys) => keys.some((key) => titleAlt.indexOf(key) !== -1)); | ||
}); | ||
} | ||
if (resolved.exclude) { | ||
const exclude = resolved.exclude; | ||
chains.push((r) => { | ||
// @ts-expect-error | ||
const titleAlt: string = r.titleAlt || normalizeTitle(r.title); | ||
return exclude.every((key) => titleAlt.indexOf(key) === -1); | ||
}); | ||
} | ||
|
||
return (res) => chains.every((fn) => fn(res)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters