Skip to content

Commit 3143d55

Browse files
authored
capitalizeName: Capitalize each part individually (zotero#18)
...If it's either in all uppercase or all lowercase.
1 parent d4e514c commit 3143d55

1 file changed

Lines changed: 23 additions & 9 deletions

File tree

utilities.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,24 +120,38 @@ var Utilities = {
120120

121121
/**
122122
* Fixes author name capitalization.
123-
* Currently for all uppercase names only
123+
* Splits names into space-separated parts and only changes parts either in all uppercase
124+
* or all lowercase.
124125
*
125126
* JOHN -> John
126127
* GUTIÉRREZ-ALBILLA -> Gutiérrez-Albilla
127128
* O'NEAL -> O'Neal
129+
* o'neal -> O'Neal
130+
* O'neal -> O'neal
131+
* John MacGregor O'NEILL -> John MacGregor O'Neill
132+
* martha McMiddlename WASHINGTON -> Martha McMiddlename Washington
128133
*
129134
* @param {String} string Uppercase author name
130135
* @return {String} Title-cased author name
131136
*/
132137
capitalizeName: function (string) {
133-
if (typeof string === "string" && string.toUpperCase() === string) {
134-
string = Utilities.XRegExp.replace(
135-
string.toLowerCase(),
136-
Utilities.XRegExp('(^|[^\\pL])\\pL', 'g'),
137-
m => m.toUpperCase()
138-
);
139-
}
140-
return string;
138+
if (!(typeof string === 'string')) {
139+
return string;
140+
}
141+
return string.split(' ')
142+
.map((part) => {
143+
if (part.toUpperCase() === part || part.toLowerCase() === part) {
144+
return Utilities.XRegExp.replace(
145+
part.toLowerCase(),
146+
Utilities.XRegExp('(^|[^\\pL])\\pL', 'g'),
147+
m => m.toUpperCase()
148+
);
149+
}
150+
else {
151+
return part;
152+
}
153+
})
154+
.join(' ');
141155
},
142156

143157
/**

0 commit comments

Comments
 (0)