diff --git a/__tests__/hl7.build.test.ts b/__tests__/hl7.build.test.ts index 523d0c6..fabf989 100644 --- a/__tests__/hl7.build.test.ts +++ b/__tests__/hl7.build.test.ts @@ -7,8 +7,7 @@ describe('node hl7 client - builder tests', () => { test("error - Message Object - nothing passed", async () => { try { - // @ts-expect-error Should error out.. - const message = new Message() + new Message() } catch (err) { expect(err).toEqual(new Error('mshHeader must be set if no HL7 message is being passed.')) } @@ -16,8 +15,7 @@ describe('node hl7 client - builder tests', () => { test("error - Message Object - text empty ", async () => { try { - // @ts-expect-error Message is not used. That's fine. - const message = new Message({ text: ""}) + new Message({ text: ""}) } catch (err) { expect(err).toEqual(new Error('mshHeader must be set if no HL7 message is being passed.')) } @@ -25,8 +23,7 @@ describe('node hl7 client - builder tests', () => { test("error - Message Object - text must start with MSH ", async () => { try { - // @ts-expect-error Message is not used. That's fine. - const message = new Message({ text: "PV1|||||||^Jones\rMSH|^~\\&\r"}) + new Message({ text: "PV1|||||||^Jones\rMSH|^~\\&\r"}) } catch (err) { expect(err).toEqual(new Error('text must begin with the MSH segment.')) } @@ -34,8 +31,7 @@ describe('node hl7 client - builder tests', () => { test("error - Message Object - msh 9.1 is empty ", async () => { try { - // @ts-expect-error Message is not used. That's fine. - const message = new Message({ + new Message({ mshHeader: { msh_9: { // @ts-expect-error 9.1 should be not empty @@ -50,8 +46,7 @@ describe('node hl7 client - builder tests', () => { test("error - Message Object - msh 9.2 is empty ", async () => { try { - // @ts-expect-error Message is not used. That's fine. - const message = new Message({ + new Message({ mshHeader: { msh_9: { // @ts-expect-error 9.2 should be not empty @@ -66,8 +61,7 @@ describe('node hl7 client - builder tests', () => { test("error - Message Object - msh 9.3 is empty ", async () => { try { - // @ts-expect-error Message is not used. That's fine. - const message = new Message({ + new Message({ mshHeader: { msh_9: { // @ts-expect-error 9.3 should be not empty @@ -82,8 +76,7 @@ describe('node hl7 client - builder tests', () => { test("error - Message Object - msh 9.1 is not 3 character long ", async () => { try { - // @ts-expect-error Message is not used. That's fine. - const message = new Message({ + new Message({ mshHeader: { msh_9: { // @ts-expect-error 9.1 should be 3 characters @@ -100,8 +93,7 @@ describe('node hl7 client - builder tests', () => { test("error - Message Object - msh 9.2 is not 3 character long ", async () => { try { - // @ts-expect-error Message is not used. That's fine. - const message = new Message({ + new Message({ mshHeader: { msh_9: { msh_9_1: "ADT", @@ -118,8 +110,7 @@ describe('node hl7 client - builder tests', () => { test("error - Message Object - msh 10 is more than 199 characters ", async () => { try { - // @ts-expect-error Message is not used. That's fine. - const message = new Message({ + new Message({ mshHeader: { msh_9: { msh_9_1: "ADT", @@ -135,8 +126,7 @@ describe('node hl7 client - builder tests', () => { test("error - Message Object - msh 10 can not be blank", async () => { try { - // @ts-expect-error Message is not used. That's fine. - const message = new Message({ + new Message({ mshHeader: { msh_9: { msh_9_1: "ADT", @@ -169,7 +159,6 @@ describe('node hl7 client - builder tests', () => { }) expect(message.toString()).toContain("MSH|^~\\&") expect(message.toString()).toContain(`|ADT^A01^ADT_A01|${randomControlID}||2.7`) - }) }) diff --git a/src/message.ts b/src/message.ts index 0e1cfef..aaa8109 100644 --- a/src/message.ts +++ b/src/message.ts @@ -62,10 +62,8 @@ export class Message extends NodeBase { this._matchEscape = Message._makeMatchEscape(this._delimiters) } - if (typeof this._opt.mshHeader !== 'undefined' && this._opt.text === '') { + if (typeof this._opt.mshHeader !== 'undefined') { if (this._opt.specification.checkMSH(this._opt.mshHeader) === true) { - this.set('MSH.1', `${this._opt.separatorField}`) - this.set('MSH.2', `${this._opt.separatorComponent}${this._opt.separatorRepetition}${this._opt.separatorEscape}${this._opt.separatorSubComponent}`) this.set('MSH.7', Util.createDate(new Date())) this.set('MSH.9.1', this._opt.mshHeader.msh_9.msh_9_1.toString()) this.set('MSH.9.2', this._opt.mshHeader.msh_9.msh_9_2.toString()) diff --git a/src/nodeBase.ts b/src/nodeBase.ts index ec204d3..60d9975 100644 --- a/src/nodeBase.ts +++ b/src/nodeBase.ts @@ -9,7 +9,7 @@ import { Message } from './message.js' export class NodeBase implements Node { protected parent: NodeBase | null - private _name: string + _name: string private _text: string private readonly _delimiter: Delimiters | undefined private _delimiterText: string @@ -60,7 +60,8 @@ export class NodeBase implements Node { this.set(`${path}.${i + 1}`, value[i]) } } else { - this.write(this.preparePath(path), this.prepareValue(value)) + const _path = this.preparePath(path) + this.write(_path, this.prepareValue(value)) } return this @@ -84,7 +85,9 @@ export class NodeBase implements Node { } get name (): string { - if (this._name !== undefined) return this._name + if (this._name !== undefined) { + return this._name + } this._name = this.path.join('.') return this._name } @@ -310,7 +313,7 @@ export class NodeBase implements Node { protected setDirty (): void { if (!this._dirty) { this._dirty = true - if (this.parent !== null) { + if (typeof this.parent !== 'undefined' && this.parent !== null) { this.parent.setDirty() } } diff --git a/src/normalize.ts b/src/normalize.ts index 8c6f7f3..ec9b79b 100644 --- a/src/normalize.ts +++ b/src/normalize.ts @@ -197,6 +197,10 @@ export function normalizedClientBuilderOptions (raw?: ClientBuilderOptions): Cli throw new Error('newLine must be \r or \n') } + if (props.text === '') { + props.text = `MSH${props.separatorField}${props.separatorComponent}${props.separatorRepetition}${props.separatorEscape}${props.separatorSubComponent}` + } + return props } diff --git a/src/segment.ts b/src/segment.ts index 8b5cb7d..c3b858d 100644 --- a/src/segment.ts +++ b/src/segment.ts @@ -26,6 +26,7 @@ export class Segment extends NodeBase { throw new Error('Segment must have a name.') } this._segmentName = text.slice(0, 3) + this._name = this._segmentName } /** @@ -51,10 +52,18 @@ export class Segment extends NodeBase { * @protected */ protected writeCore (path: string[], value: string): Node { - const index = parseInt(path.shift() as string) + let index = parseInt(path.shift() as string) if (index < 1) { throw new Error("Can't have an index < 1") } + if (this._name === 'MSH') { + if (index === 1 || index === 2) { + throw new Error('You cannot assign the field separator or encoding characters') + } else { + index = index - 1 + } + } + return this.writeAtIndex(path, value, index) }