Skip to content

Commit

Permalink
feat: updates to the code to make it cleaner
Browse files Browse the repository at this point in the history
- node-hl7-server unit tests still pass and local unit tests pass
- added reconnection logic into the system (working on unit test)
- moved some files and functions around

#15

[ci skip]
  • Loading branch information
Bugs5382 committed Dec 13, 2023
1 parent 6dc84e3 commit 631026a
Show file tree
Hide file tree
Showing 19 changed files with 211 additions and 187 deletions.
30 changes: 0 additions & 30 deletions __tests__/__utils__/utils.ts

This file was deleted.

13 changes: 6 additions & 7 deletions __tests__/hl7.build.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {randomUUID} from "crypto";
import {EmptyNode} from "../src/builder/modules/emptyNode";
import * as Util from '../src/utils/'
import {Batch, Message} from "../src";
import {Batch, createHL7Date, isBatch, isFile, Message} from "../src";
import { Node } from "../src/builder/interface/node";

describe('node hl7 client - builder tests', () => {
Expand Down Expand Up @@ -339,7 +338,7 @@ describe('node hl7 client - builder tests', () => {

let batch: Batch
let message: Message
const date = Util.createHL7Date(new Date(), "8")
const date = createHL7Date(new Date(), "8")

beforeEach(async () => {
batch = new Batch()
Expand Down Expand Up @@ -488,7 +487,7 @@ describe('node hl7 client - builder tests', () => {
msh_10: 'CONTROL_ID'
}
})
expect(Util.isFile(message.toString())).toBe(false)
expect(isFile(message.toString())).toBe(false)

})

Expand All @@ -501,7 +500,7 @@ describe('node hl7 client - builder tests', () => {
msh_10: 'CONTROL_ID'
}
})
expect(Util.isBatch(message.toString())).toBe(false)
expect(isBatch(message.toString())).toBe(false)

})

Expand All @@ -510,7 +509,7 @@ describe('node hl7 client - builder tests', () => {
let batch = new Batch()
batch.start()
batch.end()
expect(Util.isFile(batch.toString())).toBe(false)
expect(isFile(batch.toString())).toBe(false)

})

Expand All @@ -519,7 +518,7 @@ describe('node hl7 client - builder tests', () => {
let batch = new Batch()
batch.start()
batch.end()
expect(Util.isBatch(batch.toString())).toBe(true)
expect(isBatch(batch.toString())).toBe(true)

})

Expand Down
3 changes: 1 addition & 2 deletions __tests__/hl7.client.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import portfinder from 'portfinder'
import {Hl7Inbound, Server} from "../../node-hl7-server/src";
import {Client, HL7Outbound, Message} from '../src'
import {expectEvent} from "./__utils__/utils";
import {Client, expectEvent, HL7Outbound, Message} from '../src'

describe('node hl7 client', () => {

Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@
"url": "https://github.com/Bugs5382/node-hl7-client/issues"
},
"homepage": "https://github.com/Bugs5382/node-hl7-client#readme",
"dependencies": {
"node-hl7-server": "file:../node-hl7-server"
},
"devDependencies": {
"@semantic-release/changelog": "^6.0.3",
"@semantic-release/commit-analyzer": "^11.1.0",
Expand All @@ -66,6 +63,7 @@
"jest": "^29.7.0",
"jest-ts-webcompat-resolver": "^1.0.0",
"npm-check-updates": "^16.14.11",
"node-hl7-server": "file:../node-hl7-server",
"portfinder": "^1.0.32",
"pre-commit": "^1.2.2",
"semantic-release": "^22.0.11",
Expand Down
4 changes: 2 additions & 2 deletions src/builder/batch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Util from '../utils'
import { HL7FatalError, HL7ParserError } from '../utils/exception.js'
import { ClientBuilderBatchOptions, normalizedClientBatchBuilderOptions } from '../utils/normalizedBuilder.js'
import { createHL7Date } from '../utils/utils'
import { Node } from './interface/node.js'
import { Message } from './message.js'
import { RootBase } from './modules/rootBase.js'
Expand Down Expand Up @@ -96,7 +96,7 @@ export class Batch extends RootBase {
* @since 1.0.0
*/
start (): void {
this.set('BSH.7', Util.createHL7Date(new Date()))
this.set('BSH.7', createHL7Date(new Date()))
}

/** @internal */
Expand Down
4 changes: 2 additions & 2 deletions src/builder/message.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { HL7FatalError } from '../utils/exception.js'
import { ClientBuilderMessageOptions, normalizedClientMessageBuilderOptions } from '../utils/normalizedBuilder.js'
import { createHL7Date } from '../utils/utils'
import { RootBase } from './modules/rootBase.js'
import { Segment } from './modules/segment.js'
import { SegmentList } from './modules/segmentList.js'
import * as Util from '../utils'
import { Node } from './interface/node.js'

/**
Expand Down Expand Up @@ -41,7 +41,7 @@ export class Message extends RootBase {

if (typeof this._opt.messageHeader !== 'undefined') {
if (this._opt.specification.checkMSH(this._opt.messageHeader) === true) {
this.set('MSH.7', Util.createHL7Date(new Date()))
this.set('MSH.7', createHL7Date(new Date()))
this.set('MSH.9.1', this._opt.messageHeader.msh_9_1.toString())
this.set('MSH.9.2', this._opt.messageHeader.msh_9_2.toString())
this.set('MSH.9.3', `${this._opt.messageHeader.msh_9_1.toString()}_${this._opt.messageHeader.msh_9_2.toString()}`)
Expand Down
2 changes: 1 addition & 1 deletion src/builder/modules/component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Delimiters } from '../decorators/delimiters.js'
import { Delimiters } from '../../utils/enum'
import { Node } from '../interface/node.js'
import { NodeBase } from './nodeBase.js'
import { SubComponent } from './subComponent.js'
Expand Down
2 changes: 1 addition & 1 deletion src/builder/modules/field.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HL7FatalError } from '../../utils/exception.js'
import { Delimiters } from '../decorators/delimiters.js'
import { Delimiters } from '../../utils/enum'
import { Node } from '../interface/node.js'
import { FieldRepetition } from './fieldRepetition.js'
import { NodeBase } from './nodeBase.js'
Expand Down
2 changes: 1 addition & 1 deletion src/builder/modules/fieldRepetition.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component } from './component.js'
import { Delimiters } from '../decorators/delimiters.js'
import { Delimiters } from '../../utils/enum'
import { Node } from '../interface/node.js'
import { HL7FatalError } from '../../utils/exception.js'
import { NodeBase } from './nodeBase.js'
Expand Down
12 changes: 6 additions & 6 deletions src/builder/modules/nodeBase.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { isNumber, isString, pad } from '../../utils/utils'
import { Batch } from '../batch.js'
import { EmptyNode } from './emptyNode.js'
import { HL7FatalError } from '../../utils/exception.js'
import * as Util from '../../utils'
import { Delimiters } from '../decorators/delimiters.js'
import { Delimiters } from '../../utils/enum'
import { Node } from '../interface/node.js'
import { Message } from '../message.js'

Expand Down Expand Up @@ -64,7 +64,7 @@ export class NodeBase implements Node {
}

return this
} else if (Util.isNumber(path)) {
} else if (isNumber(path)) {
if (Array.isArray(value)) {
const child = this.ensure(path)
for (let i = 0, l = value.length; i < l; i++) {
Expand Down Expand Up @@ -152,7 +152,7 @@ export class NodeBase implements Node {
}
if (typeof path === 'number') {
return this.setChild(this.createChild('', path), path)
} else if (Util.isString(path)) {
} else if (isString(path)) {
return this.write(this.preparePath(path), '')
}
throw new HL7FatalError(500, 'There seems to be a problem.')
Expand Down Expand Up @@ -331,12 +331,12 @@ export class NodeBase implements Node {
private _formatDateTime (date: Date): string {
// check if there is a time component
if (date.getHours() !== 0 || date.getMinutes() !== 0 || date.getSeconds() !== 0 || date.getMilliseconds() !== 0) {
return this._formatDate(date) + Util.pad(date.getHours(), 2) + Util.pad(date.getMinutes(), 2) + Util.pad(date.getSeconds(), 2)
return this._formatDate(date) + pad(date.getHours(), 2) + pad(date.getMinutes(), 2) + pad(date.getSeconds(), 2)
}
return this._formatDate(date)
}

private _formatDate (date: Date): string {
return date.getFullYear().toString() + Util.pad(date.getMonth() + 1, 2) + Util.pad(date.getDate(), 2)
return date.getFullYear().toString() + pad(date.getMonth() + 1, 2) + pad(date.getDate(), 2)
}
}
20 changes: 9 additions & 11 deletions src/builder/modules/rootBase.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { HL7FatalError } from '../../utils/exception.js'

import { ClientBuilderOptions } from '../../utils/normalizedBuilder'
import { Delimiters } from '../decorators/delimiters.js'
import { Delimiters } from '../../utils/enum'
import { decodeHexString, escapeForRegExp } from '../../utils/utils'
import { NodeBase } from './nodeBase.js'
import * as Util from '../../utils'

/**
* Root Base
* @since 1.0.0
Expand Down Expand Up @@ -50,11 +48,11 @@ export class RootBase extends NodeBase {
*/
protected static _makeMatchEscape (delimiters: string): RegExp {
const sequences = [
Util.escapeForRegExp(delimiters[Delimiters.Escape]),
Util.escapeForRegExp(delimiters[Delimiters.Field]),
Util.escapeForRegExp(delimiters[Delimiters.Repetition]),
Util.escapeForRegExp(delimiters[Delimiters.Component]),
Util.escapeForRegExp(delimiters[Delimiters.SubComponent])
escapeForRegExp(delimiters[Delimiters.Escape]),
escapeForRegExp(delimiters[Delimiters.Field]),
escapeForRegExp(delimiters[Delimiters.Repetition]),
escapeForRegExp(delimiters[Delimiters.Component]),
escapeForRegExp(delimiters[Delimiters.SubComponent])
]
return new RegExp(sequences.join('|'), 'g')
}
Expand All @@ -67,7 +65,7 @@ export class RootBase extends NodeBase {
*/
protected static _makeMatchUnescape (delimiters: string): RegExp {
// setup regular expression for matching escape sequences, see http://www.hl7standards.com/blog/2006/11/02/hl7-escape-sequences/
const matchEscape = Util.escapeForRegExp(delimiters[Delimiters.Escape])
const matchEscape = escapeForRegExp(delimiters[Delimiters.Escape])
return new RegExp([matchEscape, '[^', matchEscape, ']*', matchEscape].join(''), 'g')
}

Expand Down Expand Up @@ -107,7 +105,7 @@ export class RootBase extends NodeBase {
case 'T':
return this._delimiters[Delimiters.SubComponent]
case 'X':
return Util.decodeHexString(match.slice(2, match.length - 1))
return decodeHexString(match.slice(2, match.length - 1))
case 'C':
case 'H':
case 'M':
Expand Down
6 changes: 3 additions & 3 deletions src/builder/modules/segment.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Delimiters } from '../decorators/delimiters.js'
import { Delimiters } from '../../utils/enum'
import { HL7FatalError } from '../../utils/exception.js'
import { isString } from '../../utils/utils'
import { Field } from './field.js'
import { NodeBase } from './nodeBase.js'
import { Node } from '../interface/node.js'
import * as Util from '../../utils'
import { SubComponent } from './subComponent.js'

/**
Expand All @@ -23,7 +23,7 @@ export class Segment extends NodeBase {
*/
constructor (parent: NodeBase, text: string) {
super(parent, text, Delimiters.Field)
if (!Util.isString(text) || text.length === 0) {
if (!isString(text) || text.length === 0) {
throw new HL7FatalError(500, 'Segment must have a name.')
}
this._segmentName = text.slice(0, 3)
Expand Down
4 changes: 2 additions & 2 deletions src/builder/modules/subComponent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Util from '../../utils'
import { HL7FatalError } from '../../utils/exception.js'
import { isString } from '../../utils/utils'
import { ValueNode } from './valueNode.js'

/**
Expand All @@ -24,6 +24,6 @@ export class SubComponent extends ValueNode {
* @since 1.0.0
*/
isEmpty (): boolean {
return !Util.isString(this.toString())
return !isString(this.toString())
}
}
2 changes: 1 addition & 1 deletion src/builder/modules/valueNode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HL7FatalError } from '../../utils/exception.js'
import { Delimiters } from '../decorators/delimiters.js'
import { Delimiters } from '../../utils/enum'
import { NodeBase } from './nodeBase.js'

/** @internal */
Expand Down
Loading

0 comments on commit 631026a

Please sign in to comment.