Skip to content

Commit

Permalink
feat(experiential): fully typescript checked x3
Browse files Browse the repository at this point in the history
#4 [ci skip]
  • Loading branch information
Bugs5382 committed Dec 9, 2023
1 parent 823b2c9 commit 98826e3
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 83 deletions.
12 changes: 6 additions & 6 deletions src/fieldRepetition.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {Component} from "./component";
import {Delimiters} from "./decorators/enum/delimiters";
import {Node} from "./decorators/interfaces/node"
import {HL7FatalError} from "./exception";
import {NodeBase} from "./nodeBase";
import {ValueNode} from "./valueNode";
import {Component} from "./component.js";
import {Delimiters} from "./decorators/enum/delimiters.js";
import {Node} from "./decorators/interfaces/node.js"
import {HL7FatalError} from "./exception.js";
import {NodeBase} from "./nodeBase.js";
import {ValueNode} from "./valueNode.js";

/**
* Create a Field Repetition in an HL7 message segment
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import { Message } from './message.js'
import { NodeBase } from './nodeBase.js'
import { Parser, ParserPlan } from './parser.js'
import { Segment } from './segment.js'
import { SegmentList } from './segmentList.js'
import { HL7_2_7 } from './specification/2.7.js'
import { HL7_SPEC, HL7_SPEC_BASE } from './specification/specification.js'
import { SubComponent } from './subComponent.js'

export default Client
export { Client, Listener, Parser, ParserPlan, Message, Delimiters, Segment, Component, SubComponent, Field, FieldRepetition, EmptyNode, NodeBase, Node }
export { Client, Listener, Parser, ParserPlan, Message, Delimiters, Segment, SegmentList, Component, SubComponent, Field, FieldRepetition, EmptyNode, NodeBase, Node }

/** HL7 Specs **/
export type { MSH } from './specification/specification.js'
Expand Down
136 changes: 99 additions & 37 deletions src/message.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
// @ts-nocheck

import {HL7FatalError} from "./exception";
import {NodeBase} from "./nodeBase";
import {Segment} from "./segment";
import {HL7FatalError} from "./exception.js";
import {NodeBase} from "./nodeBase.js";
import {Segment} from "./segment.js";
import {SegmentList} from "./segmentList.js";
import * as Util from "./utils.js";
import { Delimiters } from './decorators/enum/delimiters'
import { Delimiters } from './decorators/enum/delimiters.js'
import { ClientBuilderOptions, normalizedClientBuilderOptions } from './normalize.js'
import { Node } from "./decorators/interfaces/node";
import { Node } from "./decorators/interfaces/node.js";

/**
* Message Class
* @since 1.0.0
* @extends NodeBase
*/
export class Message extends NodeBase {

/** @internal */
private readonly _delimiters: string
/** @internal */
private readonly _opt: ReturnType<typeof normalizedClientBuilderOptions>

private _delimiters: string
private _matchUnescape: RegExp;
private _matchEscape: RegExp;
/** @internal */
private readonly _matchEscape: RegExp;
/** @internal */
private readonly _matchUnescape: RegExp;

/** @internal */
private static _defaultDelimiters = "\r|^~\\&";
/** @internal */
private static _defaultMatchUnescape = Message._makeMatchUnescape(Message._defaultDelimiters);
/** @internal */
private static _defaultMatchEscape = Message._makeMatchEscape(Message._defaultDelimiters);

/**
* @param props Override default encoding characters. (@default |^~\\&)
* Build the Message or Parse It
* @since 1.0.0
* @param props {@link ClientBuilderOptions}
* @example
* If you are processing a full message, do this:
*
Expand All @@ -38,7 +46,6 @@ export class Message extends NodeBase {
*
* which then you add segments with fields and values for your Hl7 message.
*
* @since 1.0.0
*/
constructor (props?: ClientBuilderOptions) {
let opt = normalizedClientBuilderOptions(props)
Expand All @@ -58,15 +65,13 @@ export class Message extends NodeBase {

if (opt.text == "" && this._opt.specification.checkMSH(this._opt.mshHeader)) {

// set header
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', this.createDate())
this.set('MSH.7', Util.createData(new Date()))
this.set('MSH.9.1', this._opt.mshHeader?.msh_9.msh_9_1)
this.set('MSH.9.2', this._opt.mshHeader?.msh_9.msh_9_2)
this.set('MSH.9.3', this._opt.mshHeader?.msh_9.msh_9_3)
this.set('MSH.9.3', `${this._opt.mshHeader?.msh_9.msh_9_1}_${this._opt.mshHeader?.msh_9.msh_9_2}`)
this.set('MSH.10', this._opt.mshHeader?.msh_10)
// @todo Add MSH.11
this.set('MSH.12', this._opt.specification.name)

} else {
Expand All @@ -82,15 +87,13 @@ export class Message extends NodeBase {
* @private
*/
private static _makeMatchEscape(delimiters: string): RegExp {

let 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]),
];

return new RegExp(sequences.join("|"), "g");
}

Expand All @@ -106,11 +109,19 @@ export class Message extends NodeBase {
return new RegExp([matchEscape,"[^", matchEscape, "]*", matchEscape].join(""), "g");
}

/**
* Get Delimiters
* @since 1.0.0
*/
get delimiters(): string {

return this._delimiters;
}

/**
* Unescape Text
* @since 1.0.0
* @param text
*/
unescape(text: string): string {
if (text == null) {
throw new HL7FatalError(500, "text must be passed in unescape function.")
Expand Down Expand Up @@ -150,6 +161,11 @@ export class Message extends NodeBase {
});
}

/**
* Escape String
* @since 1.0.0
* @param text
*/
escape(text: string): string {
if (text == null) {
throw new HL7FatalError(500, "text must be passed in escape function.")
Expand Down Expand Up @@ -186,8 +202,26 @@ export class Message extends NodeBase {
});
}

/**
* Add a new segment to a message.
* @since 1.0.0
* @description Creating a new segment adds an empty segment to the message.
* It could be blank, or it could have values added into it.
* @param path
* @example
*
* const message = new Message({. the correct options here .})
*
* const segment = message.addSegment('PV1')
* segment.set('PV1.1', 'Value Here');
*
* // When doing this, it overall adds it to the 'message' object
* // when your output is the final result.
*
* const finalMessage = message.toString();
*
*/
addSegment(path: string): Segment {

if (!path) {
throw new Error("Missing segment path.");
}
Expand All @@ -200,66 +234,94 @@ export class Message extends NodeBase {
return this.addChild(preparedPath[0]);
}

/**
* Read a path of a message.
* @description Could return {@link SegmentList}
* @since 1.0.0
* @param path
*/
read(path: string[]): Node {
let segmentName = path.shift();
if(path.length == 0) {
if (path.length == 0) {
// only the segment name was in the path so return a SegmentList
let segments = <Segment[]>this.children.filter(x => (<Segment>x).name == segmentName);
if(segments.length > 0) {
return new SegmentList(this, segments);
return new SegmentList(this, segments) as Node;
}
} else {
if (typeof segmentName === 'undefined') {
throw new Error("We have an error Huston.")
}
}
else {
let segment = this._getFirstSegment(segmentName);
if(segment) {
return segment.read(path);
if (segment) {
return segment.read(path) as Node;
}
}

return null;
throw new Error("Failure is not an option.")
}

/**
* Write Core of the Message
* @since 1.0.0
* @param path
* @param value
* @protected
*/
protected writeCore(path: string[], value: string): Node {

let segmentName = path.shift();
if (typeof segmentName == 'undefined') {
throw new Error("Danger, Will Robinson")
}
let index = this._getFirstSegmentIndex(segmentName);
if(index === undefined) {
index = this.children.length;
}
return this.writeAtIndex(path, value, index, segmentName);
}

protected createChild(text: string, index: number): Node {

// make sure to remove any \n that might follow the \r
/**
* Create a new child of a message which is a segment.
* @since
* @see {@link Segment}
* @param text Segment string. Must be 3 characters long.
* @param _index Not used to create a segment.
* @protected
*/
protected createChild(text: string, _index: number): Node {
return new Segment(this, text.trim());
}

/**
* Path Core
* @since 1.0.0
* @protected
*/
protected pathCore(): string[] {

// the message has an empty path
return [];
}

/** @internal */
private _getFirstSegment(name: string): Segment {

let children = this.children;
for (let i = 0, l = children.length; i < l; i++) {
let segment = <Segment>children[i];
if (segment.name == name) {
return segment;
}
}
throw new Error("We have a problem.")
}
private _getFirstSegmentIndex(name: string): number {

/** @internal */
private _getFirstSegmentIndex(name: string): number {
let children = this.children;
for (let i = 0, l = children.length; i < l; i++) {
let segment = <Segment>children[i];
if (segment.name == name) {
return i;
}
}
return 0
}

}
Loading

0 comments on commit 98826e3

Please sign in to comment.