Solved

Is there a way to optimise this code?

Posted on 2016-10-20
3
29 Views
Last Modified: 2016-10-20
/**
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("PatientClass",next.getField("PV1.PatientClass"));
}

//*
//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.NamespaceID");
                  //PASAF = next.getField(sPID + "PatientIdentifierList["+ i +"].AssigningFacility.NamespaceID");
                  next.setProperty("PASID", PASIDNumber);
            }                  
      }            
      
            
      next.setField(sPID + "PatientIdentifierList[0].IDNumber",PASIDNumber);
      next.setField(sPID + "PatientIdentifierList[0].AssigningAuthority.NamespaceID", "MRN");
      next.setField(sPID + "PatientIdentifierList[0].IdentifierTypeCode", '');
      //next.setField(sPID + "PatientIdentifierList[0].AssigningAuthority.NamespaceID",PASAA);
      //next.setField(sPID + "PatientIdentifierList[0].AssigningFacility.NamespaceID",PASAF);
            
      
      next.setField(sPID + "AlternatePatientID[0].IDNumber",NHSIDNumber);
      next.setField(sPID + "AlternatePatientID[0].AssigningAuthority.NamespaceID", "NHS Number");
      next.setField(sPID + "AlternatePatientID[0].IdentifierTypeCode", '');
      //next.setField(sPID + "AlternatePatientID[0].IdentifierTypeCode.NamespaceID",'');
      //next.setField(sPID + "AlternatePatientID[0].IdentifierTypeCode.NamespaceID",'');
      
      next.setField(sPID + "PatientIdentifierList[1].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[1].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[1].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[1].AssigningFacility.NamespaceID", '');
      
      next.setField(sPID + "PatientIdentifierList[2].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[2].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[2].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[2].AssigningFacility.NamespaceID", '');
      
      next.setField(sPID + "PatientIdentifierList[3].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[3].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[3].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[3].AssigningFacility.NamespaceID", '');
      
      next.setField(sPID + "PatientIdentifierList[4].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[4].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[4].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[4].AssigningFacility.NamespaceID", '');
      
      next.setField(sPID + "PatientIdentifierList[5].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[5].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[5].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[5].AssigningFacility.NamespaceID", '');
      
      next.setField(sPID + "PatientIdentifierList[6].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[6].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[6].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[6].AssigningFacility.NamespaceID", '');
      
      next.setField(sPID + "PatientIdentifierList[7].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[7].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[7].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[7].AssigningFacility.NamespaceID", '');
      
      next.setField(sPID + "PatientIdentifierList[8].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[8].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[8].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[8].AssigningFacility.NamespaceID", '');
      
      next.setField(sPID + "PatientIdentifierList[9].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[9].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[9].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[9].AssigningFacility.NamespaceID", '');
      
      next.setField(sPID + "PatientIdentifierList[10].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[10].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[10].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[10].AssigningFacility.NamespaceID", '');
}      
//**
//*End SWAP      NHS + PASID
//**
0
Comment
Question by:Olugbnega Oyeneye
  • 2
3 Comments
 
LVL 21

Accepted Solution

by:
Kim Walker earned 500 total points
Comment Utility
Be sure to always select all your code and click the CODE button above the comment/question window so it will be displayed in a format that is easy to parse.

It's difficult to say if anything can be optimized without knowing what is returned by output.append(input[0]);. I'm guessing output is a custom object.

But the following long repeat code is easily replaced with a loop
      next.setField(sPID + "PatientIdentifierList[1].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[1].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[1].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[1].AssigningFacility.NamespaceID", '');
      
      ... 

      next.setField(sPID + "PatientIdentifierList[10].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[10].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[10].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[10].AssigningFacility.NamespaceID", '');

Open in new window

Can be reduced to
      var ii = 1;
      while (ii <= 10) {
          next.setField(sPID + "PatientIdentifierList[" + ii +"].IDNumber",'');
          next.setField(sPID + "PatientIdentifierList[" + ii +"].IdentifierTypeCode", '');
          next.setField(sPID + "PatientIdentifierList[" + ii +"].AssigningAuthority.NamespaceID", '');
          next.setField(sPID + "PatientIdentifierList[" + ii +"].AssigningFacility.NamespaceID", '');
          ii++;
      }

Open in new window

2. I don't see anything in this code that generates any output -- certainly not the string that ends with "NHS Number."
0
 

Author Comment

by:Olugbnega Oyeneye
Comment Utility
Yeah, that solved that bit. Thank you.
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.NamespaceID", '');
          next.setField(sPID + "PatientIdentifierList[" + ii +"].AssigningFacility.NamespaceID", '');
          ii++;
      }
0
 

Author Comment

by:Olugbnega Oyeneye
Comment Utility
//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("PatientClass",next.getField("PV1.PatientClass"));
}

//*
//Start SWAP NHS + PASID
//*

var sPID = "PID."; //default to 'normal'

//if (next.getField("MSH.MessageType.TriggerEvent") == "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.NamespaceID");
                  //PASAF = next.getField(sPID + "PatientIdentifierList["+ i +"].AssigningFacility.NamespaceID");
                  next.setProperty("PASID", PASIDNumber);
            }                  
      }            
      
            
      next.setField(sPID + "PatientIdentifierList[0].IDNumber",PASIDNumber);
      next.setField(sPID + "PatientIdentifierList[0].AssigningAuthority.NamespaceID", "MRN");
      next.setField(sPID + "PatientIdentifierList[0].IdentifierTypeCode", '');
      //next.setField(sPID + "PatientIdentifierList[0].AssigningAuthority.NamespaceID",PASAA);
      //next.setField(sPID + "PatientIdentifierList[0].AssigningFacility.NamespaceID",PASAF);
            
      
      
            if (NHSIDNumber == null)
      {      
      next.setField(sPID + "AlternatePatientID[0].IDNumber",'');
      next.setField(sPID + "AlternatePatientID[0].AssigningAuthority.NamespaceID",'' );
      next.setField(sPID + "AlternatePatientID[0].IdentifierTypeCode", '');
      //next.setField(sPID + "AlternatePatientID[0].IdentifierTypeCode.NamespaceID",'');
      //next.setField(sPID + "AlternatePatientID[0].IdentifierTypeCode.NamespaceID",'');
      }
      else
      {
      next.setField(sPID + "AlternatePatientID[0].IDNumber",NHSIDNumber);
      next.setField(sPID + "AlternatePatientID[0].AssigningAuthority.NamespaceID", "NHS Number");
      next.setField(sPID + "AlternatePatientID[0].IdentifierTypeCode", '');
      //next.setField(sPID + "AlternatePatientID[0].IdentifierTypeCode.NamespaceID",'');
      //next.setField(sPID + "AlternatePatientID[0].IdentifierTypeCode.NamespaceID",'');
      }
      


 var ii = 1;
      while (ii <= i) {
          next.setField(sPID + "PatientIdentifierList[" + ii +"].IDNumber",'');
          next.setField(sPID + "PatientIdentifierList[" + ii +"].IdentifierTypeCode", '');
          next.setField(sPID + "PatientIdentifierList[" + ii +"].AssigningAuthority.NamespaceID", '');
          next.setField(sPID + "PatientIdentifierList[" + ii +"].AssigningFacility.NamespaceID", '');
          ii++;
      }
      

      
//**
//*End SWAP      NHS + PASID
//**
}
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now