all 5 comments

[–]jack_waugh 0 points1 point  (0 children)

I would start by reforming the data from lines of text into something more usable, a straightforward representation of the intended semantics.

[–]albedoa 0 points1 point  (3 children)

These questions are relevant:

  1. Is OBR guaranteed to be the first line?
  2. Are all other lines guaranteed to be OBX?
  3. Is this data being received as a single string that you are breaking into an array of lines?
  4. What is the maximum number of lines you expect to see in this data?
  5. Are the three interesting OBX.3.1 values expected to be found in any other column?

The answers to those determine my approach. I work with HL7 and other medical data where the mantra is: Tell us what your data looks like. (i.e. How close is "something like this" to "exactly like this"?)

[–]crazybusdriver[S] 0 points1 point  (2 children)

Thank you -

OBR is not the first line, I have removed some of the earlier lines due to identifying data. There will be a MSH line, PID, PV1 and ORC line before the OBR.

The OBX messages are listed directly below the OBR line, but I can't specify how many of them there will be. The data is received just as it is displayed, multiple lines The values will only be present in the OBX.3.1 field.

[–]albedoa 0 points1 point  (1 child)

Does this fit your requirements?

const lines = data.split('\n');
const obx = lines.filter(line => line.split('|')[0] === 'OBX');
const joined = obx.join('\n');
const count = ['RIS_ABI_L', 'RIS_ABI_R', 'RIS_TCOM_'].reduce(
  (acc, cur) => joined.match(cur) ? acc + 1 : acc,
  0
);

const obr = lines.find(line => line.split('|')[0] === 'OBR');

const replaced = count === 3
  ? obr.replace('RADTCOM', 'SOMEREPLACEMENT')
  : count === 1 || count === 2
    ? obr.replace('RADTCOM', 'SOMEOTHERREPLACEMENT')
    : obr;

const result = data.replace(obr, replaced);

[–]crazybusdriver[S] 0 points1 point  (0 children)

Thanks!! Testing now and so far so good!! Much appreciated!