Merge pull request #211 from aurimasv/isbn

Fixes to cleanISBN and translator code
This commit is contained in:
Simon Kornblith 2012-12-11 21:29:49 -08:00
commit 5f0f86fab9
2 changed files with 8 additions and 11 deletions

View File

@ -1045,7 +1045,7 @@ Zotero.Translate.Base.prototype = {
this._currentState = "translate";
if(!this.translator || !this.translator.length) {
throw new Error("Failed: no translator specified");
this.complete(false, new Error("No translator specified"));
}
this._libraryID = libraryID;
@ -2093,8 +2093,8 @@ Zotero.Translate.Search.prototype.setTranslator = function(translator) {
*/
Zotero.Translate.Search.prototype.complete = function(returnValue, error) {
if(this._currentState == "translate" && (!this.newItems || !this.newItems.length)) {
Zotero.debug("Translate: Could not find a result using "+this.translator[0].label+": \n"
+this._generateErrorString(error), 3);
Zotero.debug("Translate: Could not find a result using "+this.translator[0].label, 3);
if(error) Zotero.debug(this._generateErrorString(error), 3);
if(this.translator.length > 1) {
this.translator.shift();
this.translate(this._libraryID, this._saveAttachments);

View File

@ -277,7 +277,10 @@ Zotero.Utilities = {
* Return isbn if valid, otherwise return false
*/
"cleanISBN":function(/**String*/ isbn) {
isbn = isbn.replace(/[^x\d]+/ig, '').toUpperCase();
isbn = isbn.replace(/[^0-9a-z]+/ig, '').toUpperCase() //we only want to ignore punctuation, spaces
.match(/(?:97[89][0-9]{10}|[0-9]{9}[0-9X])/); //13 digit or 10 digit
if(!isbn) return false;
isbn = isbn[0];
if(isbn.length == 10) {
// Verify ISBN-10 checksum
@ -292,17 +295,11 @@ Zotero.Utilities = {
return (sum % 11 == 0) ? isbn : false;
}
isbn = isbn.replace(/X/g, ''); //get rid of Xs
if(isbn.length == 13) {
// ISBN-13 should start with 978 or 979 i.e. GS1 for book publishing industry
var prefix = isbn.slice(0,3);
if (prefix != "978" && prefix != "979") return false;
// Verify checksum
var sum = 0;
for (var i = 0; i < 12; i+=2) sum += isbn[i]*1; //to make sure it's int
for (i = 1; i < 12; i+=2) sum += isbn[i]*3;
for (var i = 1; i < 12; i+=2) sum += isbn[i]*3;
sum += isbn[12]*1; //add the check digit
return (sum % 10 == 0 )? isbn : false;