Skip to content

Commit

Permalink
feat(parser): segment generation
Browse files Browse the repository at this point in the history
the first iteration of parsing each segment of a HL7 message // this is, for sure, not working. still some kinks in the system #4 [ci skip]
  • Loading branch information
Bugs5382 committed Dec 9, 2023
1 parent 5105171 commit 8492db5
Show file tree
Hide file tree
Showing 2 changed files with 192 additions and 5 deletions.
44 changes: 39 additions & 5 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import EventEmitter from 'events'
import Segment from "./segment.js";

export interface ParserOptions {
/** Force processing items as a batch even if it's a single item. */
forceBatch?: boolean;
/** Separator for Repeating Fields
* @default & */
repeatingFields?: string;
/** Specification Version HL7
* @default none */
specification?: any;
/** Separator for Sub Components Fields
* @default ~ */
subComponents?: string;
Expand All @@ -16,6 +20,8 @@ export interface ParserProcessRawData {
data: string;
}



export class Parser extends EventEmitter {
/** @internal */
_forceBatch: boolean = false
Expand All @@ -24,7 +30,11 @@ export class Parser extends EventEmitter {
/** @internal */
_subComponents: string = "~"
/** @internal */
_lineSpliter: string = "|"
/** @internal */
_isBatchProcessing: boolean;
/** @internal */
_results: any[] = []

constructor(props?: ParserOptions) {
super();
Expand All @@ -38,7 +48,8 @@ export class Parser extends EventEmitter {
this.emit('initialized', { subComponents: this._subComponents, repeatingFields: this._repeatingFields})
}

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

const data = props.data
Expand All @@ -57,24 +68,31 @@ export class Parser extends EventEmitter {

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

const _b = this._splitBatch(data)
for (let i = 0; i < _b.length; i++) {
console.log('Batch To Transform:', _b[i])
this._results.push(this._processLine(_b[i], i))
}

console.log(this._results)

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

}

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

getBatchProcess() {
/** 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 {
return this._isBatchProcessing
}

Expand Down Expand Up @@ -125,4 +143,20 @@ export class Parser extends EventEmitter {
}
return list
}

private _processLine(data: string, index: number) {
console.log("Process Line:", data)
let name = data.substring(0, 3);
let content = data.split(this._lineSpliter)
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()
}

}

}
153 changes: 153 additions & 0 deletions src/segment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import {Parser} from "./parser.js";

export default class Segment {
_name: string = ""
_data: any = {}
_index?: number
_subComponents: string
_repeatingFields: string
_dataSep = "."
_subComponentSplit = "^"

constructor(parser: Parser, name: string, index: number) {
this._name = name
this._index = index
this._subComponents = parser._subComponents
this._repeatingFields = parser._repeatingFields
}

set () {
console.log('set')
}

get () {
console.log('get')
}

processContent(content: string[]) {
const name: string = this._name
for (let idx = 1; idx < content.length; idx++) {
let pos = this._name == "MSH" ? idx + 1 : idx;
if (content[idx].indexOf(this._subComponents) > -1) {

/************ ***/
/*** BIG BLOCK **/
/************ ***/


let subcomponent = content[idx].split(this._subComponents);
for (let j = 0; j < subcomponent.length; j++) {
let component = {};
let subs = subcomponent[j].split(this._subComponentSplit);
for (let k = 0; k < subs.length; k++) {
let pos_k = (k + 1)
if (subs[k].indexOf(this._repeatingFields) > -1) {
let repeating = subs[k].split(this._repeatingFields);
let tmpRepeating: string[] = []
for (let l = 0; l < repeating.length; l++) {
tmpRepeating.push(repeating[l])
}
component = {
...component,
[`${name}${this._dataSep}${pos}${this._dataSep}${pos_k}`]: tmpRepeating
}
} else {
component = { [`${name}${this._dataSep}${pos}${this._dataSep}${pos_k}`]: subs[k] }
}
}
this._data = {
...this._data,
[`${name}${this._dataSep}${pos}`]: component
}
}

/************ ***/
/*** BIG BLOCK **/
/************ ***/

} else if (content[idx].indexOf(this._subComponentSplit) > -1) {

/************ ***/
/*** BIG BLOCK **/
/************ ***/

let subs = content[idx].split(this._subComponentSplit);
for (let j = 0; j < subs.length; j++) {
let component = {};
let pos_j = (j + 1)
if (subs[j].indexOf(this._repeatingFields) > -1) {
let repeating = subs[j].split(this._repeatingFields);
let tmpRepeating: string[] = []
for (let l = 0; l < repeating.length; l++) {
tmpRepeating.push(repeating[l])
}
component = {
...component,
[`${name}${this._dataSep}${pos}`]: {
// ...[`${name}${this._dataSep}${pos}`],
[`${name}${this._dataSep}${pos}${this._dataSep}${pos_j}`]: tmpRepeating
}
}
} else {
component = {
...component,
[`${name}${this._dataSep}${pos}`]: {
// ...[`${name}${this._dataSep}${pos}`],
[`${name}${this._dataSep}${pos}${this._dataSep}${pos_j}`]: subs[j]
}
}
}
this._data = {
...this._data,
[`${name}.${pos}`] : component
}
}

/************ ***/
/*** BIG BLOCK **/
/************ ***/

} else if (content[idx].indexOf(this._repeatingFields) > -1) {

/************ ***/
/*** BIG BLOCK **/
/************ ***/

let repeating = content[idx].split(this._repeatingFields);
let tmpRepeating: string[] = []
for (let l = 0; l < repeating.length; l++) {
tmpRepeating.push(repeating[l]);
}
this._data = {
...this._data,
[`${name}.${pos}`] : tmpRepeating
}

/************ ***/
/*** BIG BLOCK **/
/************ ***/

} else {

/************ ***/
/*** BIG BLOCK **/
/************ ***/

this._data = {
...this._data,
[`${name}${this._dataSep}${pos}`]: content[idx]
}

/************ ***/
/*** BIG BLOCK **/
/************ ***/

}
}
}

getData() {
return this._data
}

}

0 comments on commit 8492db5

Please sign in to comment.