From cdbc656e991eab1fce43fe071e3480d02467792c Mon Sep 17 00:00:00 2001 From: Jon Staab Date: Fri, 13 Dec 2024 16:24:28 -0800 Subject: [PATCH] Add a few tag functions --- packages/util/src/Tags.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/util/src/Tags.ts b/packages/util/src/Tags.ts index 730b119..d9af2db 100644 --- a/packages/util/src/Tags.ts +++ b/packages/util/src/Tags.ts @@ -8,9 +8,18 @@ export const getTags = (types: string | string[], tags: string[][]) => { return tags.filter(t => types.includes(t[0])) } +export const getTag = (types: string | string[], tags: string[][]) => { + types = ensurePlural(types) + + return tags.find(t => types.includes(t[0])) +} + export const getTagValues = (types: string | string[], tags: string[][]) => getTags(types, tags).map(nth(1)) +export const getTagValue = (types: string | string[], tags: string[][]) => + getTag(types, tags)?.[1] + export const getEventTags = (tags: string[][]) => tags.filter(t => ["e"].includes(t[0]) && t[1].length === 64) @@ -26,9 +35,9 @@ export const getPubkeyTags = (tags: string[][]) => export const getPubkeyTagValues = (tags: string[][]) => getPubkeyTags(tags).map(nth(1)) -export const getTopicTags = (tags: string[][]) => tags.filter(t => ["t"].includes(t[0])) +export const getTopicTags = (tags: string[][]) => tags.filter(nthEq(0, "t")) -export const getTopicTagValues = (tags: string[][]) => getTopicTags(tags).map(nth(1)) +export const getTopicTagValues = (tags: string[][]) => getTopicTags(tags).map(t => t[1].replace(/^#/, '')) export const getRelayTags = (tags: string[][]) => tags.filter(t => ["r", "relay"].includes(t[0]) && isRelayUrl(t[1] || "")) @@ -88,3 +97,5 @@ export const getRelayHints = (tags: string[][]) => uniq(tags.flatMap(t => t.slice(2).filter(isShareableRelayUrl))) export const uniqTags = (tags: string[][]) => uniqBy(t => t.join(":"), tags) + +export const tagsFromIMeta = (imeta: string[]) => imeta.map((m: string) => m.split(" "))