From 71f2e85412d0e3f4ac14b3e6fecca31d65716beb Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Thu, 24 Apr 2025 15:22:17 -0400 Subject: [PATCH] refactor - cleanup code --- src/core/author.ts | 155 ++++++++++++++++++++++----------------------- 1 file changed, 74 insertions(+), 81 deletions(-) diff --git a/src/core/author.ts b/src/core/author.ts index 86621a09949..384a23abc43 100644 --- a/src/core/author.ts +++ b/src/core/author.ts @@ -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; } }