Skip to content

Commit

Permalink
Added normalization code to multiple wrappers.
Browse files Browse the repository at this point in the history
  • Loading branch information
gaurav committed Apr 24, 2024
1 parent 0f9af87 commit 12185da
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/wrappers/PhylorefWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class PhylorefWrapper {

normalizedPhyloref.internalSpecifiers = (phyloref.internalSpecifiers || [])
.map(TaxonomicUnitWrapper.normalize);
normalizedPhyloref.externalSpecifiers = (phyloref.internalSpecifiers || [])
normalizedPhyloref.externalSpecifiers = (phyloref.externalSpecifiers || [])
.map(TaxonomicUnitWrapper.normalize);

return normalizedPhyloref;
Expand Down
19 changes: 19 additions & 0 deletions src/wrappers/SpecimenWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@ class SpecimenWrapper {
this.specimen = specimen;
}

/**
* Normalize the specified specimen.
* @param specimen A specimen to be normalized.
*/
static normalize(specimen) {
const wrapped = new SpecimenWrapper(specimen);
const normalizedSpecimen = {
'@type': SpecimenWrapper.TYPE_SPECIMEN,
label: wrapped.label,
'dwc:basisOfRecord': wrapped.basisOfRecord,
occurrenceID: wrapped.occurrenceID,
catalogNumber: wrapped.catalogNumber,
institutionCode: wrapped.institutionCode,
collectionCode: wrapped.collectionCode,
};
if ('@id' in specimen) normalizedSpecimen['@id'] = specimen['@id'];
return normalizedSpecimen;
}

/**
* Parse the provided occurrence ID. The two expected formats are:
* - 'urn:catalog:[institutionCode]:[collectionCode]:[catalogNumber]'
Expand Down
25 changes: 21 additions & 4 deletions src/wrappers/TaxonConceptWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ class TaxonConceptWrapper {
this.defaultNomenCode = defaultNomenCode;
}

/**
* Normalize the specified taxon concept.
* @param tc A taxon concept to be normalized.
*/
static normalize(tc) {
const wrapped = new TaxonConceptWrapper(tc);
const normalizedTC = {
'@type': TaxonConceptWrapper.TYPE_TAXON_CONCEPT,
label: wrapped.label,
hasName: TaxonNameWrapper.normalize(wrapped.taxonName),
nameString: wrapped.taxonName.nameComplete,
accordingTo: wrapped.accordingTo,
};
if ('@id' in tc) normalizedTC['@id'] = tc['@id'];
return normalizedTC;
}

/**
* Return the taxon name of this taxon concept (if any) as an object.
*/
Expand Down Expand Up @@ -89,10 +106,10 @@ class TaxonConceptWrapper {
*/
get accordingTo() {
// Do we have any accordingTo information?
if (has(this.tunit, 'accordingTo')) return this.type.accordingTo;
if (has(this.tunit, 'accordingTo')) return this.tunit.accordingTo;

// Do we have an accordingToString?
if (has(this.tunit, 'accordingToString')) return this.type.accordingToString;
if (has(this.tunit, 'accordingToString')) return this.tunit.accordingToString;

// If not, we have no accodingTo information!
return undefined;
Expand All @@ -106,10 +123,10 @@ class TaxonConceptWrapper {
*/
get accordingToString() {
// Do we have any accordingTo information?
if (has(this.tunit, 'accordingTo')) return JSON.stringify(this.type.accordingTo);
if (has(this.tunit, 'accordingTo')) return JSON.stringify(this.tunit.accordingTo);

// Do we have an accordingToString?
if (has(this.tunit, 'accordingToString')) return this.type.accordingToString;
if (has(this.tunit, 'accordingToString')) return this.tunit.accordingToString;

// If not, we have no accodingTo information!
return undefined;
Expand Down
19 changes: 19 additions & 0 deletions src/wrappers/TaxonNameWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,25 @@ class TaxonNameWrapper {
return undefined;
}

/**
* Normalize the specified taxon name.
* @param txname A taxon name to be normalized.
*/
static normalize(txname) {
const wrapped = new TaxonNameWrapper(txname);
const normalizedTxname = {
'@type': TaxonNameWrapper.TYPE_TAXON_NAME,
nomenclaturalCode: wrapped.nomenclaturalCode,
label: wrapped.label,
nameComplete: wrapped.nameComplete,
genusPart: wrapped.genusPart,
specificEpithet: wrapped.specificEpithet,
infraspecificEpithet: wrapped.infraspecificEpithet,
};
if ('@id' in txname) normalizedTxname['@id'] = txname['@id'];
return normalizedTxname;
}

/**
* Returns the nomenclatural code of this taxon name.
*/
Expand Down
19 changes: 19 additions & 0 deletions src/wrappers/TaxonomicUnitWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@ class TaxonomicUnitWrapper {
this.defaultNomenCode = defaultNomenCode;
}

/**
* Normalize the specified taxonomic unit.
* @param tunit A taxonomic unit to be normalized.
*/
static normalize(tunit) {
const wrapped = new TaxonomicUnitWrapper(tunit);
if (wrapped.taxonConcept) {
return TaxonConceptWrapper.normalize(tunit);
}
if (wrapped.specimen) {
return SpecimenWrapper.normalize(tunit);
}
if (wrapped.externalReferences) {
// External references should only have an `@id`.
return tunit;
}
return tunit;
}

/**
* What type of specifier is this? This is an array that could contain multiple
* classes, but should contain one of:
Expand Down

0 comments on commit 12185da

Please sign in to comment.