Skip to content

Commit

Permalink
feat: unit tests pass
Browse files Browse the repository at this point in the history
fixed issue with building the MSH header #4
  • Loading branch information
Bugs5382 committed Dec 5, 2023
1 parent d167aa1 commit 9447e78
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 29 deletions.
31 changes: 10 additions & 21 deletions __tests__/hl7.build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,31 @@ 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.'))
}
})

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.'))
}
})

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.'))
}
})

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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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`)

})

})
Expand Down
4 changes: 1 addition & 3 deletions src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
11 changes: 7 additions & 4 deletions src/nodeBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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()
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
11 changes: 10 additions & 1 deletion src/segment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand All @@ -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)
}

Expand Down

0 comments on commit 9447e78

Please sign in to comment.