mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-08 16:28:49 +00:00
mergeFilters()
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import {matchFilter, matchFilters} from './filter.ts'
|
||||
import {matchFilter, matchFilters, mergeFilters} from './filter.ts'
|
||||
import {buildEvent} from './test-helpers.ts'
|
||||
|
||||
describe('Filter', () => {
|
||||
@@ -18,7 +18,7 @@ describe('Filter', () => {
|
||||
kind: 1,
|
||||
pubkey: 'abc',
|
||||
created_at: 150,
|
||||
tags: [['tag', 'value']],
|
||||
tags: [['tag', 'value']]
|
||||
})
|
||||
|
||||
const result = matchFilter(filter, event)
|
||||
@@ -162,7 +162,12 @@ describe('Filter', () => {
|
||||
{authors: ['abc'], limit: 3}
|
||||
]
|
||||
|
||||
const event = buildEvent({id: '123', kind: 1, pubkey: 'abc', created_at: 150})
|
||||
const event = buildEvent({
|
||||
id: '123',
|
||||
kind: 1,
|
||||
pubkey: 'abc',
|
||||
created_at: 150
|
||||
})
|
||||
|
||||
const result = matchFilters(filters, event)
|
||||
|
||||
@@ -189,11 +194,35 @@ describe('Filter', () => {
|
||||
{kinds: [1], limit: 2},
|
||||
{authors: ['abc'], limit: 3}
|
||||
]
|
||||
const event = buildEvent({id: '456', kind: 2, pubkey: 'def', created_at: 200})
|
||||
const event = buildEvent({
|
||||
id: '456',
|
||||
kind: 2,
|
||||
pubkey: 'def',
|
||||
created_at: 200
|
||||
})
|
||||
|
||||
const result = matchFilters(filters, event)
|
||||
|
||||
expect(result).toEqual(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('mergeFilters', () => {
|
||||
it('should merge filters', () => {
|
||||
expect(
|
||||
mergeFilters(
|
||||
{ids: ['a', 'b'], limit: 3},
|
||||
{authors: ['x'], ids: ['b', 'c']}
|
||||
)
|
||||
).toEqual({ids: ['a', 'b', 'c'], limit: 3, authors: ['x']})
|
||||
|
||||
expect(
|
||||
mergeFilters(
|
||||
{kinds: [1], since: 15, until: 30},
|
||||
{since: 10, kinds: [7], until: 15},
|
||||
{kinds: [9, 10]}
|
||||
)
|
||||
).toEqual({kinds: [1, 7, 9, 10], since: 10, until: 30})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
34
filter.ts
34
filter.ts
@@ -56,3 +56,37 @@ export function matchFilters(
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
export function mergeFilters(...filters: Filter<number>[]): Filter<number> {
|
||||
let result: Filter<number> = {}
|
||||
for (let i = 0; i < filters.length; i++) {
|
||||
let filter = filters[i]
|
||||
Object.entries(filter).forEach(([property, values]) => {
|
||||
if (
|
||||
property === 'kinds' ||
|
||||
property === 'ids' ||
|
||||
property === 'authors' ||
|
||||
property[0] === '#'
|
||||
) {
|
||||
// @ts-ignore
|
||||
result[property] = result[property] || []
|
||||
// @ts-ignore
|
||||
for (let v = 0; v < values.length; v++) {
|
||||
// @ts-ignore
|
||||
let value = values[v]
|
||||
// @ts-ignore
|
||||
if (!result[property].includes(value)) result[property].push(value)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
if (filter.limit && (!result.limit || filter.limit > result.limit))
|
||||
result.limit = filter.limit
|
||||
if (filter.until && (!result.until || filter.until > result.until))
|
||||
result.until = filter.until
|
||||
if (filter.since && (!result.since || filter.since < result.since))
|
||||
result.since = filter.since
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "nostr-tools",
|
||||
"version": "1.11.2",
|
||||
"version": "1.12.0",
|
||||
"description": "Tools for making a Nostr client.",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
||||
Reference in New Issue
Block a user