Skip to content

Filter

Types and functions for filtering Nostr events.

Import

ts
import { matchFilter, matchFilters } from 'nostr-core'

Filter Type

ts
type Filter = {
  ids?: string[]
  kinds?: number[]
  authors?: string[]
  since?: number
  until?: number
  limit?: number
  search?: string
  [key: `#${string}`]: string[] | undefined
}
FieldTypeDescription
idsstring[]?Match event IDs
kindsnumber[]?Match event kinds
authorsstring[]?Match author pubkeys
sincenumber?Events created after this timestamp
untilnumber?Events created before this timestamp
limitnumber?Max events to return
searchstring?Full-text search query
#<tag>string[]?Match tag values (e.g. #e, #p)

Tag Filters

Filter by tag values using the # prefix:

ts
const filter: Filter = {
  kinds: [1],
  '#e': ['event_id_1', 'event_id_2'], // Events referencing these event IDs
  '#p': ['pubkey_1'],                  // Events mentioning this pubkey
}

matchFilter

ts
function matchFilter(filter: Filter, event: NostrEvent): boolean

Tests whether an event matches a single filter. All specified conditions must match (logical AND).

ParameterTypeDescription
filterFilterFilter to test against
eventNostrEventEvent to test

Returns: boolean

ts
const filter = { kinds: [1], authors: ['abc...'] }

if (matchFilter(filter, event)) {
  console.log('Event matches')
}

matchFilters

ts
function matchFilters(filters: Filter[], event: NostrEvent): boolean

Tests whether an event matches any filter in the array (logical OR).

ParameterTypeDescription
filtersFilter[]Filters to test against
eventNostrEventEvent to test

Returns: boolean

ts
const filters = [
  { kinds: [1] },
  { kinds: [6], '#e': ['some_id'] },
]

if (matchFilters(filters, event)) {
  console.log('Event matches at least one filter')
}

Released under the MIT License.