mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-09 16:48:50 +00:00
nip27: make matchAll a generator function
This commit is contained in:
@@ -6,7 +6,7 @@ test('matchAll', () => {
|
|||||||
'Hello nostr:npub108pv4cg5ag52nq082kd5leu9ffrn2gdg6g4xdwatn73y36uzplmq9uyev6!\n\nnostr:note1gmtnz6q2m55epmlpe3semjdcq987av3jvx4emmjsa8g3s9x7tg4sclreky'
|
'Hello nostr:npub108pv4cg5ag52nq082kd5leu9ffrn2gdg6g4xdwatn73y36uzplmq9uyev6!\n\nnostr:note1gmtnz6q2m55epmlpe3semjdcq987av3jvx4emmjsa8g3s9x7tg4sclreky'
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(result).toEqual([
|
expect([...result]).toEqual([
|
||||||
{
|
{
|
||||||
uri: 'nostr:npub108pv4cg5ag52nq082kd5leu9ffrn2gdg6g4xdwatn73y36uzplmq9uyev6',
|
uri: 'nostr:npub108pv4cg5ag52nq082kd5leu9ffrn2gdg6g4xdwatn73y36uzplmq9uyev6',
|
||||||
value: 'npub108pv4cg5ag52nq082kd5leu9ffrn2gdg6g4xdwatn73y36uzplmq9uyev6',
|
value: 'npub108pv4cg5ag52nq082kd5leu9ffrn2gdg6g4xdwatn73y36uzplmq9uyev6',
|
||||||
|
|||||||
8
nip27.ts
8
nip27.ts
@@ -14,20 +14,20 @@ export interface NostrURIMatch extends nip21.NostrURI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Find and decode all NIP-21 URIs. */
|
/** Find and decode all NIP-21 URIs. */
|
||||||
export function matchAll(content: string): NostrURIMatch[] {
|
export function * matchAll(content: string): Iterable<NostrURIMatch> {
|
||||||
const matches = content.matchAll(regex())
|
const matches = content.matchAll(regex())
|
||||||
|
|
||||||
return [...matches].map(match => {
|
for (const match of matches) {
|
||||||
const [uri, value] = match
|
const [uri, value] = match
|
||||||
|
|
||||||
return {
|
yield {
|
||||||
uri: uri as `nostr:${string}`,
|
uri: uri as `nostr:${string}`,
|
||||||
value,
|
value,
|
||||||
decoded: nip19.decode(value),
|
decoded: nip19.decode(value),
|
||||||
start: match.index!,
|
start: match.index!,
|
||||||
end: match.index! + uri.length
|
end: match.index! + uri.length
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user