Olugbnega Oyeneye
asked on
Is there a way to optimise this code?
/**
Is there a way to optimise this code by
1. looping instead of the long repetition.
2. Also, to display nothing (instead of string 'NHS Number') when 'NHSIDNumber' is null
----
When NHS is 'not null':
|abc9874^^^MRN|1234567890^ ^^NHS Number|
----
When NHS is null:
Correct Display Should be:
|abc9874^^^MRN||
Wrong Display:
abc9874^^^MRN|^^^NHS Number|
**/
var next = output.append(input[0]);
var body = next.text;
var type;
if(body.indexOf("PV1|") > 0)
{
next.setProperty("PatientC lass",next .getField( "PV1.Patie ntClass")) ;
}
//*
//Start SWAP NHS + PASID
//*
var sPID = "PID."; //default to 'normal'
// Get the number of items in the array
var count = next.getRepeatCount(sPID + "PatientIdentifierList");
next.setProperty("pCount", count);
var NHSIDNumber = "";
var PASIDNumber = "";
//var PASAA = "";
//var PASAF = "";
if (count > 0)
{
for(var i = 0; i < count; i ++)
{
// Get the field's current value
if (next.getField(sPID + "PatientIdentifierList["+ i +"].IdentifierTypeCode") == "NHS")
{
NHSIDNumber = next.getField(sPID + "PatientIdentifierList["+ i +"].IDNumber")
next.setProperty("NHSID", NHSIDNumber);
}
else if (next.getField(sPID + "PatientIdentifierList["+ i +"].IdentifierTypeCode") == "FAH")
{
PASIDNumber = next.getField(sPID + "PatientIdentifierList["+ i +"].IDNumber")
//PASAA = next.getField(sPID + "PatientIdentifierList["+ i +"].AssigningAuthority.Nam espaceID") ;
//PASAF = next.getField(sPID + "PatientIdentifierList["+ i +"].AssigningFacility.Name spaceID");
next.setProperty("PASID", PASIDNumber);
}
}
next.setField(sPID + "PatientIdentifierList[0]. IDNumber", PASIDNumbe r);
next.setField(sPID + "PatientIdentifierList[0]. AssigningA uthority.N amespaceID ", "MRN");
next.setField(sPID + "PatientIdentifierList[0]. Identifier TypeCode", '');
//next.setField(sPID + "PatientIdentifierList[0]. AssigningA uthority.N amespaceID ",PASAA);
//next.setField(sPID + "PatientIdentifierList[0]. AssigningF acility.Na mespaceID" ,PASAF);
next.setField(sPID + "AlternatePatientID[0].IDN umber",NHS IDNumber);
next.setField(sPID + "AlternatePatientID[0].Ass igningAuth ority.Name spaceID", "NHS Number");
next.setField(sPID + "AlternatePatientID[0].Ide ntifierTyp eCode", '');
//next.setField(sPID + "AlternatePatientID[0].Ide ntifierTyp eCode.Name spaceID",' ');
//next.setField(sPID + "AlternatePatientID[0].Ide ntifierTyp eCode.Name spaceID",' ');
next.setField(sPID + "PatientIdentifierList[1]. IDNumber", '');
next.setField(sPID + "PatientIdentifierList[1]. Identifier TypeCode", '');
next.setField(sPID + "PatientIdentifierList[1]. AssigningA uthority.N amespaceID ", '');
next.setField(sPID + "PatientIdentifierList[1]. AssigningF acility.Na mespaceID" , '');
next.setField(sPID + "PatientIdentifierList[2]. IDNumber", '');
next.setField(sPID + "PatientIdentifierList[2]. Identifier TypeCode", '');
next.setField(sPID + "PatientIdentifierList[2]. AssigningA uthority.N amespaceID ", '');
next.setField(sPID + "PatientIdentifierList[2]. AssigningF acility.Na mespaceID" , '');
next.setField(sPID + "PatientIdentifierList[3]. IDNumber", '');
next.setField(sPID + "PatientIdentifierList[3]. Identifier TypeCode", '');
next.setField(sPID + "PatientIdentifierList[3]. AssigningA uthority.N amespaceID ", '');
next.setField(sPID + "PatientIdentifierList[3]. AssigningF acility.Na mespaceID" , '');
next.setField(sPID + "PatientIdentifierList[4]. IDNumber", '');
next.setField(sPID + "PatientIdentifierList[4]. Identifier TypeCode", '');
next.setField(sPID + "PatientIdentifierList[4]. AssigningA uthority.N amespaceID ", '');
next.setField(sPID + "PatientIdentifierList[4]. AssigningF acility.Na mespaceID" , '');
next.setField(sPID + "PatientIdentifierList[5]. IDNumber", '');
next.setField(sPID + "PatientIdentifierList[5]. Identifier TypeCode", '');
next.setField(sPID + "PatientIdentifierList[5]. AssigningA uthority.N amespaceID ", '');
next.setField(sPID + "PatientIdentifierList[5]. AssigningF acility.Na mespaceID" , '');
next.setField(sPID + "PatientIdentifierList[6]. IDNumber", '');
next.setField(sPID + "PatientIdentifierList[6]. Identifier TypeCode", '');
next.setField(sPID + "PatientIdentifierList[6]. AssigningA uthority.N amespaceID ", '');
next.setField(sPID + "PatientIdentifierList[6]. AssigningF acility.Na mespaceID" , '');
next.setField(sPID + "PatientIdentifierList[7]. IDNumber", '');
next.setField(sPID + "PatientIdentifierList[7]. Identifier TypeCode", '');
next.setField(sPID + "PatientIdentifierList[7]. AssigningA uthority.N amespaceID ", '');
next.setField(sPID + "PatientIdentifierList[7]. AssigningF acility.Na mespaceID" , '');
next.setField(sPID + "PatientIdentifierList[8]. IDNumber", '');
next.setField(sPID + "PatientIdentifierList[8]. Identifier TypeCode", '');
next.setField(sPID + "PatientIdentifierList[8]. AssigningA uthority.N amespaceID ", '');
next.setField(sPID + "PatientIdentifierList[8]. AssigningF acility.Na mespaceID" , '');
next.setField(sPID + "PatientIdentifierList[9]. IDNumber", '');
next.setField(sPID + "PatientIdentifierList[9]. Identifier TypeCode", '');
next.setField(sPID + "PatientIdentifierList[9]. AssigningA uthority.N amespaceID ", '');
next.setField(sPID + "PatientIdentifierList[9]. AssigningF acility.Na mespaceID" , '');
next.setField(sPID + "PatientIdentifierList[10] .IDNumber" ,'');
next.setField(sPID + "PatientIdentifierList[10] .Identifie rTypeCode" , '');
next.setField(sPID + "PatientIdentifierList[10] .Assigning Authority. NamespaceI D", '');
next.setField(sPID + "PatientIdentifierList[10] .Assigning Facility.N amespaceID ", '');
}
//**
//*End SWAP NHS + PASID
//**
Is there a way to optimise this code by
1. looping instead of the long repetition.
2. Also, to display nothing (instead of string 'NHS Number') when 'NHSIDNumber' is null
----
When NHS is 'not null':
|abc9874^^^MRN|1234567890^
----
When NHS is null:
Correct Display Should be:
|abc9874^^^MRN||
Wrong Display:
abc9874^^^MRN|^^^NHS Number|
**/
var next = output.append(input[0]);
var body = next.text;
var type;
if(body.indexOf("PV1|") > 0)
{
next.setProperty("PatientC
}
//*
//Start SWAP NHS + PASID
//*
var sPID = "PID."; //default to 'normal'
// Get the number of items in the array
var count = next.getRepeatCount(sPID + "PatientIdentifierList");
next.setProperty("pCount",
var NHSIDNumber = "";
var PASIDNumber = "";
//var PASAA = "";
//var PASAF = "";
if (count > 0)
{
for(var i = 0; i < count; i ++)
{
// Get the field's current value
if (next.getField(sPID + "PatientIdentifierList["+ i +"].IdentifierTypeCode") == "NHS")
{
NHSIDNumber = next.getField(sPID + "PatientIdentifierList["+ i +"].IDNumber")
next.setProperty("NHSID", NHSIDNumber);
}
else if (next.getField(sPID + "PatientIdentifierList["+ i +"].IdentifierTypeCode") == "FAH")
{
PASIDNumber = next.getField(sPID + "PatientIdentifierList["+ i +"].IDNumber")
//PASAA = next.getField(sPID + "PatientIdentifierList["+ i +"].AssigningAuthority.Nam
//PASAF = next.getField(sPID + "PatientIdentifierList["+ i +"].AssigningFacility.Name
next.setProperty("PASID", PASIDNumber);
}
}
next.setField(sPID + "PatientIdentifierList[0].
next.setField(sPID + "PatientIdentifierList[0].
next.setField(sPID + "PatientIdentifierList[0].
//next.setField(sPID + "PatientIdentifierList[0].
//next.setField(sPID + "PatientIdentifierList[0].
next.setField(sPID + "AlternatePatientID[0].IDN
next.setField(sPID + "AlternatePatientID[0].Ass
next.setField(sPID + "AlternatePatientID[0].Ide
//next.setField(sPID + "AlternatePatientID[0].Ide
//next.setField(sPID + "AlternatePatientID[0].Ide
next.setField(sPID + "PatientIdentifierList[1].
next.setField(sPID + "PatientIdentifierList[1].
next.setField(sPID + "PatientIdentifierList[1].
next.setField(sPID + "PatientIdentifierList[1].
next.setField(sPID + "PatientIdentifierList[2].
next.setField(sPID + "PatientIdentifierList[2].
next.setField(sPID + "PatientIdentifierList[2].
next.setField(sPID + "PatientIdentifierList[2].
next.setField(sPID + "PatientIdentifierList[3].
next.setField(sPID + "PatientIdentifierList[3].
next.setField(sPID + "PatientIdentifierList[3].
next.setField(sPID + "PatientIdentifierList[3].
next.setField(sPID + "PatientIdentifierList[4].
next.setField(sPID + "PatientIdentifierList[4].
next.setField(sPID + "PatientIdentifierList[4].
next.setField(sPID + "PatientIdentifierList[4].
next.setField(sPID + "PatientIdentifierList[5].
next.setField(sPID + "PatientIdentifierList[5].
next.setField(sPID + "PatientIdentifierList[5].
next.setField(sPID + "PatientIdentifierList[5].
next.setField(sPID + "PatientIdentifierList[6].
next.setField(sPID + "PatientIdentifierList[6].
next.setField(sPID + "PatientIdentifierList[6].
next.setField(sPID + "PatientIdentifierList[6].
next.setField(sPID + "PatientIdentifierList[7].
next.setField(sPID + "PatientIdentifierList[7].
next.setField(sPID + "PatientIdentifierList[7].
next.setField(sPID + "PatientIdentifierList[7].
next.setField(sPID + "PatientIdentifierList[8].
next.setField(sPID + "PatientIdentifierList[8].
next.setField(sPID + "PatientIdentifierList[8].
next.setField(sPID + "PatientIdentifierList[8].
next.setField(sPID + "PatientIdentifierList[9].
next.setField(sPID + "PatientIdentifierList[9].
next.setField(sPID + "PatientIdentifierList[9].
next.setField(sPID + "PatientIdentifierList[9].
next.setField(sPID + "PatientIdentifierList[10]
next.setField(sPID + "PatientIdentifierList[10]
next.setField(sPID + "PatientIdentifierList[10]
next.setField(sPID + "PatientIdentifierList[10]
}
//**
//*End SWAP NHS + PASID
//**
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
//This is solved:
//=============
/* Single input message case */
// Create the output message
var next = output.append(input[0]);
var body = next.text;
var type;
if(body.indexOf("PV1|") > 0)
{
next.setProperty("PatientC lass",next .getField( "PV1.Patie ntClass")) ;
}
//*
//Start SWAP NHS + PASID
//*
var sPID = "PID."; //default to 'normal'
//if (next.getField("MSH.Messag eType.Trig gerEvent") == "A40")
//{
// sPID = "PatientInformation[0].PID ."
//}
// Get the number of items in the array
var count = next.getRepeatCount(sPID + "PatientIdentifierList");
next.setProperty("pCount", count);
var NHSIDNumber = "";
var PASIDNumber = "";
//var PASAA = "";
//var PASAF = "";
if (count > 0)
{
for(var i = 0; i < count; i ++)
{
// Get the field's current value
if (next.getField(sPID + "PatientIdentifierList["+ i +"].IdentifierTypeCode") == "NHS")
{
NHSIDNumber = next.getField(sPID + "PatientIdentifierList["+ i +"].IDNumber")
next.setProperty("NHSID", NHSIDNumber);
}
else if (next.getField(sPID + "PatientIdentifierList["+ i +"].IdentifierTypeCode") == "FACIL")
{
PASIDNumber = next.getField(sPID + "PatientIdentifierList["+ i +"].IDNumber")
//PASAA = next.getField(sPID + "PatientIdentifierList["+ i +"].AssigningAuthority.Nam espaceID") ;
//PASAF = next.getField(sPID + "PatientIdentifierList["+ i +"].AssigningFacility.Name spaceID");
next.setProperty("PASID", PASIDNumber);
}
}
next.setField(sPID + "PatientIdentifierList[0]. IDNumber", PASIDNumbe r);
next.setField(sPID + "PatientIdentifierList[0]. AssigningA uthority.N amespaceID ", "MRN");
next.setField(sPID + "PatientIdentifierList[0]. Identifier TypeCode", '');
//next.setField(sPID + "PatientIdentifierList[0]. AssigningA uthority.N amespaceID ",PASAA);
//next.setField(sPID + "PatientIdentifierList[0]. AssigningF acility.Na mespaceID" ,PASAF);
if (NHSIDNumber == null)
{
next.setField(sPID + "AlternatePatientID[0].IDN umber",'') ;
next.setField(sPID + "AlternatePatientID[0].Ass igningAuth ority.Name spaceID",' ' );
next.setField(sPID + "AlternatePatientID[0].Ide ntifierTyp eCode", '');
//next.setField(sPID + "AlternatePatientID[0].Ide ntifierTyp eCode.Name spaceID",' ');
//next.setField(sPID + "AlternatePatientID[0].Ide ntifierTyp eCode.Name spaceID",' ');
}
else
{
next.setField(sPID + "AlternatePatientID[0].IDN umber",NHS IDNumber);
next.setField(sPID + "AlternatePatientID[0].Ass igningAuth ority.Name spaceID", "NHS Number");
next.setField(sPID + "AlternatePatientID[0].Ide ntifierTyp eCode", '');
//next.setField(sPID + "AlternatePatientID[0].Ide ntifierTyp eCode.Name spaceID",' ');
//next.setField(sPID + "AlternatePatientID[0].Ide ntifierTyp eCode.Name spaceID",' ');
}
var ii = 1;
while (ii <= i) {
next.setField(sPID + "PatientIdentifierList[" + ii +"].IDNumber",'');
next.setField(sPID + "PatientIdentifierList[" + ii +"].IdentifierTypeCode", '');
next.setField(sPID + "PatientIdentifierList[" + ii +"].AssigningAuthority.Nam espaceID", '');
next.setField(sPID + "PatientIdentifierList[" + ii +"].AssigningFacility.Name spaceID", '');
ii++;
}
//**
//*End SWAP NHS + PASID
//**
}
//=============
/* Single input message case */
// Create the output message
var next = output.append(input[0]);
var body = next.text;
var type;
if(body.indexOf("PV1|") > 0)
{
next.setProperty("PatientC
}
//*
//Start SWAP NHS + PASID
//*
var sPID = "PID."; //default to 'normal'
//if (next.getField("MSH.Messag
//{
// sPID = "PatientInformation[0].PID
//}
// Get the number of items in the array
var count = next.getRepeatCount(sPID + "PatientIdentifierList");
next.setProperty("pCount",
var NHSIDNumber = "";
var PASIDNumber = "";
//var PASAA = "";
//var PASAF = "";
if (count > 0)
{
for(var i = 0; i < count; i ++)
{
// Get the field's current value
if (next.getField(sPID + "PatientIdentifierList["+ i +"].IdentifierTypeCode") == "NHS")
{
NHSIDNumber = next.getField(sPID + "PatientIdentifierList["+ i +"].IDNumber")
next.setProperty("NHSID", NHSIDNumber);
}
else if (next.getField(sPID + "PatientIdentifierList["+ i +"].IdentifierTypeCode") == "FACIL")
{
PASIDNumber = next.getField(sPID + "PatientIdentifierList["+ i +"].IDNumber")
//PASAA = next.getField(sPID + "PatientIdentifierList["+ i +"].AssigningAuthority.Nam
//PASAF = next.getField(sPID + "PatientIdentifierList["+ i +"].AssigningFacility.Name
next.setProperty("PASID", PASIDNumber);
}
}
next.setField(sPID + "PatientIdentifierList[0].
next.setField(sPID + "PatientIdentifierList[0].
next.setField(sPID + "PatientIdentifierList[0].
//next.setField(sPID + "PatientIdentifierList[0].
//next.setField(sPID + "PatientIdentifierList[0].
if (NHSIDNumber == null)
{
next.setField(sPID + "AlternatePatientID[0].IDN
next.setField(sPID + "AlternatePatientID[0].Ass
next.setField(sPID + "AlternatePatientID[0].Ide
//next.setField(sPID + "AlternatePatientID[0].Ide
//next.setField(sPID + "AlternatePatientID[0].Ide
}
else
{
next.setField(sPID + "AlternatePatientID[0].IDN
next.setField(sPID + "AlternatePatientID[0].Ass
next.setField(sPID + "AlternatePatientID[0].Ide
//next.setField(sPID + "AlternatePatientID[0].Ide
//next.setField(sPID + "AlternatePatientID[0].Ide
}
var ii = 1;
while (ii <= i) {
next.setField(sPID + "PatientIdentifierList[" + ii +"].IDNumber",'');
next.setField(sPID + "PatientIdentifierList[" + ii +"].IdentifierTypeCode", '');
next.setField(sPID + "PatientIdentifierList[" + ii +"].AssigningAuthority.Nam
next.setField(sPID + "PatientIdentifierList[" + ii +"].AssigningFacility.Name
ii++;
}
//**
//*End SWAP NHS + PASID
//**
}
ASKER
It is HL7 messaging., using java script to transform it and the message is in transit.
var ii = 1;
while (ii <= i) {
next.setField(sPID + "PatientIdentifierList[" + ii +"].IDNumber",'');
next.setField(sPID + "PatientIdentifierList[" + ii +"].IdentifierTypeCode", '');
next.setField(sPID + "PatientIdentifierList[" + ii +"].AssigningAuthority.Nam
next.setField(sPID + "PatientIdentifierList[" + ii +"].AssigningFacility.Name
ii++;
}