Merge pull request #89 from aurimasv/cleanAuthor

Clean author
This commit is contained in:
Simon Kornblith 2012-04-02 18:13:41 -07:00
commit 9862e3aa16

View file

@ -140,19 +140,24 @@ Zotero.Utilities = {
* @return {Object} firstName, lastName, and creatorType
*/
"cleanAuthor":function(author, type, useComma) {
const allCapsRe = /^[A-Z\u0400-\u042f]+$/;
var allCaps = 'A-Z' +
'\u0400-\u042f'; //cyrilic
var allCapsRe = new RegExp('^[' + allCaps + ']+$');
var initialRe = new RegExp('^-?[' + allCaps + ']$');
if(typeof(author) != "string") {
throw "cleanAuthor: author must be a string";
}
author = author.replace(/^[\s\.\,\/\[\]\:]+/, '');
author = author.replace(/[\s\,\/\[\]\:\.]+$/, '');
author = author.replace(/ +/, ' ');
author = author.replace(/^[\s\u00A0\.\,\/\[\]\:]+/, '')
.replace(/[\s\u00A0\.\,\/\[\]\:]+$/, '')
.replace(/[\s\u00A0]+/, ' ');
if(useComma) {
// Add spaces between periods
author = author.replace(/\.([^ ])/, ". $1");
var splitNames = author.split(/, ?/);
if(splitNames.length > 1) {
var lastName = splitNames[0];
@ -165,7 +170,7 @@ Zotero.Utilities = {
var lastName = author.substring(spaceIndex+1);
var firstName = author.substring(0, spaceIndex);
}
if(firstName && allCapsRe.test(firstName) &&
firstName.length < 4 &&
(firstName.length == 1 || lastName.toUpperCase() != lastName)) {
@ -182,15 +187,15 @@ Zotero.Utilities = {
var names = firstName.replace(/^[\s\.]+/,'')
.replace(/[\s\,]+$/,'')
//remove spaces surronding any dashes
.replace(/\s*([\u002D\u00AD\u2010-\u2015\u2212\u2E3A\u2E3B])\s*/,'$1')
.split(/[\s\.]+/);
.replace(/\s*([\u002D\u00AD\u2010-\u2015\u2212\u2E3A\u2E3B])\s*/,'-')
.split(/(?:[\s\.]+|(?=-))/);
var newFirstName = '';
for(var i=0, n=names.length; i<n; i++) {
newFirstName += names[i];
if(names[i].match(/^[A-Z]$/)) newFirstName += '.';
if(initialRe.test(names[i])) newFirstName += '.';
newFirstName += ' ';
}
firstName = newFirstName.trim();
firstName = newFirstName.replace(/ -/g,'-').trim();
}
return {firstName:firstName, lastName:lastName, creatorType:type};