mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-12 10:08:49 +00:00
Refactor imports: use file extension, improve tree shaking, update tests
This commit is contained in:
14
nip27.ts
14
nip27.ts
@@ -1,12 +1,12 @@
|
||||
import * as nip19 from './nip19'
|
||||
import * as nip21 from './nip21'
|
||||
import {decode} from './nip19.ts'
|
||||
import {NOSTR_URI_REGEX, type NostrURI} from './nip21.ts'
|
||||
|
||||
/** Regex to find NIP-21 URIs inside event content. */
|
||||
export const regex = () =>
|
||||
new RegExp(`\\b${nip21.NOSTR_URI_REGEX.source}\\b`, 'g')
|
||||
new RegExp(`\\b${NOSTR_URI_REGEX.source}\\b`, 'g')
|
||||
|
||||
/** Match result for a Nostr URI in event content. */
|
||||
export interface NostrURIMatch extends nip21.NostrURI {
|
||||
export interface NostrURIMatch extends NostrURI {
|
||||
/** Index where the URI begins in the event content. */
|
||||
start: number
|
||||
/** Index where the URI ends in the event content. */
|
||||
@@ -23,7 +23,7 @@ export function * matchAll(content: string): Iterable<NostrURIMatch> {
|
||||
yield {
|
||||
uri: uri as `nostr:${string}`,
|
||||
value,
|
||||
decoded: nip19.decode(value),
|
||||
decoded: decode(value),
|
||||
start: match.index!,
|
||||
end: match.index! + uri.length
|
||||
}
|
||||
@@ -51,13 +51,13 @@ export function * matchAll(content: string): Iterable<NostrURIMatch> {
|
||||
*/
|
||||
export function replaceAll(
|
||||
content: string,
|
||||
replacer: (match: nip21.NostrURI) => string
|
||||
replacer: (match: NostrURI) => string
|
||||
): string {
|
||||
return content.replaceAll(regex(), (uri, value) => {
|
||||
return replacer({
|
||||
uri: uri as `nostr:${string}`,
|
||||
value,
|
||||
decoded: nip19.decode(value)
|
||||
decoded: decode(value)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user