fix(jslib): correct types for `UpObject.attr()`

fix/notes-editor
Tomáš Mládek 2024-01-28 16:32:29 +01:00
parent de9f808b7a
commit 6467d6c3b7
5 changed files with 12 additions and 12 deletions

View File

@ -67,9 +67,9 @@ export class UpObject {
);
}
private _attr: { [key: string]: UpEntry[] } | undefined;
private _attr: Record<string, UpEntry[] | undefined> | undefined;
public get attr() {
public get attr(): Record<string, UpEntry[] | undefined> {
if (!this._attr) {
const result = {} as { [key: string]: UpEntry[] };
this.attributes.forEach((entry) => {
@ -94,8 +94,8 @@ export class UpObject {
return this._attr;
}
public get(attr: string) {
return this.attr[attr] ? this.attr[attr][0].value.c : undefined;
public get(attr: string): string | number | null | undefined {
return this.attr[attr]?.[0].value.c;
}
public identify(): string[] {

View File

@ -1,6 +1,6 @@
{
"name": "@upnd/upend",
"version": "0.3.2",
"version": "0.3.3",
"description": "Client library to interact with the UpEnd system.",
"scripts": {
"build": "tsc --build --verbose",

View File

@ -237,7 +237,7 @@
});
break;
case 'entry-delete': {
const inEntry = $entity?.attr[`~${ATTR_IN}`].find((e) => e.entity === change.address);
const inEntry = $entity?.attr[`~${ATTR_IN}`]?.find((e) => e.entity === change.address);
if (inEntry) {
await api.deleteEntry(inEntry.address);
} else {

View File

@ -26,7 +26,7 @@
.map((v) => v.c as string)
.filter((groupAddr) => {
return Object.values(allGroups.objects).every((obj) => {
return obj.attr[ATTR_IN].some((v) => v.value.c === groupAddr);
return obj.attr[ATTR_IN]?.some((v) => v.value.c === groupAddr);
});
})
);
@ -55,7 +55,7 @@
async function removeGroup(address: string) {
await Promise.all(
entities.map((entity) => {
const group = groupListing?.objects[entity].attr[ATTR_IN].find(
const group = groupListing?.objects[entity].attr[ATTR_IN]?.find(
(v) => v.value.c === address
);
if (group) {

View File

@ -57,7 +57,7 @@
if (annotation.get('W3C_FRAGMENT_SELECTOR')) {
anno.addAnnotation({
type: 'Annotation',
body: annotation.attr[ATTR_LABEL].map((e) => {
body: (annotation.attr[ATTR_LABEL] || []).map((e) => {
return {
type: 'TextualBody',
value: String(e.value.c),
@ -145,9 +145,9 @@
anno.on('updateAnnotation', async (annotation) => {
const annotationObject = await api.fetchEntity(annotation.id);
await Promise.all(
annotationObject.attr[ATTR_LABEL].concat(
annotationObject.attr['W3C_FRAGMENT_SELECTOR']
).map(async (e) => api.deleteEntry(e.address))
(annotationObject.attr[ATTR_LABEL] || [])
.concat(annotationObject.attr['W3C_FRAGMENT_SELECTOR'] || [])
.map(async (e) => api.deleteEntry(e.address))
);
await api.putEntry([
{