commit 9862e3aa16fa21781b0d10a8dd7707dececc208f
parent 8f0296baf349107faf5a49230d363eee07544ad5
Author: Simon Kornblith <simon@simonster.com>
Date: Mon, 2 Apr 2012 18:13:41 -0700
Merge pull request #89 from aurimasv/cleanAuthor
Clean author
Diffstat:
1 file changed, 17 insertions(+), 12 deletions(-)
diff --git a/chrome/content/zotero/xpcom/utilities.js b/chrome/content/zotero/xpcom/utilities.js
@@ -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};