Skip to content

Commit

Permalink
Updated CitationWrapper to support firstname/lastname.
Browse files Browse the repository at this point in the history
  • Loading branch information
gaurav committed Feb 23, 2021
1 parent 83e692e commit 4b63d30
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
27 changes: 24 additions & 3 deletions src/wrappers/CitationWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,44 @@ class CitationWrapper {
this.citation = citation;
}

/**
* Helper method to return a single name for a given agent entry.
* The algorithm we use is:
* - `name`, if one is present.
* - Some combination of `lastname`, `firstname` and `middlename`, if present.
*/
static getAgentName(agent) {
if (has(agent, 'name')) return agent.name;
if (has(agent, 'lastname')) {
if (has(agent, 'firstname')) {
if (has(agent, 'middlename')) {
return `${agent.firstname} ${agent.middlename} ${agent.lastname}`;
}

return `${agent.firstname} ${agent.lastname}`;
}
return `${agent.lastname}`;
}
return '(Unable to read name)';
}

/** Returns a single string with the entire bibliographic citation. */
toString() {
if (!this.citation || isEmpty(this.citation)) return undefined;

// If we already have a bibliographic citation, we can just return that.
if (has(this.citation, 'bibliographicCitation')) return this.citation.bibliographicCitation;

let authors = (this.citation.authors || []).map(author => author.name);
let authors = (this.citation.authors || []).map(CitationWrapper.getAgentName);
if (authors.length === 0) authors = ['Anonymous'];
if (authors.length > 2) authors = [`${authors[0]} et al`];
let authorsAndTitle = `${authors.join(' and ')} (${this.citation.year || 'n.d.'}) ${this.citation.title || 'Untitled'}`;

const editorLists = [];
const editors = (this.citation.editors || []).map(editor => editor.name);
const editors = (this.citation.editors || []).map(CitationWrapper.getAgentName);
if (editors.length > 0) editorLists.push(`eds: ${editors.join(' and ')}`);

const seriesEditors = (this.citation.series_editors || []).map(editor => editor.name);
const seriesEditors = (this.citation.series_editors || []).map(CitationWrapper.getAgentName);
if (seriesEditors.length > 0) editorLists.push(`series eds: ${seriesEditors.join(' and ')}`);

if (editorLists.length > 0) authorsAndTitle += ` [${editorLists.join(', ')}]`;
Expand Down
2 changes: 1 addition & 1 deletion test/examples/correct/testudinata_phylonym.jsonld
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
}
]
},
"bibliographicCitation": "undefined et al (2020) Testudinata (#273) [eds: and and ] CRC Press, Boca Raton, FL "
"bibliographicCitation": "W. G. Joyce et al (2020) Testudinata (#273) [eds: K. de Queiroz and P. D. Cantino and J. A. Gauthier] CRC Press, Boca Raton, FL "
},
"apomorphy": {
"@type": "https://semanticscience.org/resource/SIO_010056",
Expand Down
2 changes: 1 addition & 1 deletion test/examples/correct/testudinata_phylonym.nq
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<http://example.org/phyx.js/example#phyloref0> <http://www.w3.org/2000/01/rdf-schema#label> "Testudinata" .
<http://example.org/phyx.js/example#phyloref0> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://ontology.phyloref.org/phyloref.owl#Phyloreference> .
<http://example.org/phyx.js/example#phyloref0> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://ontology.phyloref.org/phyloref.owl#PhyloreferenceUsingApomorphy> .
_:b0 <http://purl.org/dc/terms/bibliographicCitation> "undefined et al (2020) Testudinata (#273) [eds: and and ] CRC Press, Boca Raton, FL " .
_:b0 <http://purl.org/dc/terms/bibliographicCitation> "W. G. Joyce et al (2020) Testudinata (#273) [eds: K. de Queiroz and P. D. Cantino and J. A. Gauthier] CRC Press, Boca Raton, FL " .
_:b0 <http://purl.org/dc/terms/title> "Testudinata (#273)" .
_:b1 <http://purl.org/spar/pso/withStatus> <http://purl.org/spar/pso/submitted> .
_:b1 <http://www.essepuntato.it/2012/04/tvc/atTime> _:b2 .
Expand Down

0 comments on commit 4b63d30

Please sign in to comment.