Answered
I know... it's a mess.
but the weeks i put into this has saved me months of unnecessary work.
I'm running into a problem lately though... sometimes when i input certain values, it gets cut off like half way through or right before the last character... it throws my whole operation off then =/
is there a way to speed this up? like maybe a good way to recycle variables?
var fld = this.getField("8_1"); var out = this.getField("9a_1"); var inn = fld.valueAsString; var fxtrm = (inn.slice(16, 20) + " " + inn.slice(20,24) + " " + inn.slice(24,28)); var fgtrm = (inn.slice(7,15) + " " + inn.slice(15,22)); var uspstrm = (inn.slice(0,2) + " " + inn.slice(2,5) + " " + inn.slice(5,8) + " " + inn.slice(8,11) + " " + inn.slice(11,13)); var uspsctrm = (inn.slice(0,4) + " " + inn.slice(4,8) + " " + inn.slice(8,12) + " " + inn.slice(12,16) + " " + inn.slice(16,20)); var browntrm = (inn.slice(0,2) + " " + inn.slice(2,5) + " " + inn.slice(5,8) + " " + inn.slice(8,10) + " " + inn.slice(10,14) + " " + inn.slice(14,18)); var dhltrm = (inn.slice(0,2) + " " + inn.slice(2,6) + " " + inn.slice(6,10)); var omtrm = (inn.slice(0,5) + " " + inn.slice(5,9) + " " + inn.slice(9,13) + " " + inn.slice(13,17) + " " + inn.slice(17,20)); var uspse = /^E\D{1}\d{9}\D{2}$/; var uspsr = /^R\D{1}\d{9}\D{2}$/; var uspsc = /^7\d{19}$/; var uspsi = /^V\D{1}\d{9}\D{2}$/; var fgreg = /^961[1-3](01[5-9]|02[0-1]|131|1[5-6][0-9]|3[0-9][0-9]|4[0-3][0-9]|44[0-4])\d{15}$/; var fxig = /^961[1-3](02[2-8]|135|141|20[0-3]|4[7-8][0-9]|49[0-3])d{15}$/; var fxgh = /^961[1-3][7-8][0-9][0-9]d{15}$/; var fxgr = /^961[1-3]900d{15}$/; var fxgsp = /^961[1-3](90[6-9]|91[0-8])d{15}$/; var fxso = /^3\d{7}5\d{23}$/; var fxpo = /^3\d{7}1\d{23}$/; var fx2 = /^3\d{7}3\d{23}$/; var fxs = /^3\d{7}2\d{23}$/; var fxfo = /^3\d{7}6\d{23}$/; var fxie = /^3\d{7}4\d{23}$/; var fxm = /^3\d{7}0\d{23}$/; var ugreg = /^1Z\w{6}(03|[247]2|78)\d{8}$/; var usda = /^1Z\w{6}02\d{8}$/; var unda = /^1Z\w{6}(01|13|15|32)\d{8}$/; var unaa = /^1Z\w{6}A[0129A]\d{8}$/; var ugas = /^1Z\w{6}A8\d{8}$/; var u3d = /^1Z\w{6}12\d{8}$/; var unas = /^1Z\w{6}(33|41|44)\d{8}$/; var uwe = /^1Z\w{6}66\d{8}$/; var ugr = /^1Z\w{6}90\d{8}$/; var de = /^\d{10}$/; var ome = /^00\d{18}$/; var upsv = /^1Z\w{6}D\d{9}$/; if (uspse.test(inn)){out.value = "USPS Express", fld.value = uspstrm} if (uspsr.test(inn)){out.value = "USPS Registered", fld.value = uspstrm} if (uspsc.test(inn)){out.value = "USPS Certified", fld.value = uspsctrm} if (uspsi.test(inn)){out.value = "USPS Insured", fld.value =uspstrm} if (fgreg.test(inn)){out.value = "FedEx Ground", fld.value = fgtrm} if (fxig.test(inn)){out.value = "FedEx Intl Ground", fld.value = fgtrm} if (fxgh.test(inn)){out.value = "FedEx Ground Home", fld.value = fgtrm} if (fxgr.test(inn)){out.value = "FedEx Ground Returns", fld.value = fgtrm} if (fxgsp.test(inn)){out.value = "FedEx Ground SmartPost", fld.value = fgtrm} if (fxso.test(inn)){out.value = "FedEx Standard Overnight", fld.value = fxtrm} if (fxpo.test(inn)){out.value = "FedEx Priority Overnight", fld.value = fxtrm} if (fx2.test(inn)){out.value = "FedEx 2-Day Express", fld.value = fxtrm} if (fxs.test(inn)){out.value = "FedEx Express Saver", fld.value = fxtrm} if (fxfo.test(inn)){out.value = "FedEx First Overnight", fld.value = fxtrm} if (fxie.test(inn)){out.value = "FedEx Intl Economy", fld.value = fxtrm} if (fxm.test(inn)){out.value = "FedEx Express Master", fld.value = fxtrm} if (ugreg.test(inn)){out.value = "UPS Ground", fld.value = browntrm} if (upsv.test(inn)){out.value = "UPS Saver", fld.value = browntrm} if (usda.test(inn)){out.value = "UPS 2nd Day Air", fld.value = browntrm} if (unda.test(inn)){out.value = "UPS Next Day Air", fld.value = browntrm} if (unaa.test(inn)){out.value = "UPS Next Day Air Adult", fld.value = browntrm} if (ugas.test(inn)){out.value = "UPS Ground Adult Sign", fld.value = browntrm} if (u3d.test(inn)){out.value = "UPS 3-Day Select", fld.value = browntrm} if (unas.test(inn)){out.value = "UPS Next Day Air Sat", fld.value = browntrm} if (uwe.test(inn)){out.value = "UPS Worldwide Express", fld.value = browntrm} if (ugr.test(inn)){out.value = "UPS Ground Returns", fld.value = browntrm} if (de.test(inn)){out.value = "DHL Express", fld.value = dhltrm} if (ome.test(inn)){out.value = "OfficeMax Express", fld.value = omtrm}
It seems like what you're doing is setting the value of two fields based on the value of another input field. There are 28 possibilities, and each possibility has an associated regular expression, a description, and another string constructed by inserting spaces to the input string.
I might be inclined to set up a 28 element array, with each value being either an object (for regexp, description, modified input) or 3 element array (for regexp, description, modified input). You could then set up a loop to go through the array and test each regular expression against the input, and output the corresponding description and modified input. You could also break out of the loop and stop if it's not necessary to test any subsequent regular expressions after you've found a match. If it's not, and you order the elements in the array according to most likely to occur, it should speed things up considerably.
e.g.,
// First ten lines of your original script go here
// Populate array
var a = [];
a.push([/^E\D{1}\d{9}\D{2}$/, "USPS Express", uspstrm]);
a.push([/^R\D{1}\d{9}\D{2}$/, "USPS Registered", uspstrm]);
... // Continue adding elements here
a.push([/^1Z\w{6}D\d{9}$/, "OfficeMax Express", omtrm]);
for (var i = 0; i < a.length; i += 1) {
if (a[i][0].test(inn) {
out.value = a[i][1];
fld.value = a[i][2];
break; // unless you need to continue
}
}
If you do something like this, you can prepopulate the first two items of each subarray when the document opens, so you don't need to do it more than once at runtime, which may speed things up. It's not clear if this code normally gets executed more than once in a session.
Good luck!