Skip to content

Commit e45ab48

Browse files
authored
Merge pull request #12622 from quarto-dev/refactor/author
refactor - cleanup author normalization code
2 parents 7cfbe4c + 71f2e85 commit e45ab48

File tree

1 file changed

+74
-81
lines changed

1 file changed

+74
-81
lines changed

src/core/author.ts

+74-81
Original file line numberDiff line numberDiff line change
@@ -29,99 +29,92 @@ const kOrcid = "orcid";
2929
const kUrl = "url";
3030

3131
export function cslNameToString(cslName: string | CSLName) {
32-
if (typeof cslName === "string") {
33-
return cslName;
34-
} else {
35-
if (cslName.literal) {
36-
return cslName.literal;
37-
} else {
38-
const parts: string[] = [];
39-
40-
if (cslName.given) {
41-
parts.push(cslName.given);
42-
}
32+
if (typeof cslName === "string") return cslName;
33+
if (cslName.literal) return cslName.literal;
34+
const parts: string[] = [];
4335

44-
if (cslName["dropping-particle"]) {
45-
parts.push(cslName["dropping-particle"]);
46-
}
47-
if (cslName["non-dropping-particle"]) {
48-
parts.push(cslName["non-dropping-particle"]);
49-
}
50-
if (cslName.family) {
51-
parts.push(cslName.family);
52-
}
36+
if (cslName.given) {
37+
parts.push(cslName.given);
38+
}
5339

54-
return parts.join(" ");
55-
}
40+
if (cslName["dropping-particle"]) {
41+
parts.push(cslName["dropping-particle"]);
42+
}
43+
if (cslName["non-dropping-particle"]) {
44+
parts.push(cslName["non-dropping-particle"]);
5645
}
46+
if (cslName.family) {
47+
parts.push(cslName.family);
48+
}
49+
50+
return parts.join(" ");
5751
}
5852

5953
export function parseAuthor(authorRaw: unknown, strict?: boolean) {
60-
if (authorRaw) {
61-
const parsed: Author[] = [];
62-
const authors = Array.isArray(authorRaw) ? authorRaw : [authorRaw];
63-
let unrecognized = 0;
64-
authors.forEach((author) => {
65-
if (typeof author === "string") {
66-
// Its a string, so make it a name
67-
parsed.push({
68-
name: author,
69-
});
70-
} else if (typeof author === "object") {
71-
// Parse the author object
72-
// Currently this only supports simple 'Distill Style'
73-
// authors and affiliations
74-
const name = author[kName];
75-
if (name) {
76-
const auth: Author = {
77-
name,
78-
};
79-
const affilation = author[kAffiliation];
80-
if (affilation) {
81-
auth.affilliation = { name: affilation };
82-
if (author[kAfilliationUrl]) {
83-
auth.affilliation.url = author[kAfilliationUrl];
84-
}
85-
}
86-
87-
const orcid = author[kOrcid];
88-
if (orcid) {
89-
auth.orcid = orcid;
54+
if (!authorRaw) {
55+
return undefined;
56+
}
57+
const parsed: Author[] = [];
58+
const authors = Array.isArray(authorRaw) ? authorRaw : [authorRaw];
59+
let unrecognized = false;
60+
authors.forEach((author) => {
61+
if (typeof author === "string") {
62+
// Its a string, so make it a name
63+
parsed.push({
64+
name: author,
65+
});
66+
} else if (typeof author === "object") {
67+
// Parse the author object
68+
// Currently this only supports simple 'Distill Style'
69+
// authors and affiliations
70+
const name = author[kName];
71+
if (name) {
72+
const auth: Author = {
73+
name,
74+
};
75+
const affilation = author[kAffiliation];
76+
if (affilation) {
77+
auth.affilliation = { name: affilation };
78+
if (author[kAfilliationUrl]) {
79+
auth.affilliation.url = author[kAfilliationUrl];
9080
}
81+
}
9182

92-
const url = author[kUrl];
93-
if (url) {
94-
auth.url = url;
95-
}
83+
const orcid = author[kOrcid];
84+
if (orcid) {
85+
auth.orcid = orcid;
86+
}
9687

97-
parsed.push(auth);
98-
} else if (author[kFamily]) {
99-
const given = author[kGiven];
100-
const family = author[kFamily];
101-
const dropping = author[kDropping];
102-
const nonDropping = author[kNonDropping];
103-
parsed.push({
104-
name: {
105-
[kGiven]: given,
106-
[kFamily]: family,
107-
[kDropping]: dropping,
108-
[kNonDropping]: nonDropping,
109-
},
110-
});
111-
} else {
112-
unrecognized = unrecognized + 1;
88+
const url = author[kUrl];
89+
if (url) {
90+
auth.url = url;
11391
}
114-
}
115-
});
11692

117-
// If we didn't know how to parse this author
118-
// just stand down - we just don't recognize this.
119-
if (strict && unrecognized > 0) {
120-
return undefined;
121-
} else {
122-
return parsed;
93+
parsed.push(auth);
94+
} else if (author[kFamily]) {
95+
const given = author[kGiven];
96+
const family = author[kFamily];
97+
const dropping = author[kDropping];
98+
const nonDropping = author[kNonDropping];
99+
parsed.push({
100+
name: {
101+
[kGiven]: given,
102+
[kFamily]: family,
103+
[kDropping]: dropping,
104+
[kNonDropping]: nonDropping,
105+
},
106+
});
107+
} else {
108+
unrecognized = true;
109+
}
123110
}
124-
} else {
111+
});
112+
113+
// If we didn't know how to parse this author
114+
// just stand down - we just don't recognize this.
115+
if (strict && unrecognized) {
125116
return undefined;
117+
} else {
118+
return parsed;
126119
}
127120
}

0 commit comments

Comments
 (0)