Skip to content

Instantly share code, notes, and snippets.

@aurimasv
Last active December 23, 2015 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aurimasv/6693503 to your computer and use it in GitHub Desktop.
Save aurimasv/6693503 to your computer and use it in GitHub Desktop.
Generates ISBN registrant tables given XML Range Message (from http://www.isbn-international.org/page/ranges) loaded in the browser
/*
***** BEGIN LICENSE BLOCK *****
Generates ISBN registrant tables given XML Range Message
(from http://www.isbn-international.org/page/ranges) loaded in the browser
Copyright © 2013 Aurimas Vinckevicius (aurimas.dev~at~gmail.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***** END LICENSE BLOCK *****
*/
var prefVarName = 'prefs';
var prefs = {};
var same = {};
var groups = document.getElementsByTagName('Group');
for(var i=0, n=groups.length; i<n; i++) {
var prefix = groups[i].getElementsByTagName('Prefix')[0].textContent.split('-');
var uccPrefix = prefix[0];
prefix = prefix[1];
var ranges = [];
var rules = groups[i].getElementsByTagName('Rule');
for(var j=0, m=rules.length; j<m; j++) {
var range = rules[j].getElementsByTagName('Range')[0].textContent.split('-');
var length = rules[j].getElementsByTagName('Length')[0].textContent * 1;
ranges.push(range[0].substr(0,length));
ranges.push(range[1].substr(0,length));
}
if(ranges.length) {
ranges = ranges.sort(function(a, b) {
if(a.length == b.length) return a==b?0:(a<b?-1:1);
return a.length - b.length;
});
if(!same[ranges.join('.')]) {
if(!prefs[uccPrefix]) prefs[uccPrefix] = {};
prefs[uccPrefix][prefix] = ranges;
same[ranges.join('.')] = [uccPrefix+"']['"+prefix];
} else {
same[ranges.join('.')].push(uccPrefix+"']['"+prefix);
}
}
}
var sameStr = '';
for(var i in same) {
if(same[i].length == 1) continue;
same[i].push(same[i].shift());
sameStr += prefVarName + "['" + same[i].join("']=" + prefVarName + "['") + "']; ";
}
console.log("var " + prefVarName + "=" + JSON.stringify(prefs) + ";\n" + sameStr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment