Skip to content

Commit

Permalink
fix: fixed types
Browse files Browse the repository at this point in the history
* doesn't quite fix #13, but typedoc is showing no export errors now
* fixing typing when this package is complied with another dependency which would be needed
* moved unit test functions back into its own file
  • Loading branch information
Bugs5382 committed Dec 16, 2023
1 parent 84341f4 commit 270baba
Show file tree
Hide file tree
Showing 16 changed files with 199 additions and 96 deletions.
34 changes: 34 additions & 0 deletions __tests__/__utils__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import EventEmitter from 'node:events'


/** @internal */
export const sleep = async (ms: number): Promise<unknown> => {
return await new Promise(resolve => setTimeout(resolve, ms))
}

/** @internal */
export const expectEvent = async <T=any>(emitter: EventEmitter, name: string | symbol): Promise<T> => {
return await new Promise<T>((resolve) => { emitter.once(name, resolve) })
}


/** @internal */
export interface Deferred<T=any> {
resolve: (value: T | PromiseLike<T>) => void
reject: (reason?: any) => void
promise: Promise<T>
}

/** @internal */
export const createDeferred = <T=any>(noUncaught?: boolean): Deferred<T> => {
const dfd: any = {}
dfd.promise = new Promise((resolve, reject) => {
dfd.resolve = resolve
dfd.reject = reject
})
/* istanbul ignore next */
if (noUncaught === false) {
dfd.promise.catch(() => {})
}
return dfd
}
4 changes: 3 additions & 1 deletion __tests__/hl7.build.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {randomUUID} from "crypto";
import fs from "fs";
import path from "node:path";
import {FileBatch, Batch, Message, createHL7Date, isBatch, isFile, sleep, EmptyNode, Node} from "../src";
import {FileBatch, Batch, Message, createHL7Date, isBatch, isFile } from "../src";
import {Node, EmptyNode} from "../src";
import {sleep} from "./__utils__";

describe('node hl7 client - builder tests', () => {

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 { HL7FatalError, HL7ParserError } from '../utils/exception.js'
import { ClientBuilderBatchOptions, normalizedClientBatchBuilderOptions } from '../utils/normalizedBuilder.js'
import { isNumber, createHL7Date } from '../utils/utils.js'
import { isHL7Number, createHL7Date } from '../utils/utils.js'
import { FileBatch } from './fileBatch.js'
import { Node } from './interface/node.js'
import { Message } from './message.js'
Expand Down Expand Up @@ -152,7 +152,7 @@ export class Batch extends RootBase {
}

return this
} else if (isNumber(path)) {
} else if (isHL7Number(path)) {
if (Array.isArray(value)) {
const child = this.ensure(path)
for (let i = 0, l = value.length; i < l; i++) {
Expand Down
4 changes: 2 additions & 2 deletions src/builder/fileBatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'node:fs'
import path from 'node:path'
import { HL7FatalError, HL7ParserError } from '../utils/exception.js'
import { ClientBuilderFileOptions, normalizedClientFileBuilderOptions } from '../utils/normalizedBuilder.js'
import { createHL7Date, isNumber } from '../utils/utils.js'
import { createHL7Date, isHL7Number } from '../utils/utils.js'
import { Batch } from './batch.js'
import { Node } from './interface/node.js'
import { Message } from './message.js'
Expand Down Expand Up @@ -193,7 +193,7 @@ export class FileBatch extends RootBase {
}

return this
} else if (isNumber(path)) {
} else if (isHL7Number(path)) {
if (Array.isArray(value)) {
const child = this.ensure(path)
for (let i = 0, l = value.length; i < l; i++) {
Expand Down
5 changes: 4 additions & 1 deletion src/builder/interface/node.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/** @internal */
/**
* Node
* @since 1.0.0
*/
export interface Node {
name: string
length: number
Expand Down
4 changes: 2 additions & 2 deletions src/builder/message.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HL7FatalError } from '../utils/exception.js'
import { ClientBuilderMessageOptions, normalizedClientMessageBuilderOptions } from '../utils/normalizedBuilder.js'
import { createHL7Date, isNumber } from '../utils/utils.js'
import { createHL7Date, isHL7Number } from '../utils/utils.js'
import { FileBatch } from './fileBatch.js'
import { NodeBase } from './modules/nodeBase.js'
import { RootBase } from './modules/rootBase.js'
Expand Down Expand Up @@ -152,7 +152,7 @@ export class Message extends RootBase {
}

return this
} else if (isNumber(path)) {
} else if (isHL7Number(path)) {
if (Array.isArray(value)) {
const child = this.ensure(path)
for (let i = 0, l = value.length; i < l; i++) {
Expand Down
7 changes: 6 additions & 1 deletion src/builder/modules/emptyNode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Node } from '../interface/node.js'

/** @internal */
/**
* Empty Node
* @description Used for anything not value in a segment index, this will be the defaults of the methods.
* Some have to be implemented on those segments to work.
* @since 1.0.0
*/
export class EmptyNode implements Node {
get name (): string {
throw new Error('Method not implemented')
Expand Down
16 changes: 10 additions & 6 deletions src/builder/modules/nodeBase.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { isNumber, isString, pad } from '../../utils/utils.js'
import { isHL7Number, isHL7String, padHL7Date } from '../../utils/utils.js'
import { Batch } from '../batch.js'
import { EmptyNode } from './emptyNode.js'
import { HL7FatalError } from '../../utils/exception.js'
import { Delimiters } from '../../utils/enum.js'
import { Node } from '../interface/node.js'
import { Message } from '../message.js'

/** @internal */
/**
* Node Base
* @since 1.0.0
* @implements Node
*/
export class NodeBase implements Node {
protected parent: NodeBase | null

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

return this
} else if (isNumber(path)) {
} else if (isHL7Number(path)) {
if (Array.isArray(value)) {
const child = this.ensure(path)
for (let i = 0, l = value.length; i < l; i++) {
Expand Down Expand Up @@ -156,7 +160,7 @@ export class NodeBase implements Node {
}
if (typeof path === 'number') {
return this.setChild(this.createChild('', path), path)
} else if (isString(path)) {
} else if (isHL7String(path)) {
return this.write(this.preparePath(path), '')
}
throw new HL7FatalError(500, 'There seems to be a problem.')
Expand Down Expand Up @@ -335,12 +339,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) + pad(date.getHours(), 2) + pad(date.getMinutes(), 2) + pad(date.getSeconds(), 2)
return this._formatDate(date) + padHL7Date(date.getHours(), 2) + padHL7Date(date.getMinutes(), 2) + padHL7Date(date.getSeconds(), 2)
}
return this._formatDate(date)
}

private _formatDate (date: Date): string {
return date.getFullYear().toString() + pad(date.getMonth() + 1, 2) + pad(date.getDate(), 2)
return date.getFullYear().toString() + padHL7Date(date.getMonth() + 1, 2) + padHL7Date(date.getDate(), 2)
}
}
6 changes: 3 additions & 3 deletions src/builder/modules/segment.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Delimiters } from '../../utils/enum.js'
import { HL7FatalError } from '../../utils/exception.js'
import { isNumber, isString } from '../../utils/utils.js'
import { isHL7Number, isHL7String } from '../../utils/utils.js'
import { Field } from './field.js'
import { NodeBase } from './nodeBase.js'
import { Node } from '../interface/node.js'
Expand All @@ -23,7 +23,7 @@ export class Segment extends NodeBase {
*/
constructor (parent: NodeBase, text: string) {
super(parent, text, Delimiters.Field)
if (!isString(text) || text.length === 0) {
if (!isHL7String(text) || text.length === 0) {
throw new HL7FatalError(500, 'Segment must have a name.')
}
this._segmentName = text.slice(0, 3)
Expand Down Expand Up @@ -74,7 +74,7 @@ export class Segment extends NodeBase {
}

return this
} else if (isNumber(path)) {
} else if (isHL7Number(path)) {
if (Array.isArray(value)) {
const child = this.ensure(path)
for (let i = 0, l = value.length; i < l; i++) {
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 { HL7FatalError } from '../../utils/exception.js'
import { isString } from '../../utils/utils.js'
import { isHL7String } from '../../utils/utils.js'
import { ValueNode } from './valueNode.js'

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

/** @internal */
/**
* Value Node
* @since 1.0.0
*/
export class ValueNode extends NodeBase {
protected key: string

Expand Down
11 changes: 5 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { HL7_2_7, HL7_2_7_BHS, HL7_2_7_FHS } from './specification/2.7.js'
import { HL7_SPEC, HL7_SPEC_BASE } from './specification/specification.js'

import { NodeBase } from './builder/modules/nodeBase.js'
import { EmptyNode } from './builder/modules/emptyNode.js'
import { Node } from './builder/interface/node.js'
Expand All @@ -18,18 +17,18 @@ import { ParserPlan } from './utils/parserPlan.js'
import { HL7Outbound, OutboundHandler } from './client/hl7Outbound.js'
import { Delimiters, ReadyState } from './utils/enum.js'

export { sleep, expectEvent, expBackoff, createDeferred, Deferred, assertNumber, isNumber, isString, validIPv4, validIPv6, createHL7Date, isBatch, isFile, pad, escapeForRegExp, decodeHexString, randomString } from './utils/utils.js'
export { expBackoff, assertNumber, isHL7Number, isHL7String, validIPv4, validIPv6, createHL7Date, isBatch, isFile, padHL7Date, escapeForRegExp, decodeHexString, randomString } from './utils/utils.js'

/** HL7 Specs **/
export type { MSH } from './specification/specification.js'
export type { HL7_2_7_MSH } from './specification/2.7.js'
export { MSH, BHS, FHS } from './specification/specification.js'
export { HL7_2_7_MSH } from './specification/2.7.js'

/** HL7 Class **/
export { HL7_2_7_BHS, HL7_2_7_FHS, HL7_SPEC, HL7_SPEC_BASE, HL7_2_7 }
export { HL7_2_7_BHS, HL7_2_7_FHS, HL7_2_7, HL7_SPEC, HL7_SPEC_BASE }

export type { ClientOptions, ClientListenerOptions } from './utils/normalizedClient.js'
export type { ClientBuilderFileOptions, ClientBuilderBatchOptions, ClientBuilderMessageOptions, ClientBuilderOptions } from './utils/normalizedBuilder.js'
export type { HL7Error, HL7FatalError, HL7ParserError } from './utils/exception.js'

export default Client
export { Client, HL7Outbound, OutboundHandler, ParserPlan, FileBatch, Batch, Message, Segment, SegmentList, Component, SubComponent, Field, FieldRepetition, NodeBase, EmptyNode, Node, Delimiters, ReadyState }
export { Client, HL7Outbound, OutboundHandler, FileBatch, Batch, Message, Segment, SegmentList, Component, SubComponent, Field, FieldRepetition, ParserPlan, Node, NodeBase, EmptyNode, Delimiters, ReadyState }
29 changes: 22 additions & 7 deletions src/specification/specification.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import { HL7_2_7_BHS, HL7_2_7_FHS, HL7_2_7_MSH } from './2.7.js'

/** @internal Unions **/
/**
* MSH Unions
* @since 1.0.0
*/
export type MSH = HL7_2_7_MSH
/** @internal Unions **/
/**
* BHS Unions
* @since 1.0.0
*/
export type BHS = HL7_2_7_BHS
/** @internal Unions **/
/**
* FHS Unions
* @since 1.0.0
*/
export type FHS = HL7_2_7_FHS

/** @internal */
/**
* HL7 Base Interface
* @since 1.0.0
*/
export interface HL7_SPEC {
/** Name of the HL7 Spec */
name: string
Expand All @@ -17,12 +29,15 @@ export interface HL7_SPEC {
checkFHS: (options: FHS) => boolean
/** Check the MSH Header for this Specification */
checkMSH: (options: MSH) => boolean

}

/** @internal */
/**
* Base Class of an HL7 Specification
* @since 1.0.0
*/
export class HL7_SPEC_BASE implements HL7_SPEC {
/** @internal */
/** Name
* @since 1.0.0 */
name = ''

/**
Expand Down
11 changes: 9 additions & 2 deletions src/utils/enum.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
/** @internal */
/**
* Type of Segments Values
* @description Used during the class creation to give each type its own index value.
* @since 1.0.0
*/
export enum Delimiters {
Segment,
Field,
Expand All @@ -8,7 +12,10 @@ export enum Delimiters {
SubComponent
}

/** @internal */
/**
* State of the Connected to the Server
* @since 1.0.0
*/
export enum ReadyState {
CONNECTING,
CONNECTED,
Expand Down
6 changes: 4 additions & 2 deletions src/utils/parserPlan.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@

/**
* Used to figure out the current HL7 message(s)/batch delimited used to encode this particular HL7 message.
* @internal
* Parse Plan
* @description Used to figure out the current HL7
* message(s)/batch delimited used to encode this particular HL7 message
* @since 1.0.0
*/
export class ParserPlan {
/** @internal */
Expand Down
Loading

0 comments on commit 270baba

Please sign in to comment.