Code review Javascript :)

Hi all,

Below is my code.
Everything runs fine, but a I need to add a condition for this variable:

var AWBPlic = (f.AWBPlic);

*Condition: when reading this line *Condition: if the string does not contain “RI0” skip the whole line of text from the output file.

Any ideas?

Thanks,
fsh22

var origFileName = get(“fisiere”);

origFileName = origFileName.substring(0, origFileName.lastIndexOf(‘.’));

var fileLocation = get(“global.workDir”) + “\retur_csv\” + origFileName + “_” + get(“id_comanda”) + “_retur.csv”
var csvHeader = ‘letter_queue_id|awb|CEID|Client_name|letter_type|Curier|’;//cap de tabel pentru nr 3. infocod EOS STANDARD
var records = JSON.parse(Watch.ExpandString(“%c”));

createFile(fileLocation, csvHeader);

for (var i = 0; i < records.length; i++) {
// fields to export

var f = records[i].fields; // keep things clean.
// wrap the export in quotes
var letterQueue = (f.COLUMN1);
//var cheie_secundara = wrappInQuotes(f.cheie_secundara);
var AWBPlic = (f.AWBPlic);
var CEID = (f.COLUMN15);
var clientName = (f.COLUMN23);
var letterType = (f.COLUMN44);
var curier = "Posta Romana"
// Create a line of text joined with the pipe character
var lineOfText = [letterQueue, AWBPlic, CEID, clientName, letterType, curier].join('|');
// write the line of text to the fileOut
appendFile(fileLocation, lineOfText);
//go to newline for new text

};

var outputFile = Watch.SetVariable(“fileName”, fileLocation);

/* Helpers */
function appendFile(path, content) {
var fs = new ActiveXObject(‘Scripting.FileSystemObject’)
var file = fs.OpenTextFile(path, 8, true)
file.WriteLine(content)
file.Close()
}

function get(name) { return typeof name === ‘number’ ? Watch.getJobInfo(name) : Watch.getVariable(name) }
function set(name, value) { typeof name === ‘number’ ? Watch.setJobInfo(name, value) : Watch.setVariable(name, value) }
function log(msg) { try { Watch.log(toString(msg), 2) } catch(e) { WScript.stdout.WriteLine(toString(msg)) } }
function err(msg) { try { Watch.log(toString(msg), 1) } catch(e) { WScript.stdout.WriteLine(toString(msg)) } }
function exp(string) { return Watch.expandString(string); }
function xml(string) { return Watch.expandString(“xmlget('/request[1]/values[1]/” + string + “[1]',Value,KeepCase,No Trim)”); }
function toString(value) { try { return JSON.stringify(value) } catch(e) { return ‘’+value } }

function format() {
var as = arguments
return as[0].replace(/{ *(\w+) *}/g,
function(m, i) { return as[i] })
}

function sanitizeFilename(filename) {
return filename.replace(//|\|:|?|*|"|||<|>/g, ‘-’)
}

function writeFile(path, content){
var fs = new ActiveXObject(‘Scripting.FileSystemObject’)
var file = fs.OpenTextFile(path, 2, true)
file.Write(content)
file.Close()
}

function createFile(path, content){
var fs = new ActiveXObject(“Scripting.FileSystemObject”)
var file = fs.CreateTextFile(path)
file.WriteLine(content)
file.Close()
}

/**

  • helper function to wrap text in quotes for escaping
  • @param {string} text
    */
    //function wrappInQuotes(text) {
    // return ‘"’ + text + ‘"’
    //};

There are 2 ways you can do this:

Method 1
Replace this line of code:

  appendFile(fileLocation, lineOfText);

with

  if(AWBPlic == 'RI0') appendFile(fileLocation, lineOfText);

Method 2
Add a new line of code immediately below the line you quoted

var AWBPlic = (f.AWBPlic);
if(AWBPlic != 'RI0') continue;  // This immediately moves to the next item in the loop

Hi Phil thanks for reply,

I’ve tested but I`ve noticed I made a mistake explaining my condition . I will modify my initial post.

var AWBPlic = (f.AWBPlic);

Condition: if the string does not contain “RI0” skip the whole line of text from the output file.

My mistake!
Many thanks in advance!

Well that’s what I understood initially, and that’s what both methods do.

  • The first one only appends the line to the file if AWBPlic contains RI0
  • The second one simply skips to the next line without doing anything if AWBPlic does not contain RI0.

Not my best day, for sure! :)))

The first one, I`ve tested and the output file was completely empty, only the header line has been outputted.

The second method not yet tested.

Cheers!

Well that means that none of the lines have AWBPlic set to RI0 (by the way, that’s “r” and “i” in capitals, followed by the number “0”). Maybe you meant RL0 or RLO or RIO … In any event, just make sure to compare the proper value.

R I 0 is what I meant.
Almost every string(value) that comes into (f.AWBPlic) starts with RI0.
During my processing I have some records, that don’t receive any values into (f.AWBplic), hence when I`m generating my report I want to skip these records, that have an empty value into (f.AWBplic.)

Perhaps the values you extracted contain some spaces or are not in the proper case. Trimming and converting to lowercase might solve the issue.

For method 1:

if(AWBPlic.trim().toLowerCase() == 'ri0') appendFile(fileLocation, lineOfText);

or for method 2

if(AWBPlic.trim().toLowerCase() != 'ri0') continue;

Hi Phil,

As always, thanks for your help.
I’m going to analyze my data and my script, because I haven`t found a way to reach my desired outcome.

Regards,
fsh22