Add some tag utils
This commit is contained in:
Generated
+1
-1
@@ -3358,7 +3358,7 @@
|
|||||||
},
|
},
|
||||||
"packages/network": {
|
"packages/network": {
|
||||||
"name": "@coracle.social/network",
|
"name": "@coracle.social/network",
|
||||||
"version": "0.0.6",
|
"version": "0.0.7",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@coracle.social/lib": "^0.0.4",
|
"@coracle.social/lib": "^0.0.4",
|
||||||
|
|||||||
@@ -47,6 +47,10 @@ export class Fluent<T> {
|
|||||||
|
|
||||||
reject = (f: (t: T) => boolean) => this.clone(this.xs.filter(t => !f(t)))
|
reject = (f: (t: T) => boolean) => this.clone(this.xs.filter(t => !f(t)))
|
||||||
|
|
||||||
|
keep = (xs: T[]) => this.filter(x => xs.includes(x))
|
||||||
|
|
||||||
|
without = (xs: T[]) => this.reject(x => xs.includes(x))
|
||||||
|
|
||||||
map = (f: (t: T) => T) => this.clone(this.xs.map(f))
|
map = (f: (t: T) => T) => this.clone(this.xs.map(f))
|
||||||
|
|
||||||
mapTo = <U>(f: (t: T) => U) => Fluent.from(this.xs.map(f))
|
mapTo = <U>(f: (t: T) => U) => Fluent.from(this.xs.map(f))
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@coracle.social/lib",
|
"name": "@coracle.social/lib",
|
||||||
"version": "0.0.4",
|
"version": "0.0.5",
|
||||||
"author": "hodlbod",
|
"author": "hodlbod",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"description": "A collection of utilities.",
|
"description": "A collection of utilities.",
|
||||||
|
|||||||
@@ -287,7 +287,7 @@ export class RouterScenario {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Adjust redundancy by limit, since if we're looking for very specific values odds
|
// Adjust redundancy by limit, since if we're looking for very specific values odds
|
||||||
// are wee're less tolerant of failure. Add more redundancy to fill our relay limit.
|
// are we're less tolerant of failure. Add more redundancy to fill our relay limit.
|
||||||
const limit = this.getLimit()
|
const limit = this.getLimit()
|
||||||
const redundancy = this.getRedundancy()
|
const redundancy = this.getRedundancy()
|
||||||
const adjustedRedundancy = Math.max(redundancy, redundancy * (limit / (allValues.size * redundancy)))
|
const adjustedRedundancy = Math.max(redundancy, redundancy * (limit / (allValues.size * redundancy)))
|
||||||
|
|||||||
+20
-11
@@ -1,6 +1,6 @@
|
|||||||
import {EventTemplate} from 'nostr-tools'
|
import {EventTemplate} from 'nostr-tools'
|
||||||
import type {OmitStatics} from '@coracle.social/lib'
|
import type {OmitStatics} from '@coracle.social/lib'
|
||||||
import {Fluent, last} from '@coracle.social/lib'
|
import {Fluent, ensurePlural, last} from '@coracle.social/lib'
|
||||||
import {isShareableRelayUrl, normalizeRelayUrl} from './Relays'
|
import {isShareableRelayUrl, normalizeRelayUrl} from './Relays'
|
||||||
import type {Address} from './Address'
|
import type {Address} from './Address'
|
||||||
import {encodeAddress, decodeAddress} from './Address'
|
import {encodeAddress, decodeAddress} from './Address'
|
||||||
@@ -51,6 +51,8 @@ export class Tags extends (Fluent<Tag> as OmitStatics<typeof Fluent<Tag>, 'from'
|
|||||||
|
|
||||||
static fromEvents = (events: Pick<EventTemplate, "tags">[]) => Tags.wrap(events.flatMap(e => e.tags || []))
|
static fromEvents = (events: Pick<EventTemplate, "tags">[]) => Tags.wrap(events.flatMap(e => e.tags || []))
|
||||||
|
|
||||||
|
static fromIMeta = (imeta: string[]) => Tags.wrap(imeta.map((m: string) => m.split(" ")))
|
||||||
|
|
||||||
unwrap = () => this.xs.map(tag => tag.valueOf())
|
unwrap = () => this.xs.map(tag => tag.valueOf())
|
||||||
|
|
||||||
whereKey = (key: string) => this.filter(t => t.key() === key)
|
whereKey = (key: string) => this.filter(t => t.key() === key)
|
||||||
@@ -59,17 +61,24 @@ export class Tags extends (Fluent<Tag> as OmitStatics<typeof Fluent<Tag>, 'from'
|
|||||||
|
|
||||||
whereMark = (mark: string) => this.filter(t => t.mark() === mark)
|
whereMark = (mark: string) => this.filter(t => t.mark() === mark)
|
||||||
|
|
||||||
removeKey = (key: string) => this.reject(t => t.key() === key)
|
filterByKey = (keys: string[]) => this.filter(t => keys.includes(t.key()))
|
||||||
|
|
||||||
removeValue = (value: string) => this.reject(t => t.value() === value)
|
filterByValue = (values: string[]) => this.filter(t => values.includes(t.value()))
|
||||||
|
|
||||||
removeMark = (mark: string) => this.reject(t => t.mark() === mark)
|
filterByMark = (marks: string[]) => this.filter(t => marks.includes(t.mark()))
|
||||||
|
|
||||||
|
rejectByKey = (keys: string[]) => this.reject(t => keys.includes(t.key()))
|
||||||
|
|
||||||
|
rejectByValue = (values: string[]) => this.reject(t => values.includes(t.value()))
|
||||||
|
|
||||||
|
rejectByMark = (marks: string[]) => this.reject(t => marks.includes(t.mark()))
|
||||||
|
|
||||||
get = (key: string) => this.whereKey(key).first()
|
get = (key: string) => this.whereKey(key).first()
|
||||||
|
|
||||||
keys = () => this.mapTo(t => t.key())
|
keys = () => this.mapTo(t => t.key())
|
||||||
|
|
||||||
values = (key?: string) => (key ? this.whereKey(key) : this).mapTo(t => t.value())
|
values = (key?: string | string[]) =>
|
||||||
|
(key ? this.filterByKey(ensurePlural(key)) : this).mapTo(t => t.value())
|
||||||
|
|
||||||
marks = () => this.mapTo(t => t.mark())
|
marks = () => this.mapTo(t => t.mark())
|
||||||
|
|
||||||
@@ -80,8 +89,8 @@ export class Tags extends (Fluent<Tag> as OmitStatics<typeof Fluent<Tag>, 'from'
|
|||||||
topics = () => this.whereKey("t").values().map((t: string) => t.replace(/^#/, ""))
|
topics = () => this.whereKey("t").values().map((t: string) => t.replace(/^#/, ""))
|
||||||
|
|
||||||
ancestors = (x?: boolean) => {
|
ancestors = (x?: boolean) => {
|
||||||
const tags = this.filter(t => ["a", "e", "q"].includes(t.key()) && !t.isContext())
|
const tags = this.filterByKey(["a", "e", "q"]).reject(t => t.isContext())
|
||||||
const parentTags = tags.filter(t => ["a", "e"].includes(t.key()))
|
const parentTags = tags.filterByKey(["a", "e"])
|
||||||
const mentionTags = tags.whereKey("q")
|
const mentionTags = tags.whereKey("q")
|
||||||
const roots: string[][] = []
|
const roots: string[][] = []
|
||||||
const replies: string[][] = []
|
const replies: string[][] = []
|
||||||
@@ -162,7 +171,7 @@ export class Tags extends (Fluent<Tag> as OmitStatics<typeof Fluent<Tag>, 'from'
|
|||||||
|
|
||||||
imeta = (url: string) => {
|
imeta = (url: string) => {
|
||||||
for (const tag of this.whereKey("imeta").xs) {
|
for (const tag of this.whereKey("imeta").xs) {
|
||||||
const tags = Tags.wrap(tag.drop(1).valueOf().map((m: string) => m.split(" ")))
|
const tags = Tags.fromIMeta(tag.drop(1).valueOf())
|
||||||
|
|
||||||
if (tags.get("url")?.value() === url) {
|
if (tags.get("url")?.value() === url) {
|
||||||
return tags
|
return tags
|
||||||
@@ -176,7 +185,7 @@ export class Tags extends (Fluent<Tag> as OmitStatics<typeof Fluent<Tag>, 'from'
|
|||||||
|
|
||||||
addTag = (...args: string[]) => this.append(Tag.from(args))
|
addTag = (...args: string[]) => this.append(Tag.from(args))
|
||||||
|
|
||||||
setTag = (k: string, ...args: string[]) => this.removeKey(k).addTag(k, ...args)
|
setTag = (k: string, ...args: string[]) => this.rejectByKey([k]).addTag(k, ...args)
|
||||||
|
|
||||||
// Context
|
// Context
|
||||||
|
|
||||||
@@ -191,7 +200,7 @@ export class Tags extends (Fluent<Tag> as OmitStatics<typeof Fluent<Tag>, 'from'
|
|||||||
addImages = (imeta: Tags[]) =>
|
addImages = (imeta: Tags[]) =>
|
||||||
this.concat(imeta.map(tags => Tag.from(["image", tags.get("url").value()])))
|
this.concat(imeta.map(tags => Tag.from(["image", tags.get("url").value()])))
|
||||||
|
|
||||||
removeImages = () => this.removeKey('image')
|
removeImages = () => this.rejectByKey(['image'])
|
||||||
|
|
||||||
setImages = (imeta: Tags[]) => this.removeImages().addImages(imeta)
|
setImages = (imeta: Tags[]) => this.removeImages().addImages(imeta)
|
||||||
|
|
||||||
@@ -200,7 +209,7 @@ export class Tags extends (Fluent<Tag> as OmitStatics<typeof Fluent<Tag>, 'from'
|
|||||||
addIMeta = (imeta: Tags[]) =>
|
addIMeta = (imeta: Tags[]) =>
|
||||||
this.concat(imeta.map(tags => Tag.from(["imeta", ...tags.valueOf().map(xs => xs.join(" "))])))
|
this.concat(imeta.map(tags => Tag.from(["imeta", ...tags.valueOf().map(xs => xs.join(" "))])))
|
||||||
|
|
||||||
removeIMeta = () => this.removeKey('imeta')
|
removeIMeta = () => this.rejectByKey(['imeta'])
|
||||||
|
|
||||||
setIMeta = (imeta: Tags[]) => this.removeIMeta().addIMeta(imeta)
|
setIMeta = (imeta: Tags[]) => this.removeIMeta().addIMeta(imeta)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@coracle.social/util",
|
"name": "@coracle.social/util",
|
||||||
"version": "0.0.6",
|
"version": "0.0.7",
|
||||||
"author": "hodlbod",
|
"author": "hodlbod",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"description": "A collection of utilities.",
|
"description": "A collection of utilities.",
|
||||||
|
|||||||
Reference in New Issue
Block a user