upend/tools/upend_js/index.ts

120 lines
3.0 KiB
TypeScript
Raw Normal View History

import type { IEntry, IValue, ListingResult, VALUE_TYPE } from "./types";
// export function listingAsOrdered(listing: ListingResult): OrderedListing {
// const entries = Object.entries(listing) as [Address, IEntry][];
// return entries
// .sort(([_, a], [__, b]) =>
// String(a.value.c).localeCompare(String(b.value.c))
// )
// .sort(([_, a], [__, b]) =>
// String(a.value.t).localeCompare(String(b.value.t))
// )
// .sort(([_, a], [__, b]) => a.attribute.localeCompare(b.attribute));
// }
2021-11-11 23:37:42 +01:00
2021-12-18 15:12:22 +01:00
export class UpListing {
public readonly entries: UpEntry[];
private _objects: { [key: string]: UpObject } = {};
2021-12-18 15:12:22 +01:00
constructor(listing: ListingResult) {
this.entries = Object.entries(listing).map(
(lr) => new UpEntry(...lr, this)
);
2021-12-18 15:12:22 +01:00
}
public get objects() {
2021-12-18 15:12:22 +01:00
const allEntities = new Set(this.entries.map((e) => e.entity));
const result: { [key: string]: UpObject } = {};
Array.from(allEntities).forEach(
(entity) => (result[entity] = new UpObject(entity, this))
2021-12-19 11:46:40 +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
}
}
export class UpObject {
public readonly address;
public listing: UpListing | undefined;
2021-12-18 15:12:22 +01:00
constructor(address: string, listing?: UpListing) {
2021-12-18 15:12:22 +01:00
this.address = address;
this.listing = listing;
2021-12-19 11:46:40 +01:00
}
public bind(listing: UpListing) {
this.listing = listing;
}
public get attributes() {
return (this.listing?.entries || []).filter(
(e) => e.entity === this.address
);
}
public get backlinks() {
return (this.listing?.entries || []).filter(
(e) => e.value.c === this.address
);
}
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[] };
this.attributes.forEach((entry) => {
if (!result[entry.attribute]) {
result[entry.attribute] = [];
}
2021-12-18 15:12:22 +01:00
result[entry.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;
}
public identify(): string[] {
const hasAliases = this.backlinks
.filter((entry) => entry.attribute === "HAS")
.map((entry) => entry.get("ALIAS"))
.filter(Boolean) as string[];
const lblValues = (this.attr["LBL"] || []).map((e) => String(e.value.c));
return lblValues.concat(hasAliases);
}
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;
value: IValue;
2021-12-18 15:12:22 +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;
}
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
}