fix: get multiple tag values

This commit is contained in:
enes 2024-11-05 14:42:22 +01:00
parent 73a7b1c1ee
commit 169ab37304
2 changed files with 19 additions and 3 deletions

View File

@ -1,6 +1,6 @@
import { NDKEvent } from '@nostr-dev-kit/ndk' import { NDKEvent } from '@nostr-dev-kit/ndk'
import { BlogCardDetails, BlogDetails } from 'types' import { BlogCardDetails, BlogDetails } from 'types'
import { getFirstTagValue, getFirstTagValueAsInt, getTagValue } from './nostr' import { getFirstTagValue, getFirstTagValueAsInt, getTagValues } from './nostr'
import { kinds, nip19 } from 'nostr-tools' import { kinds, nip19 } from 'nostr-tools'
export const extractBlogDetails = (event: NDKEvent): Partial<BlogDetails> => ({ export const extractBlogDetails = (event: NDKEvent): Partial<BlogDetails> => ({
@ -17,7 +17,7 @@ export const extractBlogDetails = (event: NDKEvent): Partial<BlogDetails> => ({
rTag: getFirstTagValue(event, 'r') || 'N/A', rTag: getFirstTagValue(event, 'r') || 'N/A',
dTag: getFirstTagValue(event, 'd'), dTag: getFirstTagValue(event, 'd'),
aTag: getFirstTagValue(event, 'a'), aTag: getFirstTagValue(event, 'a'),
tTags: getTagValue(event, 't') || [] tTags: getTagValues(event, 't') || []
}) })
export const extractBlogCardDetails = ( export const extractBlogCardDetails = (

View File

@ -65,11 +65,27 @@ export const getTagValue = (
return null return null
} }
export const getTagValues = (
event: Event | NDKEvent,
tagIdentifier: string
): string[] | null => {
// Find the tag in the event's tags array where the first element matches the tagIdentifier.
const tags = event.tags.filter((item) => item[0] === tagIdentifier)
// If a matching tag is found, return the rest of the elements in the tag (i.e., the values).
if (tags && tags.length) {
return tags.map((item) => item[1]) // Returning only the values
}
// Return null if no matching tag is found.
return null
}
export const getFirstTagValue = ( export const getFirstTagValue = (
event: Event | NDKEvent, event: Event | NDKEvent,
tagIdentifier: string tagIdentifier: string
) => { ) => {
const tags = getTagValue(event, tagIdentifier) const tags = getTagValues(event, tagIdentifier)
return tags && tags.length ? tags[0] : undefined return tags && tags.length ? tags[0] : undefined
} }