Skip to content

Commit

Permalink
fix: check if fields present in author before adding mention
Browse files Browse the repository at this point in the history
  • Loading branch information
ewan-escience committed May 27, 2024
1 parent 0f8b7e5 commit d4edece
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
15 changes: 13 additions & 2 deletions frontend/utils/getCrossref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,21 @@ export function addPoliteEmail(url:string) {
function extractAuthors(item: CrossrefSelectItem) {
if (item.author) {
return item.author.map(author => {
return `${author.given} ${author.family}`
if (author.given && author.family) {
return `${author.given} ${author.family}`
}
if (author.name) {
return author.name
}
if (author.given) {
return author.given
}
if (author.family) {
return author.family
}
}).join(', ')
}
return ''
return null
}

function extractYearPublished(item: CrossrefSelectItem) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-FileCopyrightText: 2022 - 2023 Ewan Cahen (Netherlands eScience Center) <[email protected]>
// SPDX-FileCopyrightText: 2022 - 2023 Netherlands eScience Center
// SPDX-FileCopyrightText: 2022 - 2024 Ewan Cahen (Netherlands eScience Center) <[email protected]>
// SPDX-FileCopyrightText: 2022 - 2024 Netherlands eScience Center
// SPDX-FileCopyrightText: 2022 Dusan Mijatovic (dv4all)
// SPDX-FileCopyrightText: 2022 dv4all
//
Expand Down Expand Up @@ -69,7 +69,7 @@ public CrossrefMention(String doi) {
}

@Override
public MentionRecord mentionData() throws IOException, InterruptedException, RsdResponseException{
public MentionRecord mentionData() throws IOException, InterruptedException, RsdResponseException {
StringBuilder url = new StringBuilder("https://api.crossref.org/works/" + Utils.urlEncode(doi));
Config.crossrefContactEmail().ifPresent(email -> url.append("?mailto=").append(email));
String responseJson = Utils.get(url.toString());
Expand All @@ -87,10 +87,16 @@ public MentionRecord mentionData() throws IOException, InterruptedException, Rsd
for (JsonObject authorJson : authorsJson) {
String givenName = Utils.stringOrNull(authorJson.get("given"));
String familyName = Utils.stringOrNull(authorJson.get("family"));
if (givenName == null && familyName == null) continue;
if (givenName == null) authors.add(familyName);
else if (familyName == null) authors.add(givenName);
else authors.add(givenName + " " + familyName);
String name = Utils.stringOrNull(authorJson.get("name"));
if (givenName != null && familyName != null) {
authors.add(givenName + " " + familyName);
} else if (name != null) {
authors.add(name);
} else if (givenName != null) {
authors.add(givenName);
} else if (familyName != null) {
authors.add(familyName);
}
}
result.authors = String.join(", ", authors);
}
Expand Down

0 comments on commit d4edece

Please sign in to comment.