applesauce-core 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,12 +1,12 @@
1
1
  /// <reference types="debug" />
2
2
  /// <reference types="zen-observable" />
3
3
  import { Filter, NostrEvent } from "nostr-tools";
4
- import { LRU } from "../utils/lru.js";
4
+ import { LRU } from "../helpers/lru.js";
5
5
  /**
6
6
  * An in-memory database for nostr events
7
7
  */
8
8
  export declare class Database {
9
- log: import("debug").Debugger;
9
+ protected log: import("debug").Debugger;
10
10
  /** Indexes */
11
11
  protected kinds: Map<number, Set<import("nostr-tools").Event>>;
12
12
  protected authors: Map<string, Set<import("nostr-tools").Event>>;
@@ -56,6 +56,7 @@ export declare class Database {
56
56
  flatMap<R_2>(callback: (value: import("nostr-tools").Event) => ZenObservable.ObservableLike<R_2>): import("zen-observable")<R_2>;
57
57
  concat<R_3>(...observable: import("zen-observable")<R_3>[]): import("zen-observable")<R_3>;
58
58
  };
59
+ get size(): number;
59
60
  protected claims: WeakMap<import("nostr-tools").Event, any>;
60
61
  /** Index helper methods */
61
62
  protected getKindIndex(kind: number): Set<import("nostr-tools").Event>;
@@ -3,7 +3,7 @@ import PushStream from "zen-push";
3
3
  import { getEventUID, getIndexableTags, getReplaceableUID } from "../helpers/event.js";
4
4
  import { INDEXABLE_TAGS } from "./common.js";
5
5
  import { logger } from "../logger.js";
6
- import { LRU } from "../utils/lru.js";
6
+ import { LRU } from "../helpers/lru.js";
7
7
  /**
8
8
  * An in-memory database for nostr events
9
9
  */
@@ -25,6 +25,9 @@ export class Database {
25
25
  updated = this.updatedSignal.observable;
26
26
  /** A stream of events removed of the database */
27
27
  deleted = this.deletedSignal.observable;
28
+ get size() {
29
+ return this.events.size;
30
+ }
28
31
  claims = new WeakMap();
29
32
  /** Index helper methods */
30
33
  getKindIndex(kind) {
@@ -0,0 +1,2 @@
1
+ import { EventTemplate, NostrEvent } from "nostr-tools";
2
+ export declare function getEmojiTag(event: NostrEvent | EventTemplate, code: string): ["emoji", string, string];
@@ -0,0 +1,4 @@
1
+ export function getEmojiTag(event, code) {
2
+ code = code.replace(/^:|:$/g, "").toLocaleLowerCase();
3
+ return event.tags.filter((t) => t[0] === "emoji" && t[1] && t[2]).find((t) => t[1].toLowerCase() === code);
4
+ }
@@ -0,0 +1,2 @@
1
+ import { EventTemplate, NostrEvent } from "nostr-tools";
2
+ export declare function getHashtagTag(event: NostrEvent | EventTemplate, hashtag: string): ["t", string];
@@ -0,0 +1,7 @@
1
+ import { stripInvisibleChar } from "./string.js";
2
+ export function getHashtagTag(event, hashtag) {
3
+ hashtag = stripInvisibleChar(hashtag.replace(/^#/, "").toLocaleLowerCase());
4
+ return event.tags
5
+ .filter((t) => t[0] === "t" && t[1])
6
+ .find((t) => stripInvisibleChar(t[1].toLowerCase()) === hashtag);
7
+ }
@@ -8,3 +8,7 @@ export * from "./pointers.js";
8
8
  export * from "./string.js";
9
9
  export * from "./time.js";
10
10
  export * from "./tags.js";
11
+ export * from "./emoji.js";
12
+ export * from "./lru.js";
13
+ export * from "./hashtag.js";
14
+ export * from "./url.js";
@@ -8,3 +8,7 @@ export * from "./pointers.js";
8
8
  export * from "./string.js";
9
9
  export * from "./time.js";
10
10
  export * from "./tags.js";
11
+ export * from "./emoji.js";
12
+ export * from "./lru.js";
13
+ export * from "./hashtag.js";
14
+ export * from "./url.js";
@@ -1,2 +1,4 @@
1
1
  export declare function isHex(str?: string): boolean;
2
2
  export declare function isHexKey(key?: string): boolean;
3
+ export declare function stripInvisibleChar(str: string): string;
4
+ export declare function stripInvisibleChar(str?: string | undefined): string | undefined;
@@ -8,3 +8,6 @@ export function isHexKey(key) {
8
8
  return true;
9
9
  return false;
10
10
  }
11
+ export function stripInvisibleChar(str) {
12
+ return str && str.replaceAll(/[\p{Cf}\p{Zs}]/gu, "");
13
+ }
@@ -0,0 +1,11 @@
1
+ export declare const convertToUrl: (url: string | URL) => URL;
2
+ export declare const getURLFilename: (url: URL) => string | undefined;
3
+ export declare const IMAGE_EXT: string[];
4
+ export declare const VIDEO_EXT: string[];
5
+ export declare const STREAM_EXT: string[];
6
+ export declare const AUDIO_EXT: string[];
7
+ export declare function isVisualMediaURL(url: string | URL): boolean;
8
+ export declare function isImageURL(url: string | URL): boolean;
9
+ export declare function isVideoURL(url: string | URL): boolean;
10
+ export declare function isStreamURL(url: string | URL): boolean;
11
+ export declare function isAudioURL(url: string | URL): boolean;
@@ -0,0 +1,29 @@
1
+ export const convertToUrl = (url) => (url instanceof URL ? url : new URL(url));
2
+ export const getURLFilename = (url) => url.pathname.split("/").pop()?.toLocaleLowerCase() || url.searchParams.get("filename")?.toLocaleLowerCase();
3
+ export const IMAGE_EXT = [".svg", ".gif", ".png", ".jpg", ".jpeg", ".webp", ".avif"];
4
+ export const VIDEO_EXT = [".mp4", ".mkv", ".webm", ".mov"];
5
+ export const STREAM_EXT = [".m3u8"];
6
+ export const AUDIO_EXT = [".mp3", ".wav", ".ogg", ".aac"];
7
+ export function isVisualMediaURL(url) {
8
+ return isImageURL(url) || isVideoURL(url) || isStreamURL(url);
9
+ }
10
+ export function isImageURL(url) {
11
+ url = convertToUrl(url);
12
+ const filename = getURLFilename(url);
13
+ return !!filename && IMAGE_EXT.some((ext) => filename.endsWith(ext));
14
+ }
15
+ export function isVideoURL(url) {
16
+ url = convertToUrl(url);
17
+ const filename = getURLFilename(url);
18
+ return !!filename && VIDEO_EXT.some((ext) => filename.endsWith(ext));
19
+ }
20
+ export function isStreamURL(url) {
21
+ url = convertToUrl(url);
22
+ const filename = getURLFilename(url);
23
+ return !!filename && STREAM_EXT.some((ext) => filename.endsWith(ext));
24
+ }
25
+ export function isAudioURL(url) {
26
+ url = convertToUrl(url);
27
+ const filename = getURLFilename(url);
28
+ return !!filename && AUDIO_EXT.some((ext) => filename.endsWith(ext));
29
+ }
@@ -0,0 +1,2 @@
1
+ import Observable from "zen-observable";
2
+ export declare function getValue<T>(observable: Observable<T>): T | Promise<T>;
@@ -0,0 +1,11 @@
1
+ import { isStateful } from "./stateful.js";
2
+ export function getValue(observable) {
3
+ if (isStateful(observable) && observable.value !== undefined)
4
+ return observable.value;
5
+ return new Promise((res) => {
6
+ const sub = observable.subscribe((v) => {
7
+ res(v);
8
+ sub.unsubscribe();
9
+ });
10
+ });
11
+ }
@@ -1,2 +1,3 @@
1
1
  export * from "./stateful.js";
2
2
  export * from "./throttle.js";
3
+ export * from "./getValue.js";
@@ -1,2 +1,3 @@
1
1
  export * from "./stateful.js";
2
2
  export * from "./throttle.js";
3
+ export * from "./getValue.js";
@@ -1,7 +1,8 @@
1
1
  import Observable from "zen-observable";
2
2
  import { Filter, NostrEvent } from "nostr-tools";
3
3
  import { EventStore } from "../event-store/event-store.js";
4
- import { LRU } from "../utils/lru.js";
4
+ import { StatefulObservable } from "../observable/stateful.js";
5
+ import { LRU } from "../helpers/lru.js";
5
6
  import * as Queries from "../queries/index.js";
6
7
  import { AddressPointer, EventPointer } from "nostr-tools/nip19";
7
8
  export type Query<T extends unknown> = {
@@ -13,7 +14,7 @@ export declare class QueryStore {
13
14
  static Queries: typeof Queries;
14
15
  store: EventStore;
15
16
  constructor(store: EventStore);
16
- queries: LRU<Observable<any>>;
17
+ queries: LRU<StatefulObservable<any>>;
17
18
  /** Creates a cached query */
18
19
  runQuery<T extends unknown, Args extends Array<any>>(queryConstructor: (...args: Args) => {
19
20
  key: string;
@@ -1,5 +1,5 @@
1
1
  import { stateful } from "../observable/stateful.js";
2
- import { LRU } from "../utils/lru.js";
2
+ import { LRU } from "../helpers/lru.js";
3
3
  import * as Queries from "../queries/index.js";
4
4
  export class QueryStore {
5
5
  static Queries = Queries;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "applesauce-core",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
File without changes
File without changes