Add getFilterLimit function

This commit is contained in:
Alex Gleason
2024-01-03 16:59:38 -06:00
committed by fiatjaf_
parent 498c1603b0
commit 348d118ce4
2 changed files with 41 additions and 1 deletions

View File

@@ -1,4 +1,5 @@
import { Event } from './core.ts'
import { isReplaceableKind } from './kinds.ts'
export type Filter = {
ids?: string[]
@@ -70,3 +71,19 @@ export function mergeFilters(...filters: Filter[]): Filter {
return result
}
/** Calculate the intrinsic limit of a filter. This function may return `Infinity`. */
export function getFilterLimit(filter: Filter): number {
if (filter.ids && !filter.ids.length) return 0
if (filter.kinds && !filter.kinds.length) return 0
if (filter.authors && !filter.authors.length) return 0
return Math.min(
Math.max(0, filter.limit ?? Infinity),
filter.ids?.length ?? Infinity,
filter.authors?.length &&
filter.kinds?.every((kind) => isReplaceableKind(kind))
? filter.authors.length * filter.kinds.length
: Infinity,
)
}