Skip to content

Commit

Permalink
feat: added field class
Browse files Browse the repository at this point in the history
moved results inside a field class so that it can have different functions // also for chaining later on #4
  • Loading branch information
Bugs5382 committed Dec 9, 2023
1 parent de3e81a commit 511c11c
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 29 deletions.
10 changes: 10 additions & 0 deletions src/declaration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {Segment} from "./segment";

export interface ISegment {
/* Name of the segment */
name: string;
/* The data of the segment */
data: Segment
/*Content of the HL7 */
content: string;
}
56 changes: 56 additions & 0 deletions src/field.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import EventEmitter from "events";



export class Field extends EventEmitter {
/** @internal */
_data: string | string[] | {}
/** @internal */
_dataType?: any;
/** @internal */
_internalName: string
/** @internal */
_name?: string;

/**
*
* @since 1.0.0
* @param data
* @param name */
constructor(data: string | string[] | {}, name: string) {
super();

this._data = data
this._internalName = name

// @todo Assign Long name based of field and HL7 Standards Types
// @todo Assign Field types

}

/**
* Get Name of Field based off HL7 Spec being used.
* @description This is not fully implemented yet.
* @example is_patient_name
* @since 1.0.0*/
// async getName(): Promise<string> {
//
// }

/**
* Get field Data Type
* @description This is not fully implemented yet.
* */
// async getDataType(): Promise<any> {
//
// }

/** Get Value of Field
* @since 1.0.0*/
async getValue(): Promise<string | string[] | {}> {
this.emit('field.getValue')
return this._data
}


}
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Client } from './client.js'
import { Listener } from './listener.js'
import {ISegment, Parser } from './parser.js'
import { Parser } from './parser.js'
import { Segment } from './segment.js'
import { Field } from './field'
import { ISegment } from './declaration.js'

export default Client
export { Client, Listener, Parser, Segment, ISegment }
export { Client, Listener, Parser, Segment, ISegment, Field }

export type { ClientOptions, ClientListenerOptions, ParserOptions, ParserProcessRawData } from './normalize.js'
export type { HL7ClientError, HL7ClientFatalError } from './exception.js'
17 changes: 5 additions & 12 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import EventEmitter from 'events'
import {ISegment} from "./declaration.js";
import {ParserOptions, ParserProcessRawData} from "./normalize.js";
import {Segment} from './segment.js'

Expand All @@ -13,15 +14,6 @@ if (!String.prototype.startsWith) {
});
}

export interface ISegment {
/* Name of the segment */
name: string;
/* The data of the segment */
data: Segment
/*Content of the HL7 */
content: string;
}

/** Parser Class
* @since 1.0.0 */
export class Parser extends EventEmitter {
Expand All @@ -32,7 +24,7 @@ export class Parser extends EventEmitter {
/** @internal */
_subComponents: string = '~'
/** @internal */
_dataSep: string = '.'
_dataSep: string = '_'
/** @internal */
_subComponentSplit: string = '^'
/** @internal */
Expand Down Expand Up @@ -77,6 +69,7 @@ export class Parser extends EventEmitter {
if (findIndex < 0) {
this._throwError('error.segment.not.found', segment)
}
this.emit('parser.get.segment', segment)
return this._results[findIndex].data
}

Expand Down Expand Up @@ -115,7 +108,7 @@ export class Parser extends EventEmitter {
const name = _b[i].substring(0, 3)
const content = _b[i].split(this._lineSplitter)
// @todo if MSH, get parser options for this HL7 message. use those when parsing data
this._results?.push({name: `${name}.${i+1}`, data: new Segment(this, name, content,i), content: _b[i] })
this._results?.push({name: `${name}${this._dataSep}${i+1}`, data: new Segment(this, name, content,i), content: _b[i] })
}

} else {
Expand All @@ -129,7 +122,7 @@ export class Parser extends EventEmitter {
const name = lines[i].substring(0, 3)
const content = lines[i].split(this._lineSplitter)
// @todo if MSH, get parser options for this HL7 message. use those when parsing data
this._results?.push({name: `${name}.${i+1}`, data: new Segment(this, name, content,i), content: lines[i]})
this._results?.push({name: `${name}${this._dataSep}${i+1}`, data: new Segment(this, name, content,i), content: lines[i]})
}
}
} catch (_e: any) {
Expand Down
32 changes: 17 additions & 15 deletions src/segment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import EventEmitter from "events";
import {Field} from "./field.js";
import { Parser } from './parser.js'

/**
Expand All @@ -16,7 +17,7 @@ export class Segment extends EventEmitter{
/** @internal */
_repeatingFields: string
/** @internal */
_dataSep = '.'
_dataSep = '_'
/** @internal */
_subComponentSplit = '^'
/** @internal */
Expand All @@ -41,9 +42,9 @@ export class Segment extends EventEmitter{

/** Get the data from this segment at this current index in the HL7 string.
* @since 1.0.0 */
async getValue (area: string): Promise<string | [] | {}> {
this.emit('segment.get', area)
return this._data[area]
async getField (field: string): Promise<Field | Field[]> {
this.emit('segment.get.field', field)
return this._data[field]
}

/** Process the line and create this segment to be built into the results.
Expand All @@ -66,16 +67,16 @@ export class Segment extends EventEmitter{
const posK = (k + 1)
if (subs[k].includes(this._repeatingFields)) {
const repeating = subs[k].split(this._repeatingFields)
const tmpRepeating: string[] = []
const tmpRepeating: Field[] = []
for (let l = 0; l < repeating.length; l++) {
tmpRepeating.push(repeating[l])
tmpRepeating.push(new Field(repeating[l], `${name}${this._dataSep}${pos}${this._dataSep}${posK}`))
}
component = {
...component,
[`${name}${this._dataSep}${pos}${this._dataSep}${posK}`]: tmpRepeating
}
} else {
component = { [`${name}${this._dataSep}${pos}${this._dataSep}${posK}`]: subs[k] }
component = { [`${name}${this._dataSep}${pos}${this._dataSep}${posK}`]: new Field(subs[k], `${name}${this._dataSep}${pos}${this._dataSep}${posK}`) }
}
}
this._data = {
Expand All @@ -90,9 +91,9 @@ export class Segment extends EventEmitter{
const posJ = (j + 1)
if (subs[j].includes(this._repeatingFields)) {
const repeating = subs[j].split(this._repeatingFields) // & split here
const tmpRepeating: string[] = []
const tmpRepeating: Field[] = []
for (let l = 0; l < repeating.length; l++) {
tmpRepeating.push(repeating[l])
tmpRepeating.push(new Field(repeating[l], `${name}${this._dataSep}${pos}${this._dataSep}${posJ}`) )
}
component = {
...component,
Expand All @@ -101,7 +102,7 @@ export class Segment extends EventEmitter{
} else {
component = {
...component,
[`${name}${this._dataSep}${pos}${this._dataSep}${posJ}`]: subs[j]
[`${name}${this._dataSep}${pos}${this._dataSep}${posJ}`]: new Field(subs[j], `${name}${this._dataSep}${pos}${this._dataSep}${posJ}`)
}
}
this._data = {
Expand All @@ -112,27 +113,28 @@ export class Segment extends EventEmitter{

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

// override MSH 2.1, always since it needs to include the encoding characters that we used
if (this._name === "MSH") {
this._data = {
['MSH_1']: '|',
...this._data,
['MSH.2']: '^~\\&'
['MSH_2']: '^~\\&' // if this is HL7 Spec 2,7 or higher # at the end
}
}

Expand Down

0 comments on commit 511c11c

Please sign in to comment.