Skip to content

refactor - cleanup author normalization code #12622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 74 additions & 81 deletions src/core/author.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,99 +29,92 @@ const kOrcid = "orcid";
const kUrl = "url";

export function cslNameToString(cslName: string | CSLName) {
if (typeof cslName === "string") {
return cslName;
} else {
if (cslName.literal) {
return cslName.literal;
} else {
const parts: string[] = [];

if (cslName.given) {
parts.push(cslName.given);
}
if (typeof cslName === "string") return cslName;
if (cslName.literal) return cslName.literal;
const parts: string[] = [];

if (cslName["dropping-particle"]) {
parts.push(cslName["dropping-particle"]);
}
if (cslName["non-dropping-particle"]) {
parts.push(cslName["non-dropping-particle"]);
}
if (cslName.family) {
parts.push(cslName.family);
}
if (cslName.given) {
parts.push(cslName.given);
}

return parts.join(" ");
}
if (cslName["dropping-particle"]) {
parts.push(cslName["dropping-particle"]);
}
if (cslName["non-dropping-particle"]) {
parts.push(cslName["non-dropping-particle"]);
}
if (cslName.family) {
parts.push(cslName.family);
}

return parts.join(" ");
}

export function parseAuthor(authorRaw: unknown, strict?: boolean) {
if (authorRaw) {
const parsed: Author[] = [];
const authors = Array.isArray(authorRaw) ? authorRaw : [authorRaw];
let unrecognized = 0;
authors.forEach((author) => {
if (typeof author === "string") {
// Its a string, so make it a name
parsed.push({
name: author,
});
} else if (typeof author === "object") {
// Parse the author object
// Currently this only supports simple 'Distill Style'
// authors and affiliations
const name = author[kName];
if (name) {
const auth: Author = {
name,
};
const affilation = author[kAffiliation];
if (affilation) {
auth.affilliation = { name: affilation };
if (author[kAfilliationUrl]) {
auth.affilliation.url = author[kAfilliationUrl];
}
}

const orcid = author[kOrcid];
if (orcid) {
auth.orcid = orcid;
if (!authorRaw) {
return undefined;
}
const parsed: Author[] = [];
const authors = Array.isArray(authorRaw) ? authorRaw : [authorRaw];
let unrecognized = false;
authors.forEach((author) => {
if (typeof author === "string") {
// Its a string, so make it a name
parsed.push({
name: author,
});
} else if (typeof author === "object") {
// Parse the author object
// Currently this only supports simple 'Distill Style'
// authors and affiliations
const name = author[kName];
if (name) {
const auth: Author = {
name,
};
const affilation = author[kAffiliation];
if (affilation) {
auth.affilliation = { name: affilation };
if (author[kAfilliationUrl]) {
auth.affilliation.url = author[kAfilliationUrl];
}
}

const url = author[kUrl];
if (url) {
auth.url = url;
}
const orcid = author[kOrcid];
if (orcid) {
auth.orcid = orcid;
}

parsed.push(auth);
} else if (author[kFamily]) {
const given = author[kGiven];
const family = author[kFamily];
const dropping = author[kDropping];
const nonDropping = author[kNonDropping];
parsed.push({
name: {
[kGiven]: given,
[kFamily]: family,
[kDropping]: dropping,
[kNonDropping]: nonDropping,
},
});
} else {
unrecognized = unrecognized + 1;
const url = author[kUrl];
if (url) {
auth.url = url;
}
}
});

// If we didn't know how to parse this author
// just stand down - we just don't recognize this.
if (strict && unrecognized > 0) {
return undefined;
} else {
return parsed;
parsed.push(auth);
} else if (author[kFamily]) {
const given = author[kGiven];
const family = author[kFamily];
const dropping = author[kDropping];
const nonDropping = author[kNonDropping];
parsed.push({
name: {
[kGiven]: given,
[kFamily]: family,
[kDropping]: dropping,
[kNonDropping]: nonDropping,
},
});
} else {
unrecognized = true;
}
}
} else {
});

// If we didn't know how to parse this author
// just stand down - we just don't recognize this.
if (strict && unrecognized) {
return undefined;
} else {
return parsed;
}
}
Loading