Skip to content

Commit

Permalink
feat: batch and message
Browse files Browse the repository at this point in the history
- creating and parsing is complete
- a few unit tests are failing
- removed parser.ts and parser.test.ts file now that the code has been migrated over
  • Loading branch information
Bugs5382 committed Dec 9, 2023
1 parent 22b7f83 commit 275ce6e
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 232 deletions.
52 changes: 50 additions & 2 deletions __tests__/hl7.build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,12 @@ describe('node hl7 client - builder tests', () => {
describe('parse message and batch', () => {

const hl7_string: string = "MSH|^~\\&|||||20081231||ADT^A01^ADT_A01|12345||2.7\rEVN||20081231"
const hl7_non_standard: string = "MSH:-+?*:field2:field3component1-field3component2:field4repeat1+field4repeat2:field5subcomponent1*field5subcomponent2:field6?R?"
const hl7_batch: string = "BHS|^~\\&|||||20231208\rMSH|^~\\&|||||20231208||ADT^A01^ADT_A01|CONTROL_ID||2.7\rEVN||20081231\rEVN||20081231\rBTS|1"
const hl7_batch_non_standard: string = "BHS:-+?*:::::20231208\rMSH|^~\\&|||||20231208||ADT^A01^ADT_A01|CONTROL_ID||2.7\rEVN||20081231\rEVN||20081231\rBTS|1"
// const hl7_batch_non_standard_mixed: string = "BHS:-+?*:::::20231208\rMSH|^~\\&|||||20231208||ADT^A01^ADT_A01|CONTROL_ID||2.7\rEVN||20081231\rEVN||20081231\rBTS|1"

test('...verify input', async () => {
test('...verify MSH input', async () => {

const message = new Message({text: hl7_string})

Expand All @@ -541,11 +545,20 @@ describe('node hl7 client - builder tests', () => {
expect(message.get('MSH.10').toString()).toBe("12345")
expect(message.get('MSH.12').toString()).toBe("2.7")
expect(message.get('EVN.2').toString()).toBe("20081231")

let count: number = 0
message.get("EVN").forEach((segment: Node): void => {
expect(segment.name).toBe( "EVN");
count++;
});

expect(count).toBe(1)

})

test('...parse non standard delimiters defined in MSH header', async () => {

const message = new Message({text: "MSH:-+?*:field2:field3component1-field3component2:field4repeat1+field4repeat2:field5subcomponent1*field5subcomponent2:field6?R?"});
const message = new Message({text: hl7_non_standard});

expect(message.get("MSH.3").toString()).toBe( "field2");
expect(message.get("MSH.4.2").toString()).toBe( "field3component2");
Expand All @@ -554,6 +567,41 @@ describe('node hl7 client - builder tests', () => {
expect(message.get("MSH.6.1").toString()).toBe( "field5subcomponent1*field5subcomponent2");
expect(message.get("MSH.6.1.2").toString()).toBe( "field5subcomponent2");
expect(message.get("MSH.7").toString()).toBe( "field6+");
})

test('...verify BHS input', async () => {

const batch = new Batch({text: hl7_batch})

expect(batch.toString().substring(0, 8)).toBe("BHS|^~\\&")
expect(batch.get('BHS.1').toString()).toBe("|")
expect(batch.get('BHS.2').toString()).toBe("^~\\&")
})

test('...parse non standard delimiters defined in BHS header', async () => {

const batch = new Batch({text: hl7_batch_non_standard})

expect(batch.toString().substring(0, 8)).toBe("BHS:-+?*")
expect(batch.get('BHS.1').toString()).toBe(":")
expect(batch.get('BHS.2').toString()).toBe("-+?*")
})

test('...verify BHS input ... 1 message should exist ... 2 EVN segments inside', async () => {

const batch = new Batch({text: hl7_batch})
const messages = batch.messages()
expect(messages.length).toBe(1)

messages.forEach((message: Message): void => {

let count: number = 0
message.get("EVN").forEach((segment: Node): void => {
expect(segment.name).toBe( "EVN");
count++;
});
expect(count).toBe(2)
})

})

Expand Down
101 changes: 0 additions & 101 deletions __tests__/hl7.parse.test.ts

This file was deleted.

61 changes: 60 additions & 1 deletion src/builder/batch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Util from '../utils'
import { HL7FatalError } from '../utils/exception'
import { HL7FatalError, HL7ParserError } from '../utils/exception'
import {
ClientBuilderBatchOptions,
normalizedClientBatchBuilderOptions
Expand All @@ -18,6 +18,9 @@ import { SegmentList } from './modules/segmentList'
export class Batch extends RootBase {
/** @internal **/
_opt: ReturnType<typeof normalizedClientBatchBuilderOptions>
/** @internal */
_lines?: string[]
/** @internal */
_messagesCount: number

/**
Expand All @@ -29,6 +32,10 @@ export class Batch extends RootBase {
super(opt)
this._opt = opt
this._messagesCount = 0

if (typeof opt.text !== 'undefined' && opt.parsing === true && opt.text !== '') {
this._lines = this._splitBatch(opt.text).filter(line => line.includes('MSH'))
}
}

/**
Expand All @@ -51,6 +58,22 @@ export class Batch extends RootBase {
segment.set('1', this._messagesCount)
}

/**
* Get Messages
* @since 1.0.0
*/
messages (): Message[] {
if (typeof this._lines !== 'undefined' && typeof this._opt.newLine !== 'undefined') {
const message: Message[] = []
const re = new RegExp(`${this._opt.newLine}$`, 'g')
for (let i = 0; i < this._lines.length; i++) {
message.push(new Message({ text: this._lines[i].replace(re, '') }))
}
return message
}
throw new HL7ParserError(500, 'No messages inside batch')
}

/** @internal */
read (path: string[]): Node {
const segmentName = path.shift() as string
Expand Down Expand Up @@ -118,4 +141,40 @@ export class Batch extends RootBase {
}
throw new HL7FatalError(500, 'Unable to process _getFirstSegment.')
}

/** @internal */
private _getSegIndexes (names: string[], data: string, list: string[] = []): string[] {
for (let i = 0; i < names.length; i++) {
const regexp = new RegExp(`(\n|\r\n|^|\r)${names[i]}\\|`, 'g'); let m
while ((m = regexp.exec(data)) != null) {
const s = m[0]
if (s.includes('\r\n')) {
m.index = m.index + 2
} else if (s.includes('\n')) {
m.index++
} else if (s.includes('\r')) {
m.index++
}
if (m.index !== null) {
list.push(m.index.toString())
}
}
}
return list
}

/** @internal */
private _splitBatch (data: string, batch: string[] = []): string[] {
const getSegIndex = this._getSegIndexes(['FHS', 'BHS', 'MSH', 'BTS', 'FTS'], data)
getSegIndex.sort((a, b) => parseInt(a) - parseInt(b))
for (let i = 0; i < getSegIndex.length; i++) {
const start = parseInt(getSegIndex[i])
let end = parseInt(getSegIndex[i + 1])
if (i + 1 === getSegIndex.length) {
end = data.length
}
batch.push(data.slice(start, end))
}
return batch
}
}
1 change: 0 additions & 1 deletion src/builder/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ export class Message extends RootBase {
}
}
return undefined
// throw new HL7FatalError(500, 'Unable to process _getFirstSegment.')
}

/** @internal */
Expand Down
118 changes: 0 additions & 118 deletions src/client/parser.ts

This file was deleted.

Loading

0 comments on commit 275ce6e

Please sign in to comment.