Skip to content

Instantly share code, notes, and snippets.

@aurimasv
Last active January 2, 2023 01:49
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aurimasv/6693537 to your computer and use it in GitHub Desktop.
Save aurimasv/6693537 to your computer and use it in GitHub Desktop.
Hyphenates ISBN numbers using tables generated by https://gist.github.com/aurimasv/6693503
/*
ISBN hyphenation using tables generated by https://gist.github.com/aurimasv/6693503
Includes tables generated on 2013-09-24
***** BEGIN LICENSE BLOCK *****
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 *****
*/
function hyphenateISBN(isbn) {
isbn = (isbn + '').replace(/[^x\d]/ig, '');
//pad just in case
if(isbn.length < 10) {
isbn = ('000000000' + isbn);
isbn = isbn.substr(isbn.length-10);
} else if(isbn.length != 10 && isbn.length != 13) {
return;
}
var parts = [];
var uccPref = '978';
if(isbn.length == 13) {
uccPref = isbn.substr(0,3);
parts.push(uccPref);
isbn = isbn.substr(3);
}
if(!prefs[uccPref]) return;
for(var i=1, pref=isbn[0]; !prefs[uccPref][pref] && i < isbn.length-2; pref += isbn[i++]) {}
var regRanges = prefs[uccPref][pref];
if(!regRanges) {
return;
}
parts.push(pref);
isbn = isbn.substr(pref.length);
var reg = '';
var done = false;
for(var j=0; !done && j<isbn.length; j++) {
reg+=isbn[j];
for(var k=1, m=regRanges.length; k<m && reg.length >= regRanges[k].length; k+=2) {
if(reg.length == regRanges[k].length && regRanges[k-1] <= reg && reg <= regRanges[k]) {
done = true;
break;
}
}
}
if(!done) return;
isbn = isbn.substr(reg.length);
parts.push(reg);
parts.push(isbn.substr(0,isbn.length-1));
parts.push(isbn[isbn.length-1]);
return parts.join('-');
}
var prefs={"978":{"0":["00","19","200","227","229","368","370","638","640","644","646","647","649","654","656","699","2280","2289","3690","3699","6390","6397","6550","6559","7000","8499","85000","89999","900000","949999","6398000","6399999","6450000","6459999","6480000","6489999","9500000","9999999"],"1":["01","02","04","06","000","009","030","034","100","397","714","716","0350","0399","0700","0999","3980","5499","6500","6799","6860","7139","7170","7319","7900","7999","8672","8675","9730","9877","55000","64999","68000","68599","74000","77499","77540","77639","77650","77699","77770","78999","80000","80049","80050","80499","80500","83799","83850","86719","86760","86979","869800","915999","916506","972999","987800","991149","991200","998989","7320000","7399999","7750000","7753999","7764000","7764999","7770000","7776999","8380000","8384999","9160000","9165059","9911500","9911999","9989900","9999999"],"2":["00","19","200","349","400","489","495","495","497","699","4960","4966","7000","8399","35000","39999","49670","49699","84000","89999","91980","91980","490000","494999","900000","919799","919810","919942","919969","949999","9199430","9199689","9500000","9999999"],"3":["00","02","04","19","030","033","200","699","0340","0369","7000","8499","03700","03999","85000","89999","95400","96999","98500","99999","900000","949999","9500000","9539999","9700000","9849999"],"4":["00","19","200","699","7000","8499","85000","89999","900000","949999","9500000","9999999"],"5":["01","19","200","420","430","430","440","440","450","603","605","699","0050","0099","4210","4299","4310","4399","4410","4499","7000","8499","9200","9299","9501","9799","9910","9999","00000","00499","85000","89999","91000","91999","93000","94999","98000","98999","900000","909999","6040000","6049999","9500000","9500999","9900000","9909999"],"7":["00","09","100","499","5000","7999","80000","89999","900000","999999"],"65":["","","","","","","","","","","","","00","01","250","299","300","302","5000","5129","5500","5999","80000","81824","86000","89999","900000","902449","990000","999999"],"80":["00","19","200","699","7000","8499","85000","89999","99900","99999","900000","998999"],"81":["00","19","200","699","7000","8499","85000","89999","900000","999999"],"82":["00","19","200","689","7000","8999","90000","98999","690000","699999","990000","999999"],"83":["00","19","200","599","7000","8499","60000","69999","85000","89999","900000","999999"],"84":["00","10","140","149","200","699","1100","1199","1300","1399","7000","8499","9000","9199","9700","9999","15000","19999","85000","89999","92400","92999","95000","96999","120000","129999","920000","923999","930000","949999"],"85":["00","19","96","97","200","454","456","528","534","539","5320","5339","5440","5479","5500","5999","7000","8499","9450","9599","45530","45599","52900","53199","54000","54029","54030","54039","54050","54089","54100","54399","54800","54999","60000","69999","85000","89999","92500","94499","98000","99999","455000","455299","540400","540499","540900","540999","900000","924999"],"86":["00","29","300","599","6000","7999","80000","89999","900000","999999"],"87":["","","","","","","","","00","29","400","649","7000","7999","85000","94999","970000","999999"],"88":["00","19","200","311","315","318","323","326","339","360","363","548","555","599","910","926","3270","3389","3610","3629","5490","5549","6000","8499","9270","9399","31200","31499","31900","32299","85000","89999","94800","99999","900000","909999","940000","947999"],"89":["00","24","250","549","990","999","5500","8499","85000","94999","97000","98999","950000","969999"],"90":["","","","","00","19","90","90","94","94","200","499","5000","6999","8500","8999","70000","79999","800000","849999"],"91":["","","","","","","0","1","20","49","500","649","7000","8199","85000","94999","970000","999999"],"92":["0","5","60","79","800","899","9000","9499","95000","98999","990000","999999"],"93":["00","09","100","499","5000","7999","80000","94999","950000","999999"],"94":["000","599","6000","8999","90000","99999"],"600":["00","09","100","499","993","995","5000","8999","9868","9929","90000","98679","99600","99999"],"601":["00","19","85","99","200","699","7000","7999","80000","84999"],"602":["00","06","200","499","0700","1399","1500","1699","5400","5999","6200","6999","7500","9499","14000","14999","17000","19999","50000","53999","60000","61999","70000","74999","95000","99999"],"603":["00","04","05","49","500","799","8000","8999","90000","99999"],"604":["0","4","50","89","900","979","9800","9999"],"605":["00","02","04","05","07","09","030","039","100","199","240","399","2000","2399","4000","5999","7500","7999","9000","9999","06000","06999","60000","74999","80000","89999"],"606":["09","49","000","089","500","799","910","919","975","999","8000","9099","9650","9749","92000","96499"],"607":["00","39","400","749","7500","9499","95000","99999"],"608":["0","0","7","9","10","19","200","449","4500","6499","65000","69999"],"609":["00","39","400","799","8000","9499","95000","99999"],"611":["",""],"612":["","","00","29","300","399","4000","4499","5000","5149","45000","49999"],"613":["0","9"],"615":["","","00","09","100","499","5000","7999","80000","89999"],"616":["00","19","200","699","7000","8999","90000","99999"],"617":["00","49","500","699","7000","8999","90000","99999"],"618":["00","19","200","499","5000","7999","80000","99999"],"619":["00","14","150","699","7000","8999","90000","99999"],"621":["","","","","","","00","29","400","599","8000","8999","95000","99999"],"622":["","","","","","","00","08","200","299","6000","7999","95000","99999"],"623":["","","","","","","00","09","200","399","6000","7999","90000","99999"],"624":["","","","","","","00","04","200","249","5000","5999","95000","99999"],"625":["","","","","","","00","00","400","449","7000","7999"],"626":["","","","","","","00","04","300","499","7000","7999","95000","99999"],"950":["00","49","500","899","9000","9899","99000","99999"],"951":["0","1","20","54","550","889","8900","9499","95000","99999"],"952":["00","19","60","65","80","94","200","499","5000","5999","6600","6699","7000","7999","9500","9899","67000","69999","99000","99999"],"953":["0","0","10","14","51","54","150","479","500","500","6000","9499","48000","49999","50100","50999","55000","59999","95000","99999"],"954":["00","28","300","799","2900","2999","8000","8999","9300","9999","90000","92999"],"955":["20","33","550","710","0000","1999","3400","3549","3600","3799","3900","4099","4500","4999","7150","9499","35500","35999","38000","38999","41000","44999","50000","54999","71100","71499","95000","99999"],"956":["00","08","10","19","200","599","6000","6999","7000","9999","09000","09999"],"957":["00","02","05","19","21","27","31","43","440","819","0300","0499","2000","2099","8200","9699","28000","30999","97000","99999"],"958":["00","48","490","509","600","799","5100","5199","5400","5599","8000","9499","52000","53999","56000","59999","95000","99999"],"959":["00","19","200","699","7000","8499","85000","99999"],"960":["00","19","93","93","200","659","690","699","6600","6899","7000","8499","9400","9799","85000","92999","98000","99999"],"961":["","","00","19","200","599","6000","8999","90000","95999"],"962":["00","19","200","699","900","999","7000","8499","8700","8999","85000","86999"],"963":["00","19","200","699","7000","8499","9000","9999","85000","89999"],"964":["00","14","150","249","300","549","970","989","2500","2999","5500","8999","9900","9999","90000","96999"],"965":["","","","","00","19","200","599","7000","7999","90000","99999"],"966":["00","12","14","14","130","139","170","199","279","289","300","699","910","949","980","999","1500","1699","2000","2789","2900","2999","7000","8999","90000","90999","95000","97999"],"967":["","","00","00","60","89","250","254","300","499","900","989","0100","0999","2000","2499","2800","2999","5000","5999","9900","9989","10000","19999","99900","99999"],"968":["01","39","400","499","800","899","5000","7999","9000","9999"],"969":["0","1","20","20","24","39","210","219","400","749","2200","2299","7500","9999","23000","23999"],"970":["01","59","600","899","9000","9099","9700","9999","91000","96999"],"971":["02","02","06","49","97","98","000","015","500","849","0160","0199","0300","0599","8500","9099","9600","9699","9900","9999","91000","95999"],"972":["0","1","20","54","550","799","8000","9499","95000","99999"],"973":["0","0","20","54","100","169","550","759","1700","1999","7600","8499","8900","9499","85000","88999","95000","99999"],"974":["00","19","200","699","7000","8499","9500","9999","85000","89999","90000","94999"],"975":["02","23","250","599","990","999","2400","2499","6000","9199","00000","01999","92000","98999"],"976":["0","3","40","59","600","799","8000","9499","95000","99999"],"977":["00","19","90","98","200","499","700","849","990","999","5000","6999","85000","89999"],"978":["000","199","900","999","2000","2999","8000","8999","30000","79999"],"979":["20","29","000","099","400","799","1000","1499","3000","3999","8000","9499","15000","19999","95000","99999"],"980":["00","19","200","599","6000","9999"],"981":["00","16","18","19","200","299","310","399","3000","3099","4000","9999","17000","17999"],"982":["00","09","70","89","100","699","9000","9799","98000","99999"],"983":["00","01","45","49","50","79","020","199","800","899","2000","3999","9000","9899","40000","44999","99000","99999"],"984":["00","39","400","799","8000","8999","90000","99999"],"985":["00","39","400","599","880","899","6000","8799","90000","99999"],"986":["00","05","08","11","120","539","0700","0799","5400","7999","06000","06999","80000","99999"],"987":["00","09","30","35","42","43","85","89","500","829","1000","1999","3600","3999","4000","4199","4400","4499","4900","4999","8300","8499","9000","9499","20000","29999","45000","48999","95000","99999"],"988":["00","11","200","739","8000","9699","12000","19999","74000","76999","77000","79999","97000","99999"],"989":["0","1","20","34","37","52","550","799","8000","9499","35000","36999","53000","54999","95000","99999"],"9914":["","","","","","","40","44","700","749","9850","9999"],"9915":["","","","","","","40","59","650","799","9300","9999"],"9916":["","","","","0","0","4","4","10","39","600","749","9500","9999"],"9917":["","","","","","","0","0","30","34","600","699","9800","9999"],"9918":["","","","","","","0","0","20","29","600","799","9500","9999"],"9919":["","","","","","","20","27","500","599","9500","9999"],"9920":["","","","","","","32","39","550","799","9000","9999"],"9921":["","","","","","","0","0","30","39","700","899","9700","9999"],"9922":["","","","","","","20","29","600","799","9000","9999"],"9923":["","","","","0","0","10","49","700","899","9700","9999"],"9924":["","","","","","","30","39","500","649","9000","9999"],"9925":["0","2","30","54","550","734","7350","9999"],"9926":["0","1","20","39","400","799","8000","9999"],"9927":["","","00","09","100","399","4000","4999"],"9929":["0","3","40","54","550","799","8000","9999"],"9930":["00","49","500","939","9400","9999"],"9931":["00","29","300","899","9000","9999"],"9932":["00","39","400","849","8500","9999"],"9933":["0","0","10","39","400","899","9000","9999"],"9934":["0","0","10","49","500","799","8000","9999"],"9937":["0","2","30","49","500","799","8000","9999"],"9938":["00","79","800","949","9500","9999"],"9939":["0","4","50","79","98","99","800","899","9000","9799"],"9940":["0","1","20","49","84","86","500","839","8700","9999"],"9941":["0","0","8","8","10","39","400","799","9000","9999"],"9942":["00","74","750","849","900","984","8500","8999","9850","9999"],"9943":["00","29","300","399","975","999","4000","9749"],"9944":["60","69","80","89","100","499","700","799","900","999","0000","0999","5000","5999"],"9945":["00","00","08","39","57","57","80","80","010","079","400","569","580","799","810","849","8500","9999"],"9946":["0","1","20","39","400","899","9000","9999"],"9947":["0","1","20","79","800","999"],"9949":["00","08","10","39","70","71","75","89","090","099","400","699","7200","7499","9000","9999"],"9950":["00","29","300","849","8500","9999"],"9953":["0","0","10","39","60","89","93","96","400","599","970","999","9000","9299"],"9954":["0","1","20","39","99","99","400","799","8000","9899"],"9955":["00","39","400","929","9300","9999"],"9957":["00","39","65","67","70","84","88","99","400","649","680","699","8500","8799"],"9958":["00","01","10","18","20","49","020","029","040","089","500","899","0300","0399","0900","0999","1900","1999","9000","9999"],"9959":["0","1","20","79","98","99","800","949","970","979","9500","9699"],"9960":["00","59","600","899","9000","9999"],"9961":["0","2","30","69","700","949","9500","9999"],"9962":["00","54","56","59","600","849","5500","5599","8500","9999"],"9963":["0","1","30","54","250","279","550","734","2000","2499","2800","2999","7350","7499","7500","9999"],"9964":["0","6","70","94","950","999"],"9965":["00","39","400","899","9000","9999"],"9966":["14","14","20","69","000","139","750","820","825","825","829","959","1500","1999","7000","7499","8210","8249","8260","8289","9600","9999"],"9971":["0","5","60","89","900","989","9900","9999"],"9972":["1","1","00","09","30","59","200","249","600","899","2500","2999","9000","9999"],"9973":["00","05","10","69","060","089","700","969","0900","0999","9700","9999"],"9974":["0","2","30","54","91","94","95","99","550","749","880","909","7500","8799"],"9975":["0","0","45","89","100","299","900","949","3000","3999","4000","4499","9500","9999"],"9976":["0","4","59","89","580","589","900","989","5000","5799","9900","9999"],"9977":["00","89","900","989","9900","9999"],"9978":["00","29","40","94","300","399","950","989","9900","9999"],"9979":["0","4","50","64","66","75","650","659","760","899","9000","9999"],"9980":["0","3","40","89","900","989","9900","9999"],"9981":["00","09","20","79","100","159","800","949","1600","1999","9500","9999"],"9982":["00","79","800","989","9900","9999"],"9983":["","","80","94","950","989","9900","9999"],"9984":["00","49","500","899","9000","9999"],"9985":["0","4","50","79","800","899","9000","9999"],"9986":["00","39","97","99","400","899","940","969","9000","9399"],"9987":["00","39","400","879","8800","9999"],"9988":["0","3","40","54","550","749","7500","9999"],"9989":["0","0","30","59","100","199","600","949","2000","2999","9500","9999"],"99901":["00","49","80","99","500","799"],"99903":["0","1","20","89","900","999"],"99904":["0","5","60","89","900","999"],"99905":["0","3","40","79","800","999"],"99906":["0","2","30","59","70","89","90","94","600","699","950","999"],"99908":["0","0","10","89","900","999"],"99909":["0","3","40","94","950","999"],"99910":["0","2","30","89","900","999"],"99911":["00","59","600","999"],"99912":["0","3","60","89","400","599","900","999"],"99913":["","","","","0","2","30","35","600","604"],"99914":["0","4","7","7","50","69","80","89","900","999"],"99915":["0","4","50","79","800","999"],"99916":["0","2","30","69","700","999"],"99917":["0","2","30","88","890","999"],"99919":["0","2","40","79","300","399","800","999"],"99920":["0","4","50","89","900","999"],"99921":["0","1","8","8","20","69","90","99","700","799"],"99922":["0","3","40","69","700","999"],"99925":["0","0","3","3","10","19","40","79","200","299","800","999"],"99926":["0","0","10","59","87","89","90","99","600","869"],"99927":["0","2","30","59","600","999"],"99928":["0","0","10","79","800","999"],"99932":["0","0","7","7","10","59","80","99","600","699"],"99935":["0","2","7","8","30","59","90","99","600","699"],"99936":["0","0","10","59","600","999"],"99937":["0","1","20","59","600","999"],"99938":["0","1","20","59","90","99","600","899"],"99940":["0","0","10","69","700","999"],"99941":["0","2","30","79","800","999"],"99953":["0","2","30","79","94","99","800","939"],"99954":["0","2","30","69","88","99","700","879"],"99955":["0","1","20","59","80","99","600","799"],"99956":["00","59","86","99","600","859"],"99957":["0","1","20","79","95","99","800","949"],"99958":["0","4","50","93","940","949","950","999"],"99960":["","","10","94","070","099","950","999"],"99961":["0","2","37","89","300","369","900","999"],"99963":["00","49","92","99","500","919"],"99965":["0","2","36","62","300","359","630","999"],"99966":["0","2","30","69","80","96","700","799","970","999"],"99971":["0","3","40","84","850","999"],"99974":["0","0","10","25","40","63","65","79","260","399","640","649","800","999"],"99976":["","","0","1","20","59","600","799","900","999"],"99977":["","","","","0","1","40","69","700","799","995","999"],"99978":["0","4","50","69","700","999"],"99980":["","","","","0","0","30","59","800","999"],"99981":["","","","","0","1","30","59","800","999"],"99982":["","","","","","","0","0","50","65","900","979"],"99983":["","","","","0","0","50","69","950","999"],"99985":["","","","","55","59","950","999"],"99987":["","","850","999"]},"979":{"8":["","","","","","","","","200","219","5500","7499","9850000","9850009"],"10":["00","19","200","699","7000","8999","90000","97599","976000","999999"],"11":["00","24","250","549","5500","8499","85000","94999","950000","999999"],"12":["","","","","","","","","200","299","5950","5999","80000","84999"]}};
prefs['978']['614']=prefs['978']['609']; prefs['978']['99902']=prefs['978']['99951']=prefs['978']['611']; prefs['978']['620']=prefs['978']['613']; prefs['978']['9936']=prefs['978']['9952']=prefs['978']['9926']; prefs['978']['9928']=prefs['978']['9927']; prefs['978']['9968']=prefs['978']['9930']; prefs['978']['9948']=prefs['978']['9951']=prefs['978']['9932']; prefs['978']['9935']=prefs['978']['9956']=prefs['978']['9933']; prefs['978']['99923']=prefs['978']['99924']=prefs['978']['99934']=prefs['978']['99964']=prefs['978']['9947']; prefs['978']['9967']=prefs['978']['9970']=prefs['978']['9965']; prefs['978']['99949']=prefs['978']['99903']; prefs['978']['99939']=prefs['978']['99904']; prefs['978']['99918']=prefs['978']['99973']=prefs['978']['99905']; prefs['978']['99968']=prefs['978']['99912']; prefs['978']['99929']=prefs['978']['99930']=prefs['978']['99931']=prefs['978']['99942']=prefs['978']['99944']=prefs['978']['99948']=prefs['978']['99950']=prefs['978']['99952']=prefs['978']['99962']=prefs['978']['99969']=prefs['978']['99979']=prefs['978']['99915']; prefs['978']['99947']=prefs['978']['99916']; prefs['978']['99975']=prefs['978']['99919']; prefs['978']['99945']=prefs['978']['99970']=prefs['978']['99972']=prefs['978']['99920']; prefs['978']['99933']=prefs['978']['99943']=prefs['978']['99946']=prefs['978']['99959']=prefs['978']['99927']; prefs['978']['99967']=prefs['978']['99936']; prefs['978']['99986']=prefs['978']['99983'];
@kittsville
Copy link

The function seems to trip up when attempting to hyphenate the valid ISBN 9780006483014 for the book 'The Once and Future King'. As I'm not too good with ISBNs, my attempts to find the cause of the issue failed.

@hlowagie
Copy link

hlowagie commented Oct 6, 2022

Noob question, but how do I activate that script on , for example: a div with id="isbn" and in that div=9780747532743

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment