Skip to content

Commit

Permalink
feat(animegarden): js version of database filter
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Oct 5, 2023
1 parent 6f85666 commit ce96fe7
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
60 changes: 60 additions & 0 deletions packages/animegarden/src/garden/filter.ts
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));
}
8 changes: 5 additions & 3 deletions packages/animegarden/src/garden/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ import type {
FetchResourcesOptions,
FetchResourceDetailOptions
} from './types';
import { stringifySearchURL } from './url';

import { retryFn } from './utils';
import { stringifySearchURL } from './url';

export * from './url';

export * from './types';

export { AllFansubs, findFansub } from './constant';

export { normalizeTitle } from './utils';

export { makeResourcesFilter } from './filter';

export { AllFansubs, findFansub } from './constant';

export const DefaultBaseURL = 'https://garden.onekuma.cn/api/';

interface FetchResourcesResult {
Expand Down

0 comments on commit ce96fe7

Please sign in to comment.