Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JavaScript to standardize/normalize Library of Congress Call Numbers
function normalizeCallNo(callnum2) {
var callnum2 = callnum2.toLowerCase();
var part1 = '';
var part2 = '';
var part3 = '';
//----------------- Part 1 -----------------------------------------------
//find the first period
part1 = callnum2.substring(0, callnum2.indexOf('.'));
//Prepare Part 1 of Call no of length 7
part1 = part1.replace(/\s+/g, '');
//Calculate part 1 length and prepare padding string containing zeros
var pad = 7 - (part1.length);
var padstr = Array(pad + 1).join("0");
//Get location of first digit in part 1
var i = part1.search(/\d/)
//Pad part 1 with padding string
part1 = [part1.slice(0, i), ' ', padstr, part1.slice(i)].join('');
part1 = '0' + part1.toLowerCase();//console.log('+' + part1 + '+');
//console.log('P1-'+part1);
//----------------- Part 2 -----------------------------------------------
x = callnum2;
//remove all white spaces
x = x.replace(/\s+/g, '');
//Get the character after the first period and check if it's not numeric
if(isNaN(x.substring(x.indexOf('.')+1, x.indexOf('.')+2))){
//Not Numeric. Part 2 not available
part2 = '00000';
}else{
//Numeric. Part 2 available
y = x.split('.')[1];
var pad = 5 - (y.length);
var padstr = Array(pad + 1).join("0");
part2 = y + padstr;//console.log(part2);
}
//console.log('P2-'+part2);
//----------------- Part 3 -----------------------------------------------
if(isNaN(x.substring(x.indexOf('.')+1, x.indexOf('.')+2))){
//Part 2 not available
part3 = callnum2.substring(callnum2.indexOf('.') + 1, callnum2.length);
}else{
//part 2 avaialble
part3 = callnum2.substring(callnum2.indexOf('.') + 1, callnum2.length);
part3 = part3.substring(part3.indexOf('.') + 1, part3.length);
}
//Prepare Part 3 of Call no of length 6
part3.trim();
if(part3.indexOf(' ')>-1){
part3 = part3.substring(0, part3.indexOf(' '));
}
a = part3.substring(0, 1);
b = part3.substring(1, part3.length);
part3 = a + ' ' + b;
//Calculate part 2 length and prepare padding string containing zeros
var pad = 7 - (part3.length);
var padstr = Array(pad + 1).join("0");
part3 = part3.toLowerCase() + padstr;
//console.log('P3-' + part3);
//----------------- Part 4 -----------------------------------------------
if(isNaN(x.substring(x.indexOf('.')+1, x.indexOf('.')+2))){
//Part 2 not available
part4 = callnum2.substring(callnum2.indexOf('.') + 1, callnum2.length);
}else{
//part 2 avaialble
part4 = callnum2.substring(callnum2.indexOf('.') + 1, callnum2.length);
part4 = part4.substring(part4.indexOf('.') + 1, part4.length);
}
part4.trim();
if(part4.indexOf(' ')>-1){
part4 = part4.substring(part4.indexOf(' ')+1, part4.length);
}else{
part4 = '';
}
//console.log('P4-' + part4);
//----------------- Combine -----------------------------------------------
var normCallNo = part1 + part2 + '.' + part3 + ' ' + part4;
normCallNo = normCallNo.trim();
//console.log('+' + normCallNo + '+');
return normCallNo;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.