[Utilities] add cleanISSN

This commit is contained in:
Aurimas Vinckevicius 2013-01-25 21:25:04 -06:00
parent 5b34dce40f
commit 1f08592d0e

View file

@ -308,6 +308,28 @@ Zotero.Utilities = {
return false;
},
/**
* Clean and validate ISSN.
* Return issn if valid, otherwise return false
*/
"cleanISSN":function(/**String*/ issn) {
issn = issn.replace(/[^0-9a-z]+/ig, '').toUpperCase() //we only want to ignore punctuation, spaces
.match(/[0-9]{7}[0-9X]/); //13 digit or 10 digit
if(!issn) return false;
issn = issn[0];
// Verify ISBN-10 checksum
var sum = 0;
for (var i = 0; i < 7; i++) {
if(issn[i] == 'X') return false; //X can only be a check digit
sum += issn[i] * (8-i);
}
//check digit might be 'X'
sum += (issn[9] == 'X')? 10 : issn[9]*1;
return (sum % 11 == 0) ? issn.substring(0,4) + '-' + issn.substring(4) : false;
},
/**
* Convert plain text to HTML by replacing special characters and replacing newlines with BRs or
* P tags