From 1f08592d0ea1644c56790402adf049470216aa97 Mon Sep 17 00:00:00 2001 From: Aurimas Vinckevicius Date: Fri, 25 Jan 2013 21:25:04 -0600 Subject: [PATCH] [Utilities] add cleanISSN --- chrome/content/zotero/xpcom/utilities.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/chrome/content/zotero/xpcom/utilities.js b/chrome/content/zotero/xpcom/utilities.js index 9443751ae0..e2229c6792 100644 --- a/chrome/content/zotero/xpcom/utilities.js +++ b/chrome/content/zotero/xpcom/utilities.js @@ -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