Skip to content

Commit

Permalink
feat(parser): working unit tests now
Browse files Browse the repository at this point in the history
updated to fs.readFileSync fixed the issue // removed a bunch of console.log inputs now that it's working #4 [ci skip]
  • Loading branch information
Bugs5382 committed Dec 9, 2023
1 parent e564107 commit 4eb5bdd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 27 deletions.
4 changes: 4 additions & 0 deletions __tests__/__data__/adt.hl7
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
MSH|^~\&|EPIC|EPICADT|SMS|SMSADT|199912271408|CHARRIS|ADT^A04|1817457|D|2.5|
PID||0493575^^^2^ID 1|454721||DOE^JOHN^^^^|DOE^JOHN^^^^|19480203|M||B|254 MYSTREET AVE^^MYTOWN^OH^44123^USA||(216)123-4567|||M|NON|400003403~1129086|
NK1||ROE^MARIE^^^^|SPO||(216)123-4567||EC|||||||||||||||||||||||||||
PV1||O|168 ~219~C~PMA^^^^^^^^^||||277^ALLEN MYLASTNAME^BONNIE^^^^|||||||||| ||2688684|||||||||||||||||||||||||199912271408||||||002376853
21 changes: 15 additions & 6 deletions __tests__/hl7.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,22 @@ describe('node hl7 client - parser tests', () => {
parser = new Parser()
})

test('... properties of batch Hl7 messages', async () => {
test('...adt Hl7 message', async () => {
const data = fs.readFileSync( `${__dirname}/__data__/adt.hl7`, "utf-8" );
await parser.processRawData({data})
const batch = await parser.getBatchProcess()

fs.readFile(`${__dirname}/__data__/batch.hl7`, 'utf8', async (_err, data) => {
// @ts-ignore
const message = await parser.processRawData({data})
expect(parser.getBatchProcess()).toBe(true);
});
// first test
expect(batch).toBe(false);
})

test('... batch Hl7 messages', async () => {
const data = fs.readFileSync( `${__dirname}/__data__/batch.hl7`, "utf-8" );
await parser.processRawData({data})
const batch = await parser.getBatchProcess()

// first test
expect(batch).toBe(true);
});

})
49 changes: 28 additions & 21 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ export interface ParserProcessRawData {
data: string;
}



export class Parser extends EventEmitter {
/** @internal */
_forceBatch: boolean = false
Expand All @@ -30,7 +28,7 @@ export class Parser extends EventEmitter {
/** @internal */
_subComponents: string = "~"
/** @internal */
_lineSpliter: string = "|"
_lineSplitter: string = "|"
/** @internal */
_isBatchProcessing: boolean;
/** @internal */
Expand All @@ -49,7 +47,7 @@ export class Parser extends EventEmitter {
}

// @ts-ignore
async processRawData(props: ParserProcessRawData): Promise<string> {
async processRawData(props: ParserProcessRawData) {
if (typeof props.data !== 'undefined') {

const data = props.data
Expand All @@ -63,41 +61,53 @@ export class Parser extends EventEmitter {
&& !data.startsWith('BTS')
&& !data.startsWith('FTS')) {

this._throwError('error.data', "Expected RAW data to be an HL7 Message")
this._throwError('error.data', "Expected RAW data to be an HL7 Message.")
}

// if the data is a batch
if (this._isBatch(data) || this._forceBatch) {
if (await this._isBatch(data) || this._forceBatch) {
// batch processing
this.emit('data.processingBatch')
this._isBatchProcessing = true

const _b = this._splitBatch(data)
// split up the batch
const _b = await this._splitBatch(data)

for (let i = 0; i < _b.length; i++) {
this._results.push(this._processLine(_b[i], i))
const result = await this._processLine(_b[i], i)
this._results.push(result)
}

console.log(this._results)

} else {
// regular processing
this.emit('data.processing', data)

// split up the lines. this is hardcoded to \n
let lines = data.split(`\n`).filter(line => line.indexOf('|') > -1)

for (let i = 0; i < lines.length; i++) {
const result = await this._processLine(lines[i], i)
this._results.push(result)
}

}

} else {
this._throwError('error.data', 'data did not pass any data.')
}

}

/** Tell the user we did a batch process this time around.
* This will be trying if we are processing a batch
* and will remain true until it's finished processing the entire batch.
* @since 1.0.0 */
getBatchProcess(): boolean {
async getBatchProcess(): Promise<boolean> {
return this._isBatchProcessing
}

/** @internal */
private _isBatch(data: string): boolean {
private async _isBatch(data: string): Promise<boolean> {
return (data.startsWith('FHS') || data.startsWith('BHS'));
}

Expand All @@ -108,11 +118,10 @@ export class Parser extends EventEmitter {
}

/** @internal */
private _splitBatch(data: string, batch: string[] = []): string[] {
let getSegIndex = this._getSegIndexes(['FHS', 'BHS', 'MSH', 'BTS', 'FTS'], data)
private async _splitBatch(data: string, batch: string[] = []): Promise<string[]> {
let getSegIndex = await this._getSegIndexes(['FHS', 'BHS', 'MSH', 'BTS', 'FTS'], data)
getSegIndex.sort((a, b) => parseInt(a) - parseInt(b));
for (let i = 0; i < getSegIndex.length; i++) {
console.log('Segment Output: ', getSegIndex[i])
let start = parseInt(getSegIndex[i])
let end = parseInt(getSegIndex[i + 1])
if (i + 1 === getSegIndex.length) {
Expand All @@ -124,7 +133,7 @@ export class Parser extends EventEmitter {
}

/** @internal */
private _getSegIndexes(names: string[], data: string, list: string[] = []): string[] {
private async _getSegIndexes(names: string[], data: string, list: string[] = []): Promise<string[]> {
for (let i = 0; i < names.length; i++) {
let regexp = new RegExp(`(\n|\r\n|^|\r)${names[i]}\\|`, 'g'), m;
while (m = regexp.exec(data)) {
Expand All @@ -144,14 +153,12 @@ export class Parser extends EventEmitter {
return list
}

private _processLine(data: string, index: number) {
console.log("Process Line:", data)
private async _processLine(data: string, index: number) {
let name = data.substring(0, 3);
let content = data.split(this._lineSpliter)
let content = data.split(this._lineSplitter)
if (Object.keys(this._results).indexOf(name) > -1) {
console.log(`Does Exist.`)

} else {
console.log(`Doesn't Exist.`)
const segment = new Segment(this, name, index)
segment.processContent(content)
return segment.getData()
Expand Down

0 comments on commit 4eb5bdd

Please sign in to comment.