Skip to content

Commit

Permalink
Simplify code logic
Browse files Browse the repository at this point in the history
  • Loading branch information
haideriqbal committed Nov 11, 2024
1 parent 3d7c3eb commit 7d22f87
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 142 deletions.
62 changes: 30 additions & 32 deletions frontend/src/model/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ export default abstract class Entity extends Thing {
}

getExactSynonyms(): Reified<any>[] {
return Reified.fromJson<any>(this.extractExactMatchSynonyms()) || [];
const exactSynonymTypes = [
"http://www.geneontology.org/formats/oboInOwl#hasExactSynonym",
"http://www.geneontology.org/formats/oboInOwl#hasSynonym",
];
return Reified.fromJson<any>(this.extractSynonyms(exactSynonymTypes)) || [];
}

getRelatedSynonyms(): Reified<any>[] {
Expand Down Expand Up @@ -206,38 +210,32 @@ export default abstract class Entity extends Thing {
}
}


extractExactMatchSynonyms(): string[] {
let result = []

if(this.properties["synonymProperty"]) {
let synonymProperties = this.properties["synonymProperty"];
if (!Array.isArray(synonymProperties)) {
synonymProperties = [synonymProperties];
}
synonymProperties.map((synonymProperty: string) => {
if (synonymProperty === "http://www.geneontology.org/formats/oboInOwl#hasExactSynonym" ||
synonymProperty === "http://www.geneontology.org/formats/oboInOwl#hasSynonym") {
result = result.concat(this.properties[synonymProperty]) || [];
}
})
extractSynonyms(synonymTypes: string | string[]): string[] {
const result : string[] = [];
// Check if 'synonymProperty' exists in 'this.properties'
if (!this.properties || !this.properties.hasOwnProperty("synonymProperty")) {
return result; // Return empty array if 'synonymProperty' doesn't exist
}
return result || [];
}

extractSynonyms(synonymType: string): string[] {
let result = []
if(this.properties["synonymProperty"]) {
let synonymProperties = this.properties["synonymProperty"];
if (!Array.isArray(synonymProperties)) {
synonymProperties = [synonymProperties];
}
synonymProperties.map((synonymProperty: string) => {
if (synonymProperty === synonymType) {
result = result.concat(this.properties[synonymProperty]) || [];
const synonymProperties = this.properties["synonymProperty"];
const synonymPropsArray = Array.isArray(synonymProperties)
? synonymProperties
: [synonymProperties];

const synonymTypesArray = Array.isArray(synonymTypes)
? synonymTypes
: [synonymTypes];

synonymPropsArray.forEach((synonymProperty: string) => {
if (synonymTypesArray.includes(synonymProperty)) {
if (this.properties.hasOwnProperty(synonymProperty)) {
const synonyms = this.properties[synonymProperty];
if (synonyms) {
result.push(...(Array.isArray(synonyms) ? synonyms : [synonyms]));
}
}
})
}
return result || [];
}
});

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,119 +12,52 @@ export default function EntitySynonymsSection({
entity: Entity;
linkedEntities: LinkedEntities;
}) {
let exactSynonyms = entity.getExactSynonyms();
let relatedSynonyms = entity.getRelatedSynonyms();
let narrowSynonyms = entity.getNarrowSynonyms();
let broadSynonyms = entity.getBroadSynonyms();
const synonymsData = [
{label: "Exact Synonyms", synonyms: entity.getExactSynonyms()},
{label: "Related Synonyms", synonyms: entity.getRelatedSynonyms()},
{label: "Narrow Synonyms", synonyms: entity.getNarrowSynonyms()},
{label: "Broad Synonyms", synonyms: entity.getBroadSynonyms()},
];

if ((!exactSynonyms || exactSynonyms.length === 0) &&
(!relatedSynonyms || relatedSynonyms.length === 0) &&
(!narrowSynonyms || narrowSynonyms.length === 0) &&
(!broadSynonyms || broadSynonyms.length === 0)) {
return <Fragment />;
}
if (synonymsData.every(({synonyms}) => !synonyms || synonyms.length === 0)) {
return <Fragment/>;
}

return (
<div className="flex flex-col mb-2">
{exactSynonyms && exactSynonyms.length > 0 && (
<div className="flex flex-row flex-wrap items-center mb-2">
<div className="font-bold mr-2">Exact Synonyms</div>
{exactSynonyms
.map((synonym: Reified<any>) => {
const hasMetadata = synonym.hasMetadata();
return (
return (
<div className="flex flex-col mb-2">
{synonymsData.map(
({label, synonyms}) =>
synonyms &&
synonyms.length > 0 && (
<div
key={synonym.value.toString().toUpperCase() + randomString()}
className="bg-grey-default rounded-sm font-mono py-1 px-3 mr-2 my-1 text-sm"
key={label}
className="flex flex-row flex-wrap items-center mb-2"
>
{synonym.value}
{hasMetadata && (
<MetadataTooltip
metadata={synonym.getMetadata()}
linkedEntities={linkedEntities}
/>
)}
<div className="font-bold mr-2">{label}</div>
{synonyms
.map((synonym: Reified<any>) => {
const hasMetadata = synonym.hasMetadata();
return (
<div
key={
synonym.value.toString().toUpperCase() + randomString()
}
className="bg-grey-default rounded-sm font-mono py-1 px-3 mr-2 my-1 text-sm"
>
{synonym.value}
{hasMetadata && (
<MetadataTooltip
metadata={synonym.getMetadata()}
linkedEntities={linkedEntities}
/>
)}
</div>
);
})
.sort((a, b) => sortByKeys(a, b))}
</div>
);
})
.sort((a, b) => sortByKeys(a, b))}
</div>
)}

{relatedSynonyms && relatedSynonyms.length > 0 && (
<div className="flex flex-row flex-wrap items-center mb-2">
<div className="font-bold mr-2">Related Synonyms</div>
{relatedSynonyms
.map((synonym: Reified<any>) => {
const hasMetadata = synonym.hasMetadata();
return (
<div
key={synonym.value.toString().toUpperCase() + randomString()}
className="bg-grey-default rounded-sm font-mono py-1 px-3 mr-2 my-1 text-sm"
>
{synonym.value}
{hasMetadata && (
<MetadataTooltip
metadata={synonym.getMetadata()}
linkedEntities={linkedEntities}
/>
)}
</div>
);
})
.sort((a, b) => sortByKeys(a, b))}
</div>
)}

{narrowSynonyms && narrowSynonyms.length > 0 && (
<div className="flex flex-row flex-wrap items-center mb-2">
<div className="font-bold mr-2">Narrow Synonyms</div>
{narrowSynonyms
.map((synonym: Reified<any>) => {
const hasMetadata = synonym.hasMetadata();
return (
<div
key={synonym.value.toString().toUpperCase() + randomString()}
className="bg-grey-default rounded-sm font-mono py-1 px-3 mr-2 my-1 text-sm"
>
{synonym.value}
{hasMetadata && (
<MetadataTooltip
metadata={synonym.getMetadata()}
linkedEntities={linkedEntities}
/>
)}
</div>
);
})
.sort((a, b) => sortByKeys(a, b))}
</div>
)}

{broadSynonyms && broadSynonyms.length > 0 && (
<div className="flex flex-row flex-wrap items-center mb-2">
<div className="font-bold mr-2">Broad Synonyms</div>
{broadSynonyms
.map((synonym: Reified<any>) => {
const hasMetadata = synonym.hasMetadata();
return (
<div
key={synonym.value.toString().toUpperCase() + randomString()}
className="bg-grey-default rounded-sm font-mono py-1 px-3 mr-2 my-1 text-sm"
>
{synonym.value}
{hasMetadata && (
<MetadataTooltip
metadata={synonym.getMetadata()}
linkedEntities={linkedEntities}
/>
)}
</div>
);
})
.sort((a, b) => sortByKeys(a, b))}
</div>
)}
</div>
);
)
)}
</div>
);
}

0 comments on commit 7d22f87

Please sign in to comment.