Skip to content

Instantly share code, notes, and snippets.

@allolex
Created May 26, 2014 01:14
Show Gist options
  • Select an option

  • Save allolex/2671f23eb401a6e460ef to your computer and use it in GitHub Desktop.

Select an option

Save allolex/2671f23eb401a6e460ef to your computer and use it in GitHub Desktop.
A javascript screen form parser I wrote in 2002. lol
function SplitIt(macsplit) {
var temp = new Array();
var reversed = new Array();
var re_macsplit = /[A-F0-9]/;
var j = 0;
for (var i = 0; i < macsplit.length; i++) { // Loop through whole MAC ID string
if (re_macsplit.test(macsplit[i])) { // Match to hexidecimal number regexp
temp[j] = macsplit[i]; // Assign number to the array item
j++;
}
}
temp.reverse(); // reverse the array order
var beginning = macsplit.length; // assign var length to variable
for (beginning; beginning < 12; beginning++) { // Add zeroes until total array length = 12
temp[beginning] = "0";
}
temp.reverse(); // reverse the array order back to normal
var octet1 = temp[0] + temp[1] + temp[2] + temp[3]; // assign array item value groups to vars
var octet2 = temp[4] + temp[5] + temp[6] + temp[7];
var octet3 = temp[8] + temp[9] + temp[10] + temp[11];
var results;
results = octet1 + "." + octet2 + "." + octet3; // format final MAC ID result
return results;
} // end function
function toUpper(x) { // modified from http://developer.irt.org/script/531.htm
var pattern = /(\w)(\w*)/; // a letter, and then one, none or more letters
var a = x.split(/\s+/g); // split the sentence into an array of words
for (i = 0; i < a.length; i++) {
var parts = a[i].match(pattern); // just a temp variable to store the fragments in.
var firstLetter = parts[1].toUpperCase();
var restOfWord = parts[2].toLowerCase();
a[i] = firstLetter + restOfWord; // re-assign it back to the array and move on
}
x = a.join(' '); // join it back together
return x;
}
function ParseIt() {
var scratch = window.document.parserform.parse_window.value; // Put the window contents into a string variable
var words = new Array(); // Define an array for the window contents
words = scratch.split(" "); // Split the string at all spaces
var print = new Array(); // Define a variable for the output
var results = new Array();
var phonenumber = new Array;
var number = new Array();
var fname = "FIRSTNAME"; // Define variables for the information we want to extract
var lname = "LASTNAME";
var initial = "X";
var corp = "XXXXX"
var account = "999999";
var customer = "99";
var house = "9999";
var street = "STREET";
var street_type = "SX";
var apt = "999";
var zip = "99999";
var macids = new Array();
var macarray = new Array();
var nsew = "X";
var title = "XX";
// Here are the regular expressions assigned to variables.
var re_fname = /\D{2,10}/;
var re_title = /^MR|^DR|^MS|^RV/i;
var re_titlef = /[^a-zA-Z]frst\b/;
var re_initial = /\b([A-Z])\b/;
var re_initialf = /[^a-zA-Z]lst\b/;
var re_lname = /[a-zA-Z]{2,10}/;
var re_corp = /04512|05034|10340|10380|10383|10385|17206/;
var re_account = /\((\d{6})\-/;
var re_customer = /([0-9]{1,2})\)$/;
var re_compacct = /\((\d{6})\-([ \d]+)\)/;
var re_phone = /\((\d{3})\)(\d{3})\-(\d{4})/g;
var re_zip = /9\d{4}/;
var re_macids = /([\dA-F]{9,12}\b)/i;
var re_initial = /[A-Z]{1}/;
var re_nsew = /\b([NSEW])\b/;
var re_house = /\d{1,5}/;
var re_apt = /[\dA-Z]+/i;
var re_street_type = /\b(ST|RD|DR|AV|AVE|LN|CI|CT|BLVD|BL|PW)\b/i;
var re_space = /\b/;
var re_digit = /\d./;
var re_nondigit = /\D./;
var re_ctype = /type\b/;
// Initialize counters for the parse loops
var j = 0;
var k = 0;
var l = 0;
// Parse the text
for (var i = 0; i < words.length; i++) { // Loop until the whole array is done
if (re_space.test(words[i])) { // Use the space (word boundary) test above
print[j] = words[i]; // If it's a good match, add it to the print array
j++;
}
}
for (var i = 0; i < print.length; i++) {
// complete account compacct
if (re_compacct.test(print[i])) { // check to see if the account has a two-digit cust. no.
account = print[i].replace(re_compacct, "$1"); // split the string into account
customer = print[i].replace(re_compacct, "$2"); // and customer no.
} else {
// account number
if (re_account.test(print[i])) { // Checks to see if array item matches the account number
account = print[i].replace(re_account, "$1"); // assign value and get rid of extra characters
}
// corp number "corp"
if (re_corp.test(print[i])) { // Does the array item look like a corp number?
corp = print[i];
}
if (corp.length == 4) {
corp = "0" + corp;
}
// customer number "customer"
if (re_customer.test(print[i])) { // Does the array item look like a customer number?
customer = print[i].replace(re_customer, "$1"); // assign value and get rid of extra characters
if (customer < 10) {
customer = "0" + customer;
}
}
}
// phone numbers
var phonen = "";
var phonen2 = "";
if (re_phone.test(print[i])) {
phonenumber[l] = print[i].replace(re_phone, "$1") + "-" + print[i].replace(re_phone, "$2") + "-" + print[i].replace(re_phone, "$3");
number[l] = print[i].replace(re_phone, "$1") + print[i].replace(re_phone, "$2") + print[i].replace(re_phone, "$3");
l++;
}
for (var x = 0; x < phonenumber.length; x++) {
phonen = phonen + " " + phonenumber[x];
phonen2 = phonen2 + "\r" + number[x];
}
// street type "street_type", street "street", zip "zip", apartment number "apt",
// street direction "nsew", house number "house"
if (re_street_type.test(print[i])) {
street_type = print[i];
street = print[i - 1];
if (re_nsew.test(print[i - 2])) {
house = print[i - 3];
nsew = print[i - 2];
}
if (re_nsew.test(print[i - 3])) {
street = print[i - 2] + " " + street;
nsew = print[i - 3];
house = print[i - 4];
} else {
nsew = "";
if (re_digit.test(print[i - 2])) {
house = print[i - 2];
street = print[i - 1];
}
if (re_digit.test(print[i - 4])) {
house = print[i - 4];
street = print[i - 2] + " " + street;
}
}
if (print[i + 1] == "#") {
apt = print[i + 2];
zip = print[i + 3];
}
if (re_zip.test(print[i + 2])) {
if (re_digit.test(print[i + 1])) {
apt = print[i + 1];
zip = print[i + 2];
}
} else {
zip = print[i + 1];
apt = "";
}
}
// find "tl/frst" field, first name "fname"
if (re_titlef.test(print[i])) {
if (re_title.test(print[i + 1])) {
title = print[i + 1];
if (print[i + 3] == "class") {
fname = print[i + 2];
} else {
fname = print[i + 2] + " " + print[i + 3];
}
} else {
fname = print[i + 1];
title = " ";
}
}
// find "int/lst" field, last name "lname", initial "initial"
// This bit was painstakingly written by the book: conceptualized, modeled, metacoded, coded
if (re_initialf.test(print[i])) {
if (re_ctype.test(print[i + 2])) {
lname = print[i + 1];
initial = "";
}
if (re_ctype.test(print[i + 4])) {
lname = print[i + 2] + " " + print[i + 3];
initial = print[i + 1];
}
if (re_ctype.test(print[i + 3])) {
if (re_initial.test(print[i + 1])) {
initial = print[i + 1];
lname = print[i + 2];
} else {
initial = "";
lname = print[i + 1] + " " + print[i + 2];
}
}
}
// find MAC ID
if (re_macids.test(print[i])) {
if (print[i].length < 12) {
macids[k] = print[i];
macarray[k] = SplitIt(macids[k]);
k++;
}
}
} // end for
var macout = "\r";
for (var x = 0; x < macarray.length; x++) {
macarray[x] = macarray[x].toLowerCase();
macout = "Modem MAC#: " + macarray[x] + "\r" + macout;
}
// format variables
var ELNfullaccount = corp + account + customer;
var fullaccount = corp + "-" + account + "-" + customer;
var name = fname + " " + lname; // make full name line from first and last names
name = toUpper(name); // Use toUpper function to fix formatting
var printline = "Account: " + fullaccount + "\r" + name; // make account line
printline = toUpper(printline);
if (nsew !== "") {
nsew = nsew + ".";
}
if (apt !== "") {
apt = "Apt. " + apt;
}
if (street_type !== "") {
street_type = street_type + "."; // add a period to the street type variable
}
var addline = house + " " + nsew + " " + street + " " + street_type + " " + apt + zip; // make address line from vars
addline = toUpper(addline);
// Print results to textarea in form
window.document.parserform.parse_window.value = "Account Information" + "\r\r" + "Customer Name: " + name + "\r" + "Account: " + fullaccount + "\r" + macout + "\r" + "Address: " + addline + "\r" + "Phone Number(s): " + phonen + "\r" + "\r\rEarthlink &amp; WebDT Format\r" + "Phone: " + phonen2 + "\r" + "Account: \r" + ELNfullaccount;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment