Skip to content

Commit

Permalink
feat(experiential): new design method x3
Browse files Browse the repository at this point in the history
[ci skip]
  • Loading branch information
Bugs5382 committed Dec 9, 2023
1 parent a8d0bec commit 87ddc8a
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 17 deletions.
14 changes: 14 additions & 0 deletions src/field.ts
Original file line number Diff line number Diff line change
@@ -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)

}

}
13 changes: 10 additions & 3 deletions src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {createDateTime} from "./utils";
export class Message extends Node {

private _opt: ReturnType<typeof normalizedClientBuilderOptions>;
private readonly _delimiters: string;

/**
* @param text If not provided, it will be MSH plus the default encoding characters.
Expand All @@ -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()
Expand All @@ -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 <Segment>this.addChild(preparedPath[0]);
}

Expand All @@ -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;
}

}
68 changes: 54 additions & 14 deletions src/node.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Delimiters} from "./delimiters";
import {Message} from "./message";

interface INode {

Expand All @@ -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 {
Expand Down Expand Up @@ -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);
}

Expand All @@ -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 : <any>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;
}

}
5 changes: 5 additions & 0 deletions src/segment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Delimiters} from "./delimiters";
import {Field} from "./field";
import { Node } from "./node";

export class Segment extends Node {
Expand All @@ -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);
}

}
15 changes: 15 additions & 0 deletions src/valueNode.ts
Original file line number Diff line number Diff line change
@@ -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;
}

}

0 comments on commit 87ddc8a

Please sign in to comment.