applesauce-core
Advanced tools
Comparing version 0.0.0-next-20241213180142 to 0.0.0-next-20241219164005
@@ -5,2 +5,3 @@ import { NostrEvent, VerifiedEvent } from "nostr-tools"; | ||
export declare const FromCacheSymbol: unique symbol; | ||
export declare const ReplaceableIdentifierSymbol: unique symbol; | ||
declare module "nostr-tools" { | ||
@@ -44,1 +45,6 @@ interface Event { | ||
export declare function isFromCache(event: NostrEvent): boolean; | ||
/** | ||
* Returns the replaceable identifier for a replaceable event | ||
* @throws | ||
*/ | ||
export declare function getReplaceableIdentifier(event: NostrEvent): string; |
import { kinds, verifiedSymbol } from "nostr-tools"; | ||
import { INDEXABLE_TAGS } from "../event-store/common.js"; | ||
import { getHiddenTags } from "./hidden-tags.js"; | ||
import { getOrComputeCachedValue } from "./cache.js"; | ||
import { isParameterizedReplaceableKind } from "nostr-tools/kinds"; | ||
export const EventUIDSymbol = Symbol.for("event-uid"); | ||
export const EventIndexableTagsSymbol = Symbol.for("indexable-tags"); | ||
export const FromCacheSymbol = Symbol.for("from-cache"); | ||
export const ReplaceableIdentifierSymbol = Symbol.for("replaceable-identifier"); | ||
/** | ||
@@ -89,1 +92,15 @@ * Checks if an object is a nostr event | ||
} | ||
/** | ||
* Returns the replaceable identifier for a replaceable event | ||
* @throws | ||
*/ | ||
export function getReplaceableIdentifier(event) { | ||
if (!isParameterizedReplaceableKind(event.kind)) | ||
throw new Error("Event is not replaceable"); | ||
return getOrComputeCachedValue(event, ReplaceableIdentifierSymbol, () => { | ||
const d = getTagValue(event, "d"); | ||
if (d === undefined) | ||
throw new Error("Event missing identifier"); | ||
return d; | ||
}); | ||
} |
@@ -12,2 +12,3 @@ export * from "./bolt11.js"; | ||
export * from "./hidden-tags.js"; | ||
export * from "./json.js"; | ||
export * from "./lnurl.js"; | ||
@@ -14,0 +15,0 @@ export * from "./lru.js"; |
@@ -12,2 +12,3 @@ export * from "./bolt11.js"; | ||
export * from "./hidden-tags.js"; | ||
export * from "./json.js"; | ||
export * from "./lnurl.js"; | ||
@@ -14,0 +15,0 @@ export * from "./lru.js"; |
import { naddrEncode, neventEncode, noteEncode, nprofileEncode, npubEncode, nsecEncode, } from "nostr-tools/nip19"; | ||
import { getPublicKey, kinds } from "nostr-tools"; | ||
import { getReplaceableIdentifier } from "./event.js"; | ||
import { safeRelayUrls } from "./relays.js"; | ||
import { getTagValue } from "./index.js"; | ||
import { isParameterizedReplaceableKind } from "nostr-tools/kinds"; | ||
@@ -160,5 +160,3 @@ export function parseCoordinate(a, requireD = false, silent = true) { | ||
throw new Error("Cant get AddressPointer for non-replaceable event"); | ||
const d = getTagValue(event, "d"); | ||
if (!d) | ||
throw new Error("Event missing identifier"); | ||
const d = getReplaceableIdentifier(event); | ||
return { | ||
@@ -186,5 +184,3 @@ identifier: d, | ||
if (kinds.isParameterizedReplaceableKind(event.kind)) { | ||
const d = getTagValue(event, "d"); | ||
if (!d) | ||
throw new Error("Event missing identifier"); | ||
const d = getReplaceableIdentifier(event); | ||
return { | ||
@@ -191,0 +187,0 @@ type: "naddr", |
@@ -13,1 +13,14 @@ /** Checks if tag is an "e" tag and has at least one value */ | ||
export declare function isTTag(tag: string[]): tag is ["t", string, ...string[]]; | ||
/** A pipeline that filters and maps each tag */ | ||
type TagPipe = { | ||
<A>(tags: string[][], ta: (tag: string[]) => A | undefined): A[]; | ||
<A, B>(tags: string[][], ta: (tag: string[]) => A | undefined, ab: (a: A) => B | undefined): B[]; | ||
<A, B, C>(tags: string[][], ta: (tag: string[]) => A | undefined, ab: (a: A) => B | undefined, bc: (b: B) => C | undefined): C[]; | ||
<A, B, C, D>(tags: string[][], ta: (tag: string[]) => A | undefined, ab: (a: A) => B | undefined, bc: (b: B) => C | undefined, cd: (c: C) => D | undefined): D[]; | ||
<A, B, C, D, E>(tags: string[][], ta: (tag: string[]) => A | undefined, ab: (a: A) => B | undefined, bc: (b: B) => C | undefined, cd: (c: C) => D | undefined, de: (d: D) => E | undefined): E[]; | ||
<A, B, C, D, E, F>(tags: string[][], ta: (tag: string[]) => A | undefined, ab: (a: A) => B | undefined, bc: (b: B) => C | undefined, cd: (c: C) => D | undefined, de: (d: D) => E | undefined, ef: (e: E) => F | undefined): F[]; | ||
<A, B, C, D, E, F, G>(tags: string[][], ta: (tag: string[]) => A | undefined, ab: (a: A) => B | undefined, bc: (b: B) => C | undefined, cd: (c: C) => D | undefined, de: (d: D) => E | undefined, ef: (e: E) => F | undefined, fg: (f: F) => G | undefined): G[]; | ||
}; | ||
/** Filter and transform tags */ | ||
export declare const processTags: TagPipe; | ||
export {}; |
@@ -25,1 +25,19 @@ /** Checks if tag is an "e" tag and has at least one value */ | ||
} | ||
/** Filter and transform tags */ | ||
export const processTags = (tags, ...fns) => { | ||
return fns.reduce((step, fn) => { | ||
const next = []; | ||
for (const value of step) { | ||
try { | ||
const result = fn(value); | ||
if (result === undefined) | ||
continue; // value is undefined, ignore | ||
next.push(result); | ||
} | ||
catch (error) { | ||
// failed to process value, ignore | ||
} | ||
} | ||
return next; | ||
}, tags); | ||
}; |
{ | ||
"name": "applesauce-core", | ||
"version": "0.0.0-next-20241213180142", | ||
"version": "0.0.0-next-20241219164005", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "type": "module", |
134453
107
3403