Refactor imports: use file extension, improve tree shaking, update tests

This commit is contained in:
Alex Gleason
2023-05-12 11:00:14 -05:00
committed by fiatjaf_
parent 9f896479d0
commit 08885ab8da
42 changed files with 244 additions and 212 deletions

View File

@@ -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)
})
})
}