Add Zotero.Utilities.Internal.hyphenateISBN
Hyphenates ISBN-10 or ISBN-13 according to data from https://www.isbn-international.org
This commit is contained in:
parent
37921b0910
commit
68e7849fcf
3 changed files with 86 additions and 0 deletions
11
chrome/content/zotero/xpcom/isbn.js
Normal file
11
chrome/content/zotero/xpcom/isbn.js
Normal file
File diff suppressed because one or more lines are too long
|
@ -350,6 +350,80 @@ Zotero.Utilities.Internal = {
|
|||
childWindow = childWindow.parent;
|
||||
if(childWindow === parentWindow) return true;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Hyphenate an ISBN based on the registrant table available from
|
||||
* https://www.isbn-international.org/range_file_generation
|
||||
* See isbn.js
|
||||
*
|
||||
* @param {String} isbn ISBN-10 or ISBN-13
|
||||
* @param {Boolean} dontValidate Do not attempt to validate check digit
|
||||
* @return {String} Hyphenated ISBN or empty string if invalid ISBN is supplied
|
||||
*/
|
||||
"hyphenateISBN": function(isbn, dontValidate) {
|
||||
isbn = Zotero.Utilities.cleanISBN(isbn, dontValidate);
|
||||
if (!isbn) return '';
|
||||
|
||||
var ranges = Zotero.ISBN.ranges,
|
||||
parts = [],
|
||||
uccPref,
|
||||
i = 0;
|
||||
if (isbn.length == 10) {
|
||||
uccPref = '978';
|
||||
} else {
|
||||
uccPref = isbn.substr(0,3);
|
||||
if (!ranges[uccPref]) return ''; // Probably invalid ISBN, but the checksum is OK
|
||||
parts.push(uccPref);
|
||||
i = 3; // Skip ahead
|
||||
}
|
||||
|
||||
var group = '',
|
||||
found = false;
|
||||
while (i < isbn.length-3 /* check digit, publication, registrant */) {
|
||||
group += isbn.charAt(i);
|
||||
if (ranges[uccPref][group]) {
|
||||
parts.push(group);
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
if (!found) return ''; // Did not find a valid group
|
||||
|
||||
// Array of registrant ranges that are valid for a group
|
||||
// Array always contains an even number of values (as string)
|
||||
// From left to right, the values are paired so that the first indicates a
|
||||
// lower bound of the range and the right indicates an upper bound
|
||||
// The ranges are sorted by increasing number of characters
|
||||
var regRanges = ranges[uccPref][group];
|
||||
|
||||
var registrant = '';
|
||||
found = false;
|
||||
i++; // Previous loop 'break'ed early
|
||||
while (!found && i < isbn.length-2 /* check digit, publication */) {
|
||||
registrant += isbn.charAt(i);
|
||||
|
||||
for(let j=0; j < regRanges.length && registrant.length >= regRanges[j].length; j+=2) {
|
||||
if(registrant.length == regRanges[j].length
|
||||
&& registrant >= regRanges[j] && registrant <= regRanges[j+1] // Falls within the range
|
||||
) {
|
||||
parts.push(registrant);
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
if (!found) return ''; // Outside of valid range, but maybe we need to update our data
|
||||
|
||||
parts.push(isbn.substring(i,isbn.length-1)); // Publication is the remainder up to last digit
|
||||
parts.push(isbn.charAt(isbn.length-1)); // Check digit
|
||||
|
||||
return parts.join('-');
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -46,6 +46,7 @@ const xpcomFilesAll = [
|
|||
'translation/translate_firefox',
|
||||
'translation/tlds',
|
||||
'utilities',
|
||||
'isbn',
|
||||
'utilities_internal',
|
||||
'utilities_translate'
|
||||
];
|
||||
|
|
Loading…
Reference in a new issue