diff --git a/frontend/src/model/Entity.ts b/frontend/src/model/Entity.ts index 02db00b4..cf5ac102 100644 --- a/frontend/src/model/Entity.ts +++ b/frontend/src/model/Entity.ts @@ -69,7 +69,11 @@ export default abstract class Entity extends Thing { } getExactSynonyms(): Reified[] { - return Reified.fromJson(this.extractExactMatchSynonyms()) || []; + const exactSynonymTypes = [ + "http://www.geneontology.org/formats/oboInOwl#hasExactSynonym", + "http://www.geneontology.org/formats/oboInOwl#hasSynonym", + ]; + return Reified.fromJson(this.extractSynonyms(exactSynonymTypes)) || []; } getRelatedSynonyms(): Reified[] { @@ -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; } } diff --git a/frontend/src/pages/ontologies/entities/entityPageSections/EntitySynonymsSection.tsx b/frontend/src/pages/ontologies/entities/entityPageSections/EntitySynonymsSection.tsx index 9b1ca784..7d56cdf9 100644 --- a/frontend/src/pages/ontologies/entities/entityPageSections/EntitySynonymsSection.tsx +++ b/frontend/src/pages/ontologies/entities/entityPageSections/EntitySynonymsSection.tsx @@ -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 ; - } + if (synonymsData.every(({synonyms}) => !synonyms || synonyms.length === 0)) { + return ; + } - return ( -
- {exactSynonyms && exactSynonyms.length > 0 && ( -
-
Exact Synonyms
- {exactSynonyms - .map((synonym: Reified) => { - const hasMetadata = synonym.hasMetadata(); - return ( + return ( +
+ {synonymsData.map( + ({label, synonyms}) => + synonyms && + synonyms.length > 0 && (
- {synonym.value} - {hasMetadata && ( - - )} +
{label}
+ {synonyms + .map((synonym: Reified) => { + const hasMetadata = synonym.hasMetadata(); + return ( +
+ {synonym.value} + {hasMetadata && ( + + )} +
+ ); + }) + .sort((a, b) => sortByKeys(a, b))}
- ); - }) - .sort((a, b) => sortByKeys(a, b))} -
- )} - - {relatedSynonyms && relatedSynonyms.length > 0 && ( -
-
Related Synonyms
- {relatedSynonyms - .map((synonym: Reified) => { - const hasMetadata = synonym.hasMetadata(); - return ( -
- {synonym.value} - {hasMetadata && ( - - )} -
- ); - }) - .sort((a, b) => sortByKeys(a, b))} -
- )} - - {narrowSynonyms && narrowSynonyms.length > 0 && ( -
-
Narrow Synonyms
- {narrowSynonyms - .map((synonym: Reified) => { - const hasMetadata = synonym.hasMetadata(); - return ( -
- {synonym.value} - {hasMetadata && ( - - )} -
- ); - }) - .sort((a, b) => sortByKeys(a, b))} -
- )} - - {broadSynonyms && broadSynonyms.length > 0 && ( -
-
Broad Synonyms
- {broadSynonyms - .map((synonym: Reified) => { - const hasMetadata = synonym.hasMetadata(); - return ( -
- {synonym.value} - {hasMetadata && ( - - )} -
- ); - }) - .sort((a, b) => sortByKeys(a, b))} -
- )} -
- ); + ) + )} +
+ ); } \ No newline at end of file