reformat w/ prettier

feat/vaults
Tomáš Mládek 2021-10-29 17:24:24 +02:00
parent 30189b42a5
commit ea1b05d491
1 changed files with 125 additions and 104 deletions

View File

@ -3,122 +3,143 @@ import { fetcher } from "@/utils";
import useSWRV from "swrv";
import { computed, ComputedRef, Ref } from "vue";
export function useEntity(
address: string | (() => string),
condition?: () => Boolean
) {
const { data, error, mutate } = useSWRV<ListingResult, unknown>(
() =>
condition === undefined || condition()
? `/api/obj/${typeof address === "string" ? address : address()}`
: null,
fetcher,
{ revalidateOnFocus: false }
);
export function useEntity(address: string | (() => string), condition?: () => Boolean) {
const { data, error, mutate } = useSWRV<ListingResult, unknown>(
() => (condition === undefined || condition()) ? `/api/obj/${typeof address === "string" ? address : address()}` : null,
fetcher, { revalidateOnFocus: false }
);
const entries = computed(() => {
if (data?.value) {
const entries = Object.entries(data.value) as [string, IEntry][];
return entries
.sort(([_, a], [__, b]) => String(a.value.c).localeCompare(b.value.c))
.sort(([_, a], [__, b]) => String(a.value.t).localeCompare(b.value.t))
.sort(([_, a], [__, b]) => a.attribute.localeCompare(b.attribute));
} else {
return [];
}
});
const attributes = computed(() => {
const addr = typeof address === "string" ? address : address();
return entries.value.filter(([_, e]) => e.entity === addr);
});
const backlinks = computed(() => {
const addr = typeof address === "string" ? address : address();
return entries.value.filter(([_, e]) => e.entity !== addr);
});
return {
entries,
attributes,
backlinks,
data,
error,
mutate
const entries = computed(() => {
if (data?.value) {
const entries = Object.entries(data.value) as [string, IEntry][];
return entries
.sort(([_, a], [__, b]) => String(a.value.c).localeCompare(b.value.c))
.sort(([_, a], [__, b]) => String(a.value.t).localeCompare(b.value.t))
.sort(([_, a], [__, b]) => a.attribute.localeCompare(b.attribute));
} else {
return [];
}
});
const attributes = computed(() => {
const addr = typeof address === "string" ? address : address();
return entries.value.filter(([_, e]) => e.entity === addr);
});
const backlinks = computed(() => {
const addr = typeof address === "string" ? address : address();
return entries.value.filter(([_, e]) => e.entity !== addr);
});
return {
entries,
attributes,
backlinks,
data,
error,
mutate
};
}
export function query(
query: string | (() => string),
condition?: () => Boolean
) {
const { data, error, mutate } = useSWRV<ListingResult, unknown>(
() =>
condition === undefined || condition()
? `/api/obj?query=${typeof query === "string" ? query : query()}`
: null,
fetcher,
{ revalidateOnFocus: false }
);
export function query(query: string | (() => string), condition?: () => Boolean) {
const { data, error, mutate } = useSWRV<ListingResult, unknown>(
() => (condition === undefined || condition()) ? `/api/obj?query=${typeof query === "string" ? query : query()}` : null,
fetcher, { revalidateOnFocus: false }
);
const result = computed(() => {
if (data?.value) {
const entries = Object.entries(data.value) as [string, IEntry][];
return entries
.sort(([_, a], [__, b]) => String(a.value.c).localeCompare(b.value.c))
.sort(([_, a], [__, b]) => String(a.value.t).localeCompare(b.value.t))
.sort(([_, a], [__, b]) => a.attribute.localeCompare(b.attribute));
} else {
return [];
}
});
return {
result,
data,
error,
mutate
const result = computed(() => {
if (data?.value) {
const entries = Object.entries(data.value) as [string, IEntry][];
return entries
.sort(([_, a], [__, b]) => String(a.value.c).localeCompare(b.value.c))
.sort(([_, a], [__, b]) => String(a.value.t).localeCompare(b.value.t))
.sort(([_, a], [__, b]) => a.attribute.localeCompare(b.attribute));
} else {
return [];
}
});
return {
result,
data,
error,
mutate
};
}
interface EntityIdentification {
type: string;
value: string;
type: string;
value: string;
}
export function identify(attributes: ComputedRef<[string, IEntry][]>): ComputedRef<EntityIdentification[]> {
// Get all identities of the object
const isEntries = computed(() => {
export function identify(
attributes: ComputedRef<[string, IEntry][]>
): ComputedRef<EntityIdentification[]> {
// Get all identities of the object
const isEntries = computed(() => {
return attributes.value
.filter(([_, entry]) => entry.attribute === "IS")
.map(([_, entry]) => entry.value.c);
});
// Out of those, retrieve their TYPE_ID entries
const { data: typeIdListing } = query(() => {
return (
isEntries.value &&
`(matches (in ${isEntries.value
.map(e => `"${e}"`)
.join(" ")}) "TYPE_ID" ?)`
);
});
const typeIdAttributes: ComputedRef<[string, string][]> = computed(() => {
return Object.values(typeIdListing.value || {}).map(entry => {
return [entry.entity, entry.value.c];
});
});
// Finally, filter own object's attributes according to TYPE_IDs
return computed(() => {
// For each identity/TYPE_ID pair
return typeIdAttributes.value
.map(([type, attrName]) => {
// And each associated TYPE_ID attribute...
// return own matchin attributes
return attributes.value
.filter(([_, entry]) => entry.attribute === "IS")
.map(([_, entry]) => entry.value.c);
})
// Out of those, retrieve their TYPE_ID entries
const { data: typeIdListing } = query(() => {
return isEntries.value && `(matches (in ${isEntries.value.map((e) => `"${e}"`).join(" ")}) "TYPE_ID" ?)`;
});
const typeIdAttributes: ComputedRef<[string, string][]> = computed(() => {
return Object.values(typeIdListing.value || {}).map((entry) => {
return [entry.entity, entry.value.c];
});
});
// Finally, filter own object's attributes according to TYPE_IDs
return computed(() => {
// For each identity/TYPE_ID pair
return typeIdAttributes.value
.map(([type, attrName]) => {
// And each associated TYPE_ID attribute...
// return own matchin attributes
return attributes.value
.filter(([_, e]) => e.attribute === attrName)
.map(([_, attr]) => {
return {
type,
value: attr.value.c
}
})
}).flat();
});
.filter(([_, e]) => e.attribute === attrName)
.map(([_, attr]) => {
return {
type,
value: attr.value.c
};
});
})
.flat();
});
}
export function asDict(attributes: [string, IEntry][]): { [key: string]: string } {
const result = {} as { [key: string]: string };
attributes.map(([_, attribute]) => attribute).forEach((attribute) => {
result[attribute.attribute] = attribute.value.c;
export function asDict(
attributes: [string, IEntry][]
): { [key: string]: string } {
const result = {} as { [key: string]: string };
attributes
.map(([_, attribute]) => attribute)
.forEach(attribute => {
result[attribute.attribute] = attribute.value.c;
});
return result;
}
return result;
}