mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-09 16:48:50 +00:00
Add getFilterLimit function
This commit is contained in:
17
filter.ts
17
filter.ts
@@ -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,
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user