diff --git a/src/field.ts b/src/field.ts index e69de29..4d4af9f 100644 --- a/src/field.ts +++ b/src/field.ts @@ -0,0 +1,14 @@ +import {Delimiters} from "./delimiters"; +import {Node} from "./node"; +import {ValueNode} from "./valueNode"; + +export class Field extends ValueNode { + + constructor(parent: Node, key: string, text: string) { + super(parent, key, text, Delimiters.Field); + + console.log(parent, key, text) + + } + +} \ No newline at end of file diff --git a/src/message.ts b/src/message.ts index a7ac7d2..96e43ab 100644 --- a/src/message.ts +++ b/src/message.ts @@ -8,6 +8,7 @@ import {createDateTime} from "./utils"; export class Message extends Node { private _opt: ReturnType; + private readonly _delimiters: string; /** * @param text If not provided, it will be MSH plus the default encoding characters. @@ -18,6 +19,7 @@ export class Message extends Node { super( null,text, Delimiters.Segment) this._opt = normalizedClientBuilderOptions(props) + this._delimiters = `${this._opt.newLine}${this._opt.separatorField}${this._opt.separatorRepetition}${this._opt.separatorEscape}${this._opt.separatorSubComponent}` if (!text) { this.initialBuild() @@ -26,16 +28,13 @@ export class Message extends Node { } addSegment(path: string): Segment { - if (!path) { throw new Error("Missing segment path."); } - let preparedPath = this.preparePath(path); if (preparedPath.length != 1) { throw new Error("Invalid segment path '" + path + "'."); } - return this.addChild(preparedPath[0]); } @@ -53,5 +52,13 @@ export class Message extends Node { return []; } + protected createChild(text: string, _index: number): Node { + // make sure to remove any \n that might follow the \r + return new Segment(this, text.trim()); + } + + get delimiters(): string { + return this._delimiters; + } } \ No newline at end of file diff --git a/src/node.ts b/src/node.ts index e27cf86..ef6ea99 100644 --- a/src/node.ts +++ b/src/node.ts @@ -1,4 +1,5 @@ import {Delimiters} from "./delimiters"; +import {Message} from "./message"; interface INode { @@ -11,11 +12,25 @@ interface INode { export class Node implements INode { - _path: string[] + protected parent: Node | null; + + private _children: Node[]; + private _message: Message; + private _delimiterText: string; + private _path: string[]; + private _text: string; // @ts-ignore constructor( parent: Node | null, text: string, delimiter: Delimiters = undefined) { + this.parent = parent + this._children = [] + this._path = [] + this._text = text + this._delimiterText = "" + this._message = new Message() + + } set(path: string | number, value?: any): Node { @@ -79,36 +94,31 @@ export class Node implements INode { } - // @ts-ignore protected preparePath(path: string): string[] { let parts = path.split("."); if (parts[0] == "") { parts.shift(); parts = this.path.concat(parts); } - if (!this._isSubPath(parts)) { throw new Error("'" + parts.toString() + "' is not a sub-path of '" + this.path.toString() + "'"); } - return this._remainderOf(parts); } private _isSubPath(other: string[]): boolean { - if(this.path.length >= other.length) return false; - - var path = this.path; - for(var i = 0, l = path.length; i < l; i++) { - if(path[i] != other[i]) return false; + let path = this.path; + for (let i = 0, l = path.length; i < l; i++) { + if (path[i] != other[i]) { + return false; + } } - return true; } private _remainderOf(other: string[]): string[] { - - var path = this.path; + let path = this.path; return other.slice(path.length); } @@ -130,9 +140,39 @@ export class Node implements INode { } protected addChild(text: string): Node { - console.log(text) - return this + let child = this.createChild(text, this.children.length); + this.children.push(child); + return child; + } + + protected createChild(_text: string, _index: number): Node { + throw new Error("Not implemented"); + } + + protected get delimiter(): string { + + if(this._delimiterText) return this._delimiterText; + return this._delimiterText = this.message.delimiters[this._delimiter]; + } + + protected get message(): Message { + + if(this._message) { + return this._message; + } + return this._message = this.parent ? this.parent.message : this; } + protected get children(): Node[] { + if (!this._children) { + let parts = this._text.split(this.delimiter); + let children = new Array(parts.length); + for (let i = 0, l = parts.length; i < l; i++) { + children[i] = this.createChild(parts[i], i); + } + this._children = children; + } + return this._children; + } } \ No newline at end of file diff --git a/src/segment.ts b/src/segment.ts index 22d46ac..1e65d1e 100644 --- a/src/segment.ts +++ b/src/segment.ts @@ -1,4 +1,5 @@ import {Delimiters} from "./delimiters"; +import {Field} from "./field"; import { Node } from "./node"; export class Segment extends Node { @@ -7,4 +8,8 @@ export class Segment extends Node { super(parent, text, Delimiters.Field); } + protected createChild(text: string, index: number): Field { + return new Field(this, index.toString(), text); + } + } \ No newline at end of file diff --git a/src/valueNode.ts b/src/valueNode.ts new file mode 100644 index 0000000..ef3b3bb --- /dev/null +++ b/src/valueNode.ts @@ -0,0 +1,15 @@ +import {Delimiters} from "./delimiters"; +import { Node } from "./node"; + + +export class ValueNode extends Node { + + key: string; + + constructor(parent: Node, key: string, text: string, delimiter: Delimiters) { + super(parent, text, delimiter); + + this.key = key; + } + +} \ No newline at end of file