2022-01-28 20:51:34 +01:00
|
|
|
import type { IEntry, IValue, ListingResult } from "./types";
|
2023-09-30 10:35:47 +02:00
|
|
|
export { UpEndApi } from "./api";
|
2023-10-09 21:53:44 +02:00
|
|
|
export { Query } from "./query";
|
2021-12-19 13:54:16 +01:00
|
|
|
|
2021-12-18 15:12:22 +01:00
|
|
|
export class UpListing {
|
2021-12-19 13:54:16 +01:00
|
|
|
public readonly entries: UpEntry[];
|
2021-12-19 19:20:09 +01:00
|
|
|
private _objects: { [key: string]: UpObject } = {};
|
2021-12-18 15:12:22 +01:00
|
|
|
|
|
|
|
constructor(listing: ListingResult) {
|
2021-12-19 19:20:09 +01:00
|
|
|
this.entries = Object.entries(listing).map(
|
|
|
|
(lr) => new UpEntry(...lr, this)
|
|
|
|
);
|
2021-12-18 15:12:22 +01:00
|
|
|
}
|
|
|
|
|
2021-12-19 19:20:09 +01:00
|
|
|
public get objects() {
|
2021-12-18 15:12:22 +01:00
|
|
|
const allEntities = new Set(this.entries.map((e) => e.entity));
|
2021-12-19 19:20:09 +01:00
|
|
|
const result: { [key: string]: UpObject } = {};
|
|
|
|
Array.from(allEntities).forEach(
|
|
|
|
(entity) => (result[entity] = new UpObject(entity, this))
|
2021-12-19 11:46:40 +01:00
|
|
|
);
|
2021-12-19 19:20:09 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public getObject(address: string) {
|
|
|
|
if (!this._objects[address]) {
|
|
|
|
this._objects[address] = new UpObject(address, this);
|
|
|
|
}
|
|
|
|
return this._objects[address];
|
2021-12-18 15:12:22 +01:00
|
|
|
}
|
2023-10-09 22:01:49 +02:00
|
|
|
|
|
|
|
public get entities(): string[] {
|
|
|
|
return Array.from(new Set(this.entries.map((e) => `@${e.entity}`)));
|
|
|
|
}
|
|
|
|
|
|
|
|
public get attributes(): string[] {
|
|
|
|
return Array.from(new Set(this.entries.map((e) => e.attribute)));
|
|
|
|
}
|
|
|
|
|
|
|
|
public get values(): IValue[] {
|
|
|
|
return Array.from(new Set(this.entries.map((e) => e.value)));
|
|
|
|
}
|
2021-12-18 15:12:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export class UpObject {
|
2023-08-25 23:35:29 +02:00
|
|
|
public readonly address: string;
|
2021-12-19 19:20:09 +01:00
|
|
|
public listing: UpListing | undefined;
|
2021-12-18 15:12:22 +01:00
|
|
|
|
2021-12-19 19:20:09 +01:00
|
|
|
constructor(address: string, listing?: UpListing) {
|
2021-12-18 15:12:22 +01:00
|
|
|
this.address = address;
|
2021-12-19 19:20:09 +01:00
|
|
|
this.listing = listing;
|
2021-12-19 11:46:40 +01:00
|
|
|
}
|
|
|
|
|
2021-12-19 19:20:09 +01:00
|
|
|
public bind(listing: UpListing) {
|
|
|
|
this.listing = listing;
|
2021-12-19 13:54:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public get attributes() {
|
2021-12-19 19:20:09 +01:00
|
|
|
return (this.listing?.entries || []).filter(
|
|
|
|
(e) => e.entity === this.address
|
|
|
|
);
|
2021-12-19 13:54:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public get backlinks() {
|
2021-12-19 19:20:09 +01:00
|
|
|
return (this.listing?.entries || []).filter(
|
|
|
|
(e) => e.value.c === this.address
|
|
|
|
);
|
2021-12-19 13:54:16 +01:00
|
|
|
}
|
|
|
|
|
2021-12-19 11:46:40 +01:00
|
|
|
public get attr() {
|
2021-12-18 15:12:22 +01:00
|
|
|
const result = {} as { [key: string]: UpEntry[] };
|
2021-12-19 13:54:16 +01:00
|
|
|
this.attributes.forEach((entry) => {
|
|
|
|
if (!result[entry.attribute]) {
|
|
|
|
result[entry.attribute] = [];
|
|
|
|
}
|
2021-12-18 15:12:22 +01:00
|
|
|
|
2021-12-19 13:54:16 +01:00
|
|
|
result[entry.attribute].push(entry);
|
|
|
|
});
|
2021-12-18 15:12:22 +01:00
|
|
|
|
2023-06-15 22:35:49 +02:00
|
|
|
this.backlinks.forEach((entry) => {
|
|
|
|
const attribute = `~${entry.attribute}`;
|
|
|
|
if (!result[attribute]) {
|
|
|
|
result[attribute] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
result[attribute].push(entry);
|
|
|
|
});
|
|
|
|
|
2021-12-18 15:12:22 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-12-19 11:46:40 +01:00
|
|
|
public get(attr: string) {
|
|
|
|
return this.attr[attr] ? this.attr[attr][0].value.c : undefined;
|
|
|
|
}
|
|
|
|
|
2021-12-19 13:54:16 +01:00
|
|
|
public identify(): string[] {
|
|
|
|
const lblValues = (this.attr["LBL"] || []).map((e) => String(e.value.c));
|
2022-02-06 13:26:35 +01:00
|
|
|
return lblValues;
|
2021-12-19 13:54:16 +01:00
|
|
|
}
|
|
|
|
|
2021-12-19 11:46:40 +01:00
|
|
|
public asDict() {
|
2021-12-18 15:12:22 +01:00
|
|
|
return {
|
|
|
|
address: this.address,
|
2021-12-19 11:46:40 +01:00
|
|
|
attributes: this.attr,
|
2021-12-18 15:12:22 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class UpEntry extends UpObject implements IEntry {
|
|
|
|
entity: string;
|
|
|
|
attribute: string;
|
2022-01-09 21:24:49 +01:00
|
|
|
value: IValue;
|
2023-04-23 17:35:09 +02:00
|
|
|
provenance: string;
|
|
|
|
timestamp: string;
|
2021-12-18 15:12:22 +01:00
|
|
|
|
2021-12-19 19:20:09 +01:00
|
|
|
constructor(address: string, entry: IEntry, listing: UpListing) {
|
|
|
|
super(address, listing);
|
2021-12-18 15:12:22 +01:00
|
|
|
|
|
|
|
this.entity = entry.entity;
|
|
|
|
this.attribute = entry.attribute;
|
|
|
|
this.value = entry.value;
|
2023-04-23 17:35:09 +02:00
|
|
|
this.provenance = entry.provenance;
|
|
|
|
this.timestamp = entry.timestamp;
|
2021-12-18 15:12:22 +01:00
|
|
|
}
|
2021-12-21 14:32:36 +01:00
|
|
|
|
|
|
|
public toString(): string {
|
|
|
|
return `(${this.entity} ${this.attribute} ${this.value.c} [${this.value.t}])`;
|
|
|
|
}
|
2021-11-11 23:37:42 +01:00
|
|
|
}
|