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

View File

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

View File

@ -237,7 +237,7 @@
}); });
break; break;
case 'entry-delete': { 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) { if (inEntry) {
await api.deleteEntry(inEntry.address); await api.deleteEntry(inEntry.address);
} else { } else {

View File

@ -26,7 +26,7 @@
.map((v) => v.c as string) .map((v) => v.c as string)
.filter((groupAddr) => { .filter((groupAddr) => {
return Object.values(allGroups.objects).every((obj) => { 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) { async function removeGroup(address: string) {
await Promise.all( await Promise.all(
entities.map((entity) => { 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 (v) => v.value.c === address
); );
if (group) { if (group) {

View File

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