hl7-tstd 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,457 +1,399 @@
1
- class Node {
2
- constructor(data, next = null, prev = null) {
3
- this.data = data;
4
- this.next = next;
5
- this.prev = prev;
6
- }
7
- }
8
- class Segment {
9
- constructor(type, data = null, parseOptions) {
10
- var _a, _b, _c, _d;
11
- this.type = type;
12
- this.data = data;
13
- this.parseOptions = {
14
- fieldDelim: (_a = parseOptions === null || parseOptions === void 0 ? void 0 : parseOptions.fieldDelim) !== null && _a !== void 0 ? _a : '|',
15
- repeatingDelim: (_b = parseOptions === null || parseOptions === void 0 ? void 0 : parseOptions.repeatingDelim) !== null && _b !== void 0 ? _b : '~',
16
- componentDelim: (_c = parseOptions === null || parseOptions === void 0 ? void 0 : parseOptions.componentDelim) !== null && _c !== void 0 ? _c : '^',
17
- subCompDelim: (_d = parseOptions === null || parseOptions === void 0 ? void 0 : parseOptions.subCompDelim) !== null && _d !== void 0 ? _d : '&',
18
- };
19
- }
20
- get(field, repeatingIndex = 0, subComponentIndex = 0) {
21
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
22
- if (!this.data)
23
- return null;
24
- if (!field || !/^[A-Z\d]{3}(\.\d+){0,2}$/.test(field))
25
- throw new Error(`Invalid field: '${field}'`);
26
- if (typeof repeatingIndex !== 'number' && repeatingIndex < -1)
27
- throw new Error(`Invalid Repeating index: '${repeatingIndex}' (type:${typeof repeatingIndex})`);
28
- if (typeof subComponentIndex !== 'number' && subComponentIndex < -1)
29
- throw new Error(`Invalid Subcomponent index: '${subComponentIndex}' (type:${typeof subComponentIndex})`);
30
- const [type, fieldIdx, compIdx] = field.split('.');
31
- if (type !== this.type)
32
- throw new Error(`Invalid field: '${field}'`);
33
- if (compIdx && repeatingIndex < 0)
34
- throw new Error(`Invalid Repeating index: '${repeatingIndex}' (type:${typeof repeatingIndex})\nCannot get All repeating fields when getting component ${field}`);
35
- const { fieldDelim, repeatingDelim, componentDelim, subCompDelim } = this.parseOptions;
36
- if (fieldIdx && compIdx) {
37
- if (subComponentIndex === -1) {
38
- const component = (_b = (_a = this.data[`${type}.${fieldIdx}`]) === null || _a === void 0 ? void 0 : _a[repeatingIndex]) === null || _b === void 0 ? void 0 : _b[`${type}.${fieldIdx}.${compIdx}`];
39
- if (!component)
40
- return null;
41
- return (_c = component.join(subCompDelim)) !== null && _c !== void 0 ? _c : null;
42
- }
43
- else {
44
- return ((_g = (_f = (_e = (_d = this.data[`${type}.${fieldIdx}`]) === null || _d === void 0 ? void 0 : _d[repeatingIndex]) === null || _e === void 0 ? void 0 : _e[`${type}.${fieldIdx}.${compIdx}`]) === null || _f === void 0 ? void 0 : _f[subComponentIndex]) !== null && _g !== void 0 ? _g : null);
45
- }
46
- }
47
- else if (fieldIdx) {
48
- if (repeatingIndex === -1) {
49
- const field = this.data[`${type}.${fieldIdx}`];
50
- const repeatingFieldsArr = [];
51
- if (!field)
52
- return null;
53
- for (const repeatingField of field) {
54
- const componentsArr = [];
55
- for (const compKey in repeatingField) {
56
- componentsArr.push(repeatingField[compKey].join(subCompDelim));
57
- }
58
- repeatingFieldsArr.push(componentsArr.join(componentDelim));
59
- }
60
- return repeatingFieldsArr.join(repeatingDelim);
61
- }
62
- else {
63
- const component = (_h = this.data[`${type}.${fieldIdx}`]) === null || _h === void 0 ? void 0 : _h[repeatingIndex];
64
- if (!component)
65
- return null;
66
- const componentsArr = [];
67
- for (const compKey in component) {
68
- componentsArr.push((_j = component[compKey]) === null || _j === void 0 ? void 0 : _j.join(subCompDelim));
69
- }
70
- return componentsArr.join(componentDelim);
71
- }
1
+ // src/index.ts
2
+ var Node = class {
3
+ constructor(data, next = null, prev = null) {
4
+ this.data = data;
5
+ this.next = next;
6
+ this.prev = prev;
7
+ }
8
+ };
9
+ var Segment = class {
10
+ constructor(type, data = null, parseOptions) {
11
+ this.type = type;
12
+ this.data = data;
13
+ var _a, _b, _c, _d;
14
+ this.parseOptions = {
15
+ fieldDelim: (_a = parseOptions == null ? void 0 : parseOptions.fieldDelim) != null ? _a : "|",
16
+ repeatingDelim: (_b = parseOptions == null ? void 0 : parseOptions.repeatingDelim) != null ? _b : "~",
17
+ componentDelim: (_c = parseOptions == null ? void 0 : parseOptions.componentDelim) != null ? _c : "^",
18
+ subCompDelim: (_d = parseOptions == null ? void 0 : parseOptions.subCompDelim) != null ? _d : "&"
19
+ };
20
+ }
21
+ get(field, repeatingIndex = 0, subComponentIndex = 0) {
22
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
23
+ if (!this.data) return null;
24
+ if (!field || !/^[A-Z\d]{3}(\.\d+){0,2}$/.test(field))
25
+ throw new Error(`Invalid parameter: 'field' [${field}]`);
26
+ if (typeof repeatingIndex !== "number" || repeatingIndex < -1)
27
+ throw new Error(`Invalid parameter: 'repeatingIndex' [${repeatingIndex}]`);
28
+ if (typeof subComponentIndex !== "number" || subComponentIndex < -1)
29
+ throw new Error(`Invalid parameter: 'subComponentIndex' [${subComponentIndex}]`);
30
+ const { fieldDelim, repeatingDelim, componentDelim, subCompDelim } = this.parseOptions;
31
+ const [type, fieldIdx, compIdx] = field.split(".");
32
+ if (!type || type !== this.type)
33
+ throw new Error(
34
+ `Invalid parameter: 'field' [${field}]. Cannot get [${field}] from [${this.type}] segment.`
35
+ );
36
+ if (fieldIdx && compIdx) {
37
+ if (subComponentIndex === -1) {
38
+ return (_d = (_c = (_b = (_a = this.data[`${type}.${fieldIdx}`]) == null ? void 0 : _a[repeatingIndex]) == null ? void 0 : _b[`${type}.${fieldIdx}.${compIdx}`]) == null ? void 0 : _c.join(subCompDelim)) != null ? _d : null;
39
+ } else {
40
+ return (_h = (_g = (_f = (_e = this.data[`${type}.${fieldIdx}`]) == null ? void 0 : _e[repeatingIndex]) == null ? void 0 : _f[`${type}.${fieldIdx}.${compIdx}`]) == null ? void 0 : _g[subComponentIndex]) != null ? _h : null;
41
+ }
42
+ } else if (fieldIdx) {
43
+ let field2 = repeatingIndex === -1 ? this.data[`${type}.${fieldIdx}`] : [(_i = this.data[`${type}.${fieldIdx}`]) == null ? void 0 : _i[repeatingIndex]];
44
+ const repeatingFieldsArr = [];
45
+ if (!field2 || !field2[0]) return null;
46
+ for (const repeatingField of field2) {
47
+ const componentsArr = [];
48
+ for (const compKey in repeatingField) {
49
+ componentsArr.push(repeatingField[compKey].join(subCompDelim));
72
50
  }
73
- else {
74
- if (!this.data)
75
- return null;
76
- const fieldsArr = [this.type];
77
- for (const fieldKey in this.data) {
78
- const repeatingCompsArr = [];
79
- for (const repeatingField of this.data[fieldKey]) {
80
- const compsArr = [];
81
- for (const compKey in repeatingField) {
82
- compsArr.push((_k = repeatingField[compKey]) === null || _k === void 0 ? void 0 : _k.join(subCompDelim));
83
- }
84
- repeatingCompsArr.push(compsArr.join(componentDelim));
85
- }
86
- fieldsArr.push(repeatingCompsArr.join(repeatingDelim));
87
- }
88
- return fieldsArr.join(fieldDelim);
51
+ repeatingFieldsArr.push(componentsArr.join(componentDelim));
52
+ }
53
+ return repeatingFieldsArr.join(repeatingDelim);
54
+ } else {
55
+ if (!this.data) return null;
56
+ const fieldsArr = [this.type];
57
+ for (const fieldKey in this.data) {
58
+ const repeatingCompsArr = [];
59
+ for (const repeatingField of this.data[fieldKey]) {
60
+ const compsArr = [];
61
+ for (const compKey in repeatingField) {
62
+ compsArr.push((_j = repeatingField[compKey]) == null ? void 0 : _j.join(subCompDelim));
63
+ }
64
+ repeatingCompsArr.push(compsArr.join(componentDelim));
89
65
  }
66
+ fieldsArr.push(repeatingCompsArr.join(repeatingDelim));
67
+ }
68
+ return fieldsArr.join(fieldDelim);
90
69
  }
91
- set(field, value, repeatingIndex = 0, subComponentIndex = 0) {
92
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
93
- if (!field || !/^[A-Z\d]{3}(\.\d+){1,2}$/.test(field))
94
- throw new Error(`Invalid field: '${field}'`);
95
- if (typeof value !== 'string')
96
- throw new Error(`Invalid value: '${value}' (type:${typeof value})`);
97
- if (typeof repeatingIndex !== 'number' && repeatingIndex < 0)
98
- throw new Error(`Invalid Repeating index: '${repeatingIndex}' (type:${typeof repeatingIndex})`);
99
- if (typeof subComponentIndex !== 'number' && subComponentIndex < 0)
100
- throw new Error(`Invalid Subcomponent index: '${subComponentIndex}' (type:${typeof subComponentIndex})`);
101
- const [type, fieldIdx, compIdx] = field.split('.');
102
- if (!type || type !== this.type)
103
- throw new Error(`Invalid type: '${type}'. Cannot set ${type} in ${this.type} Segment`);
104
- if (!fieldIdx)
105
- throw new Error('Invalid field'); // TODO ----------------
106
- if (!this.data)
107
- this.data = {};
108
- for (let i = type === 'MSH' ? 2 : 1; i <= parseInt(fieldIdx, 10); i++) {
109
- if (((_b = (_a = this.data[`${type}.${i}`]) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b[`${type}.${i}.${1}`]) == null)
110
- this.data[`${type}.${i}`] = [{ [`${type}.${i}.${1}`]: [''] }];
111
- }
112
- if (repeatingIndex === -1) {
113
- this.data[`${type}.${fieldIdx}`] = [{ [`${type}.${fieldIdx}.${1}`]: [''] }];
114
- }
115
- else {
116
- for (let i = 0; i <= repeatingIndex; i++) {
117
- if (((_d = (_c = this.data[`${type}.${fieldIdx}`]) === null || _c === void 0 ? void 0 : _c[i]) === null || _d === void 0 ? void 0 : _d[`${type}.${fieldIdx}.${1}`]) == null)
118
- this.data[`${type}.${fieldIdx}`][i] = {
119
- [`${type}.${fieldIdx}.${1}`]: [''],
120
- };
121
- }
122
- }
123
- if (compIdx) {
124
- for (let i = 1; i <= parseInt(compIdx, 10); i++) {
125
- if (((_f = (_e = this.data[`${type}.${fieldIdx}`]) === null || _e === void 0 ? void 0 : _e[repeatingIndex]) === null || _f === void 0 ? void 0 : _f[`${type}.${fieldIdx}.${i}`]) == null)
126
- this.data[`${type}.${fieldIdx}`][repeatingIndex][`${type}.${fieldIdx}.${i}`] = [''];
127
- }
128
- for (let i = 0; i <= subComponentIndex; i++) {
129
- if (((_j = (_h = (_g = this.data[`${type}.${fieldIdx}`]) === null || _g === void 0 ? void 0 : _g[repeatingIndex]) === null || _h === void 0 ? void 0 : _h[`${type}.${fieldIdx}.${compIdx}`]) === null || _j === void 0 ? void 0 : _j[i]) == null)
130
- this.data[`${type}.${fieldIdx}`][repeatingIndex][`${type}.${fieldIdx}.${compIdx}`][i] =
131
- '';
132
- }
133
- this.data[`${type}.${fieldIdx}`][repeatingIndex][`${type}.${fieldIdx}.${compIdx}`][subComponentIndex] = value;
134
- }
135
- else {
136
- this.data[`${type}.${fieldIdx}`][repeatingIndex] = {
137
- [`${type}.${fieldIdx}.${1}`]: [value],
138
- };
139
- }
70
+ }
71
+ set(field, value, repeatingIndex = 0, subComponentIndex = 0) {
72
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
73
+ if (!field || !/^[A-Z\d]{3}(\.\d+){1,2}$/.test(field))
74
+ throw new Error(`Invalid parameter: 'field' [${field}]`);
75
+ if (!value || typeof value !== "string")
76
+ throw new Error(`Invalid parameter: 'value' [${value}]`);
77
+ if (typeof repeatingIndex !== "number" && repeatingIndex < 0)
78
+ throw new Error(`Invalid parameter: 'repeatingIndex' [${repeatingIndex}]`);
79
+ if (typeof subComponentIndex !== "number" && subComponentIndex < 0)
80
+ throw new Error(`Invalid parameter: 'subComponentIndex' [${subComponentIndex}]`);
81
+ const [type, fieldIdx, compIdx] = field.split(".");
82
+ if (!type || type !== this.type)
83
+ throw new Error(
84
+ `Invalid parameter: 'field' [${field}]. Cannot set [${field}] from [${this.type}] segment.`
85
+ );
86
+ if (!fieldIdx) throw new Error(`Invalid parameter: 'field' [${field}]`);
87
+ if (!this.data) this.data = {};
88
+ for (let i = type === "MSH" ? 2 : 1; i <= parseInt(fieldIdx, 10); i++) {
89
+ if (((_b = (_a = this.data[`${type}.${i}`]) == null ? void 0 : _a[0]) == null ? void 0 : _b[`${type}.${i}.${1}`]) == null)
90
+ this.data[`${type}.${i}`] = [{ [`${type}.${i}.${1}`]: [""] }];
140
91
  }
141
- }
142
- class HL7 {
143
- constructor(hl7Msg, parseOptions) {
144
- var _a, _b, _c, _d, _e, _f;
145
- this.head = null;
146
- this.tail = null;
147
- this.raw = typeof hl7Msg === 'string' ? hl7Msg : '';
148
- this.parseOptions = {
149
- fieldDelim: (_a = parseOptions === null || parseOptions === void 0 ? void 0 : parseOptions.fieldDelim) !== null && _a !== void 0 ? _a : '|',
150
- repeatingDelim: (_b = parseOptions === null || parseOptions === void 0 ? void 0 : parseOptions.repeatingDelim) !== null && _b !== void 0 ? _b : '~',
151
- componentDelim: (_c = parseOptions === null || parseOptions === void 0 ? void 0 : parseOptions.componentDelim) !== null && _c !== void 0 ? _c : '^',
152
- subCompDelim: (_d = parseOptions === null || parseOptions === void 0 ? void 0 : parseOptions.subCompDelim) !== null && _d !== void 0 ? _d : '&',
153
- eolDelim: (_e = parseOptions === null || parseOptions === void 0 ? void 0 : parseOptions.eolDelim) !== null && _e !== void 0 ? _e : '\r?\n|\r',
154
- buildEolChar: (_f = parseOptions === null || parseOptions === void 0 ? void 0 : parseOptions.buildEolChar) !== null && _f !== void 0 ? _f : '\r\n',
155
- };
156
- this.transform();
92
+ for (let i = 0; i <= repeatingIndex; i++) {
93
+ if (((_d = (_c = this.data[`${type}.${fieldIdx}`]) == null ? void 0 : _c[i]) == null ? void 0 : _d[`${type}.${fieldIdx}.${1}`]) == null)
94
+ this.data[`${type}.${fieldIdx}`][i] = { [`${type}.${fieldIdx}.${1}`]: [""] };
157
95
  }
158
- transform() {
159
- if (!this.raw.startsWith('MSH')) {
160
- throw new Error('Expected raw msg to be HL7 message. Message does not start with MSH');
161
- }
162
- const segmentsStr = this.raw
163
- .split(RegExp(this.parseOptions.eolDelim))
164
- .filter((segmentStr) => /^[A-Z\d]{3}\|/.test(segmentStr));
165
- const { fieldDelim, repeatingDelim, componentDelim, subCompDelim } = this.parseOptions;
166
- for (const segmentStr of segmentsStr) {
167
- const type = segmentStr.substring(0, 3);
168
- const segment = new Segment(type, null, this.parseOptions);
169
- const fields = segmentStr.split(fieldDelim);
170
- segment.data = {};
171
- for (let i = 1; i < fields.length; i++) {
172
- const fieldIdx = type === 'MSH' ? i + 1 : i;
173
- const repeatings = fields[i].split(repeatingDelim);
174
- segment.data[`${type}.${fieldIdx}`] = [];
175
- for (let j = 0; j < repeatings.length; j++) {
176
- const components = repeatings[j].split(componentDelim);
177
- segment.data[`${type}.${fieldIdx}`][j] = {};
178
- for (let k = 0; k < components.length; k++) {
179
- const subComps = components[k].split(subCompDelim);
180
- segment.data[`${type}.${fieldIdx}`][j][`${type}.${fieldIdx}.${k + 1}`] = [];
181
- for (let l = 0; l < subComps.length; l++) {
182
- segment.data[`${type}.${fieldIdx}`][j][`${type}.${fieldIdx}.${k + 1}`][l] =
183
- subComps[l];
184
- }
185
- }
186
- }
187
- }
188
- if (type === 'MSH') {
189
- segment.data['MSH.2'] = [
190
- {
191
- 'MSH.2.1': [`${componentDelim}${repeatingDelim}\\${subCompDelim}`],
192
- },
193
- ];
194
- }
195
- this.appendSegmentNode(segment);
196
- }
96
+ if (compIdx) {
97
+ for (let i = 1; i <= parseInt(compIdx, 10); i++) {
98
+ if (((_f = (_e = this.data[`${type}.${fieldIdx}`]) == null ? void 0 : _e[repeatingIndex]) == null ? void 0 : _f[`${type}.${fieldIdx}.${i}`]) == null)
99
+ this.data[`${type}.${fieldIdx}`][repeatingIndex][`${type}.${fieldIdx}.${i}`] = [""];
100
+ }
101
+ for (let i = 0; i <= subComponentIndex; i++) {
102
+ if (((_i = (_h = (_g = this.data[`${type}.${fieldIdx}`]) == null ? void 0 : _g[repeatingIndex]) == null ? void 0 : _h[`${type}.${fieldIdx}.${compIdx}`]) == null ? void 0 : _i[i]) == null)
103
+ this.data[`${type}.${fieldIdx}`][repeatingIndex][`${type}.${fieldIdx}.${compIdx}`][i] = "";
104
+ }
105
+ this.data[`${type}.${fieldIdx}`][repeatingIndex][`${type}.${fieldIdx}.${compIdx}`][subComponentIndex] = value;
106
+ } else {
107
+ this.data[`${type}.${fieldIdx}`][repeatingIndex] = {
108
+ [`${type}.${fieldIdx}.${1}`]: [value]
109
+ };
197
110
  }
198
- build() {
199
- var _a;
200
- const segments = this.getSegments();
201
- const { fieldDelim, repeatingDelim, componentDelim, subCompDelim, buildEolChar } = this.parseOptions;
202
- const segmentsStrArr = [];
203
- for (const segment of segments) {
204
- const fieldsArr = [segment.type];
205
- for (const fieldKey in segment.data) {
206
- const repeatingCompsArr = [];
207
- for (const component of segment.data[fieldKey]) {
208
- const compsArr = [];
209
- for (const subCompsKey in component) {
210
- compsArr.push((_a = component[subCompsKey]) === null || _a === void 0 ? void 0 : _a.join(subCompDelim));
211
- }
212
- repeatingCompsArr.push(compsArr.join(componentDelim));
213
- }
214
- fieldsArr.push(repeatingCompsArr.join(repeatingDelim));
215
- }
216
- segmentsStrArr.push(fieldsArr.join(fieldDelim));
217
- }
218
- return segmentsStrArr.join(buildEolChar);
219
- }
220
- getSegment(type) {
221
- if (!/^[A-Z\d]{3}$/.test(type))
222
- throw new Error(`Invalid Segment type: '${type}'`);
223
- let curr = this.head;
224
- while (curr) {
225
- if (curr.data.type === type) {
226
- return curr.data;
227
- }
228
- curr = curr.next;
229
- }
230
- return curr;
111
+ }
112
+ };
113
+ var HL7 = class {
114
+ constructor(hl7Msg, parseOptions) {
115
+ this.head = null;
116
+ this.tail = null;
117
+ var _a, _b, _c, _d, _e, _f;
118
+ this.raw = typeof hl7Msg === "string" ? hl7Msg : "";
119
+ this.parseOptions = {
120
+ fieldDelim: (_a = parseOptions == null ? void 0 : parseOptions.fieldDelim) != null ? _a : "|",
121
+ repeatingDelim: (_b = parseOptions == null ? void 0 : parseOptions.repeatingDelim) != null ? _b : "~",
122
+ componentDelim: (_c = parseOptions == null ? void 0 : parseOptions.componentDelim) != null ? _c : "^",
123
+ subCompDelim: (_d = parseOptions == null ? void 0 : parseOptions.subCompDelim) != null ? _d : "&",
124
+ eolDelim: (_e = parseOptions == null ? void 0 : parseOptions.eolDelim) != null ? _e : "\r?\n|\r",
125
+ buildEolChar: (_f = parseOptions == null ? void 0 : parseOptions.buildEolChar) != null ? _f : "\r\n"
126
+ };
127
+ this.transform();
128
+ }
129
+ /**
130
+ * @deprecated This method is triggered internally and doesnt need to be invoked manually.
131
+ */
132
+ transform() {
133
+ if (!this.raw.startsWith("MSH")) {
134
+ throw new Error(
135
+ "Expected raw msg to be HL7 message. Message does not start with MSH segment."
136
+ );
231
137
  }
232
- getSegments(type) {
233
- if (type && !/^[A-Z\d]{3}$/.test(type))
234
- throw new Error(`Invalid Segment type: '${type}'`);
235
- const segments = [];
236
- let curr = this.head;
237
- while (curr) {
238
- if (type) {
239
- if (type === curr.data.type) {
240
- segments.push(curr.data);
241
- }
138
+ const segmentsStr = this.raw.split(RegExp(this.parseOptions.eolDelim)).filter((segmentStr) => /^[A-Z\d]{3}\|/.test(segmentStr));
139
+ const { fieldDelim, repeatingDelim, componentDelim, subCompDelim } = this.parseOptions;
140
+ for (const segmentStr of segmentsStr) {
141
+ const type = segmentStr.substring(0, 3);
142
+ const segment = new Segment(type, null, this.parseOptions);
143
+ const fields = segmentStr.split(fieldDelim);
144
+ segment.data = {};
145
+ for (let i = 1; i < fields.length; i++) {
146
+ const fieldIdx = type === "MSH" ? i + 1 : i;
147
+ const repeatingComps = fields[i].split(repeatingDelim);
148
+ segment.data[`${type}.${fieldIdx}`] = [];
149
+ for (let j = 0; j < repeatingComps.length; j++) {
150
+ const components = repeatingComps[j].split(componentDelim);
151
+ segment.data[`${type}.${fieldIdx}`][j] = {};
152
+ for (let k = 0; k < components.length; k++) {
153
+ const subComps = components[k].split(subCompDelim);
154
+ segment.data[`${type}.${fieldIdx}`][j][`${type}.${fieldIdx}.${k + 1}`] = [];
155
+ for (let l = 0; l < subComps.length; l++) {
156
+ segment.data[`${type}.${fieldIdx}`][j][`${type}.${fieldIdx}.${k + 1}`][l] = subComps[l];
242
157
  }
243
- else {
244
- segments.push(curr.data);
245
- }
246
- curr = curr.next;
158
+ }
247
159
  }
248
- return segments;
160
+ }
161
+ if (type === "MSH") {
162
+ segment.data["MSH.2"] = [
163
+ { "MSH.2.1": [`${componentDelim}${repeatingDelim}\\${subCompDelim}`] }
164
+ ];
165
+ }
166
+ this.appendSegmentNode(segment);
249
167
  }
250
- getSegmentsAfter(startSegment, type, stopSegmentType, consecutive = false) {
251
- if (!startSegment || !(startSegment instanceof Segment))
252
- throw new Error(`Invalid Target Segment: ${JSON.stringify(startSegment)}`);
253
- if (!/^[A-Z\d]{3}$/.test(type))
254
- throw new Error(`Invalid Segment type: '${type}'`);
255
- if (typeof consecutive !== 'boolean')
256
- throw new Error(`Invalid Consecutive flag: '${consecutive}'`);
257
- const startNode = this.getNode(startSegment);
258
- if (!startNode)
259
- throw new Error(`Failed to locate Start Segment: '${startSegment.type}`);
260
- const res = [];
261
- let curr = startNode.next;
262
- while (curr) {
263
- if (stopSegmentType.includes(curr.data.type))
264
- break;
265
- if (curr.data.type === type)
266
- res.push(curr.data);
267
- else if (consecutive && res.length)
268
- break;
269
- curr = curr.next;
168
+ }
169
+ build() {
170
+ var _a;
171
+ const segments = this.getSegments();
172
+ const { fieldDelim, repeatingDelim, componentDelim, subCompDelim, buildEolChar } = this.parseOptions;
173
+ const segmentsStrArr = [];
174
+ for (const segment of segments) {
175
+ const fieldsArr = [segment.type];
176
+ for (const fieldKey in segment.data) {
177
+ const repeatingCompsArr = [];
178
+ for (const component of segment.data[fieldKey]) {
179
+ const compsArr = [];
180
+ for (const subCompsKey in component) {
181
+ compsArr.push((_a = component[subCompsKey]) == null ? void 0 : _a.join(subCompDelim));
182
+ }
183
+ repeatingCompsArr.push(compsArr.join(componentDelim));
270
184
  }
271
- return res;
185
+ fieldsArr.push(repeatingCompsArr.join(repeatingDelim));
186
+ }
187
+ segmentsStrArr.push(fieldsArr.join(fieldDelim));
272
188
  }
273
- createSegment(type) {
274
- if (!/^[A-Z\d]{3}$/.test(type))
275
- throw new Error(`Invalid Segment type: '${type}'`);
276
- return this.appendSegmentNode(new Segment(type, null, this.parseOptions));
189
+ return segmentsStrArr.join(buildEolChar);
190
+ }
191
+ getSegment(type) {
192
+ if (!/^[A-Z\d]{3}$/.test(type)) throw new Error(`Invalid parameter: 'type' [${type}]`);
193
+ let curr = this.head;
194
+ while (curr) {
195
+ if (curr.data.type === type) return curr.data;
196
+ curr = curr.next;
277
197
  }
278
- createSegmentAfter(type, targetSegment) {
279
- if (!/^[A-Z\d]{3}$/.test(type))
280
- throw new Error(`Invalid Segment type: '${type}'`);
281
- if (!targetSegment || !(targetSegment instanceof Segment))
282
- throw new Error(`Invalid Target Segment : ${JSON.stringify(targetSegment)}`);
283
- return this.appendSegmentNode(new Segment(type, null, this.parseOptions), targetSegment);
198
+ return curr;
199
+ }
200
+ getSegments(type) {
201
+ if (type && !/^[A-Z\d]{3}$/.test(type)) throw new Error(`Invalid parameter: 'type' [${type}]`);
202
+ const segments = [];
203
+ let curr = this.head;
204
+ while (curr) {
205
+ if (type) {
206
+ if (type === curr.data.type) segments.push(curr.data);
207
+ } else {
208
+ segments.push(curr.data);
209
+ }
210
+ curr = curr.next;
284
211
  }
285
- createSegmentBefore(type, targetSegment) {
286
- if (!/^[A-Z\d]{3}$/.test(type))
287
- throw new Error(`Invalid Segment type: '${type}'`);
288
- if (!targetSegment || !(targetSegment instanceof Segment))
289
- throw new Error(`Invalid Target Segment: ${JSON.stringify(targetSegment)}`);
290
- return this.prependSegmentNode(new Segment(type, null, this.parseOptions), targetSegment);
212
+ return segments;
213
+ }
214
+ getSegmentsAfter(startSegment, type, stopSegmentType = [], consecutive = false) {
215
+ if (!startSegment || !(startSegment instanceof Segment))
216
+ throw new Error(`Invalid parameter: 'startSegment'`);
217
+ if (!/^[A-Z\d]{3}$/.test(type)) throw new Error(`Invalid parameter: 'type' [${type}]`);
218
+ if (!stopSegmentType || !Array.isArray(stopSegmentType))
219
+ throw new Error(`Invalid parameter: 'stopSegmentType'`);
220
+ if (typeof consecutive !== "boolean")
221
+ throw new Error(`Invalid parameter: 'consecutive' [${consecutive}]`);
222
+ const startNode = this.getNode(startSegment);
223
+ if (!startNode) throw new Error(`Failed to locate: 'startSegment' [${startSegment.type}]`);
224
+ const res = [];
225
+ let curr = startNode.next;
226
+ while (curr) {
227
+ if (stopSegmentType.length && stopSegmentType.includes(curr.data.type)) break;
228
+ if (curr.data.type === type) res.push(curr.data);
229
+ else if (consecutive && res.length) break;
230
+ curr = curr.next;
291
231
  }
292
- deleteSegment(segment) {
293
- if (!segment || !(segment instanceof Segment))
294
- throw new Error(`Invalid Segment: ${JSON.stringify(segment)}`);
295
- this.deleteSegments([segment]);
232
+ return res;
233
+ }
234
+ createSegment(type) {
235
+ if (!/^[A-Z\d]{3}$/.test(type)) throw new Error(`Invalid parameter: 'type' [${type}]`);
236
+ return this.appendSegmentNode(new Segment(type, null, this.parseOptions));
237
+ }
238
+ createSegmentAfter(type, targetSegment) {
239
+ if (!/^[A-Z\d]{3}$/.test(type)) throw new Error(`Invalid parameter: 'type' [${type}]`);
240
+ if (!targetSegment || !(targetSegment instanceof Segment))
241
+ throw new Error(`Invalid parameter: 'targetSegment'`);
242
+ return this.appendSegmentNode(new Segment(type, null, this.parseOptions), targetSegment);
243
+ }
244
+ createSegmentBefore(type, targetSegment) {
245
+ if (!/^[A-Z\d]{3}$/.test(type)) throw new Error(`Invalid parameter: 'type' [${type}]`);
246
+ if (!targetSegment || !(targetSegment instanceof Segment))
247
+ throw new Error(`Invalid parameter: 'targetSegment'`);
248
+ return this.prependSegmentNode(new Segment(type, null, this.parseOptions), targetSegment);
249
+ }
250
+ deleteSegment(segment) {
251
+ if (!segment || !(segment instanceof Segment)) throw new Error(`Invalid parameter: 'segment'`);
252
+ this.deleteSegments([segment]);
253
+ }
254
+ deleteSegments(segments) {
255
+ var _a, _b;
256
+ for (const segment of segments) {
257
+ if (!segment || !(segment instanceof Segment))
258
+ throw new Error(`Invalid parameter: 'segments'`);
296
259
  }
297
- deleteSegments(segments) {
298
- var _a, _b;
299
- for (const segment of segments) {
300
- if (!segment || !(segment instanceof Segment))
301
- throw new Error(`Invalid Segment: ${JSON.stringify(segment)}`);
302
- const node = this.getNode(segment);
303
- if (!node) {
304
- break;
305
- }
306
- if ((_a = node.prev) === null || _a === void 0 ? void 0 : _a.next)
307
- node.prev.next = node.next;
308
- if ((_b = node.next) === null || _b === void 0 ? void 0 : _b.prev)
309
- node.next.prev = node.prev;
310
- if (this.head === node)
311
- this.head = this.head.next;
312
- if (this.tail === node)
313
- this.tail = this.tail.prev;
314
- node.next = null;
315
- node.prev = null;
316
- }
260
+ for (const segment of segments) {
261
+ const node = this.getNode(segment);
262
+ if (!node) break;
263
+ if ((_a = node.prev) == null ? void 0 : _a.next) node.prev.next = node.next;
264
+ if ((_b = node.next) == null ? void 0 : _b.prev) node.next.prev = node.prev;
265
+ if (this.head === node) this.head = this.head.next;
266
+ if (this.tail === node) this.tail = this.tail.prev;
267
+ node.next = null;
268
+ node.prev = null;
317
269
  }
318
- moveSegmentAfter(segment, targetSegment) {
319
- var _a, _b, _c;
320
- if (!segment || !(segment instanceof Segment))
321
- throw new Error(`Invalid Segment: ${JSON.stringify(segment)}`);
322
- if (!targetSegment || !(targetSegment instanceof Segment))
323
- throw new Error(`Invalid Target Segment: ${JSON.stringify(targetSegment)}`);
324
- const node = this.getNode(segment);
325
- if (!node)
326
- throw new Error(`Failed to locate Segment: '${segment.type}'`);
327
- const targetNode = this.getNode(targetSegment);
328
- if (!targetNode)
329
- throw new Error(`Failed to locate Target Segment: '${targetSegment.type}'`);
330
- if ((_a = node.prev) === null || _a === void 0 ? void 0 : _a.next)
331
- node.prev.next = node.next;
332
- if ((_b = node.next) === null || _b === void 0 ? void 0 : _b.prev)
333
- node.next.prev = node.prev;
334
- node.next = targetNode.next;
335
- node.prev = targetNode;
336
- targetNode.next = node;
337
- if ((_c = node.next) === null || _c === void 0 ? void 0 : _c.prev)
338
- node.next.prev = node;
339
- if (targetNode === this.tail)
340
- this.tail = this.tail.next;
270
+ }
271
+ moveSegmentAfter(segment, targetSegment) {
272
+ var _a, _b, _c;
273
+ if (!segment || !(segment instanceof Segment)) throw new Error(`Invalid parameter: 'segment'`);
274
+ if (!targetSegment || !(targetSegment instanceof Segment))
275
+ throw new Error(`Invalid parameter: 'targetSegment'`);
276
+ if (segment === targetSegment) return;
277
+ const node = this.getNode(segment);
278
+ if (!node) throw new Error(`Failed to locate: 'segment' [${segment.type}]`);
279
+ const targetNode = this.getNode(targetSegment);
280
+ if (!targetNode) throw new Error(`Failed to locate: 'targetSegment' [${targetSegment.type}]`);
281
+ if (node === this.head) this.head = this.head.next;
282
+ if ((_a = node.prev) == null ? void 0 : _a.next) node.prev.next = node.next;
283
+ if ((_b = node.next) == null ? void 0 : _b.prev) node.next.prev = node.prev;
284
+ node.next = targetNode.next;
285
+ node.prev = targetNode;
286
+ targetNode.next = node;
287
+ if ((_c = node.next) == null ? void 0 : _c.prev) node.next.prev = node;
288
+ if (targetNode === this.tail) this.tail = this.tail.next;
289
+ }
290
+ moveSegmentBefore(segment, targetSegment) {
291
+ var _a, _b, _c;
292
+ if (!segment || !(segment instanceof Segment)) throw new Error(`Invalid parameter: 'segment'`);
293
+ if (!targetSegment || !(targetSegment instanceof Segment))
294
+ throw new Error(`Invalid parameter: 'targetSegment'`);
295
+ if (segment === targetSegment) return;
296
+ const node = this.getNode(segment);
297
+ if (!node) throw new Error(`Failed to locate: 'segment' [${segment.type}]`);
298
+ const targetNode = this.getNode(targetSegment);
299
+ if (!targetNode) throw new Error(`Failed to locate: 'targetSegment' [${targetSegment.type}]`);
300
+ if (node === this.tail) this.tail = this.tail.prev;
301
+ if ((_a = node.prev) == null ? void 0 : _a.next) node.prev.next = node.next;
302
+ if ((_b = node.next) == null ? void 0 : _b.prev) node.next.prev = node.prev;
303
+ node.next = targetNode;
304
+ node.prev = targetNode.prev;
305
+ targetNode.prev = node;
306
+ if ((_c = node.prev) == null ? void 0 : _c.next) node.prev.next = node;
307
+ if (targetNode === this.head) this.head = this.head.prev;
308
+ }
309
+ reindexSegments(resetRules, startIndex = 1, field = "1.1") {
310
+ if (!resetRules || typeof resetRules !== "object")
311
+ throw new Error(`Invalid parameter: 'resetRules'`);
312
+ if (!startIndex || typeof startIndex !== "number")
313
+ throw new Error(`Invalid parameter: 'startIndex' [${startIndex}]`);
314
+ if (field && !/^(\d+)(\.\d+){0,1}$/.test(field))
315
+ throw new Error(`Invalid parameter: 'field' [${field}]`);
316
+ let curr = this.head;
317
+ const indexMap = {};
318
+ for (const key of Object.keys(resetRules)) {
319
+ if (!Array.isArray(resetRules[key]))
320
+ throw new Error(
321
+ `Invalid parameter: 'resetRules'. Expected key [${key}] value to be an array.`
322
+ );
323
+ indexMap[key] = startIndex;
341
324
  }
342
- moveSegmentBefore(segment, targetSegment) {
343
- var _a, _b, _c;
344
- if (!segment || !(segment instanceof Segment))
345
- throw new Error(`Invalid Segment: ${JSON.stringify(segment)}`);
346
- if (!targetSegment || !(targetSegment instanceof Segment))
347
- throw new Error(`Invalid Target Segment: ${JSON.stringify(targetSegment)}`);
348
- const node = this.getNode(segment);
349
- if (!node)
350
- throw new Error(`Failed to locate Segment: '${segment.type}'`);
351
- const targetNode = this.getNode(targetSegment);
352
- if (!targetNode)
353
- throw new Error(`Failed to locate Target Segment: '${targetSegment.type}'`);
354
- if ((_a = node.prev) === null || _a === void 0 ? void 0 : _a.next)
355
- node.prev.next = node.next;
356
- if ((_b = node.next) === null || _b === void 0 ? void 0 : _b.prev)
357
- node.next.prev = node.prev;
358
- node.next = targetNode;
359
- node.prev = targetNode.prev;
360
- targetNode.prev = node;
361
- if ((_c = node.prev) === null || _c === void 0 ? void 0 : _c.next)
362
- node.prev.next = node;
363
- if (targetNode === this.head)
364
- this.head = this.head.prev;
365
- }
366
- reindexSegments(resetRules, field = '1.1') {
367
- var _a;
368
- if (!resetRules || typeof resetRules !== 'object')
369
- throw new Error('Invalid Parameter: resetRules');
370
- if (field && !/^(\d)+(\.\d+)*$/.test(field))
371
- throw new Error(`Invalid Parameter: field '${field}'`);
372
- let curr = this.head;
373
- const indexMap = {};
374
- for (const key in resetRules) {
375
- indexMap[key] = 1;
376
- }
377
- while (curr) {
378
- const segment = curr.data;
379
- if (segment.type in resetRules) {
380
- segment.set(`${segment.type}.${field}`, String(indexMap[segment.type]++));
381
- }
382
- for (const key in resetRules) {
383
- if ((_a = resetRules[key]) === null || _a === void 0 ? void 0 : _a.includes(segment.type)) {
384
- indexMap[key] = 1;
385
- }
386
- }
387
- curr = curr.next;
325
+ while (curr) {
326
+ const segment = curr.data;
327
+ if (segment.type in resetRules)
328
+ segment.set(`${segment.type}.${field}`, String(indexMap[segment.type]++));
329
+ for (const [segmentType, resetTriggers] of Object.entries(resetRules)) {
330
+ for (const resetTrigger of resetTriggers) {
331
+ if (segment.type === resetTrigger) {
332
+ indexMap[segmentType] = startIndex;
333
+ break;
334
+ }
388
335
  }
336
+ }
337
+ curr = curr.next;
389
338
  }
390
- getNode(segment) {
391
- if (!segment || !(segment instanceof Segment)) {
392
- throw new Error(`Invalid Segment: ${JSON.stringify(segment)}`);
393
- }
394
- let curr = this.head;
395
- while (curr) {
396
- if (curr.data === segment) {
397
- return curr;
398
- }
399
- curr = curr.next;
400
- }
401
- return curr;
339
+ }
340
+ getNode(segment) {
341
+ if (!segment || !(segment instanceof Segment)) {
342
+ throw new Error(`Invalid parameter: 'segment'`);
402
343
  }
403
- appendSegmentNode(newSegment, targetSegment) {
404
- var _a;
405
- if (!newSegment || !(newSegment instanceof Segment))
406
- throw new Error(`Invalid Segment: ${JSON.stringify(newSegment)}`);
407
- if (targetSegment && !(targetSegment instanceof Segment))
408
- throw new Error(`Invalid Target Segment: ${JSON.stringify(targetSegment)}`);
409
- const newNode = new Node(newSegment);
410
- if (targetSegment) {
411
- const targetNode = this.getNode(targetSegment);
412
- if (!targetNode)
413
- throw new Error(`Failed to locate Target Segment: '${targetSegment.type}'`);
414
- newNode.next = targetNode.next;
415
- newNode.prev = targetNode;
416
- targetNode.next = newNode;
417
- if ((_a = newNode.next) === null || _a === void 0 ? void 0 : _a.prev)
418
- newNode.next.prev = newNode;
419
- if (targetNode === this.tail)
420
- this.tail = newNode;
421
- }
422
- else if (!this.head) {
423
- this.head = newNode;
424
- this.tail = newNode;
425
- }
426
- else if (this.tail) {
427
- newNode.prev = this.tail;
428
- this.tail.next = newNode;
429
- this.tail = newNode;
430
- }
431
- else {
432
- throw new Error(`Failed to append Segment: ${newSegment.type}`);
433
- }
434
- return newSegment;
344
+ let curr = this.head;
345
+ while (curr) {
346
+ if (curr.data === segment) return curr;
347
+ curr = curr.next;
435
348
  }
436
- prependSegmentNode(newSegment, targetSegment) {
437
- var _a;
438
- if (!newSegment || !(newSegment instanceof Segment))
439
- throw new Error(`Invalid Segment: ${JSON.stringify(newSegment)}`);
440
- if (!targetSegment || !(targetSegment instanceof Segment))
441
- throw new Error(`Invalid Target Segment: ${JSON.stringify(targetSegment)}`);
442
- const targetNode = this.getNode(targetSegment);
443
- if (!targetNode)
444
- throw new Error(`Failed to locate Target Segment: '${targetSegment.type}'`);
445
- const newNode = new Node(newSegment);
446
- newNode.next = targetNode;
447
- newNode.prev = targetNode.prev;
448
- targetNode.prev = newNode;
449
- if ((_a = newNode.prev) === null || _a === void 0 ? void 0 : _a.next)
450
- newNode.prev.next = newNode;
451
- if (targetNode === this.head)
452
- this.head = this.head.prev;
453
- return newSegment;
349
+ return curr;
350
+ }
351
+ appendSegmentNode(newSegment, targetSegment) {
352
+ var _a;
353
+ if (!newSegment || !(newSegment instanceof Segment))
354
+ throw new Error(`Invalid parameter: 'newSegment'`);
355
+ if (targetSegment && !(targetSegment instanceof Segment))
356
+ throw new Error(`Invalid parameter: 'targetSegment'`);
357
+ const newNode = new Node(newSegment);
358
+ if (targetSegment) {
359
+ const targetNode = this.getNode(targetSegment);
360
+ if (!targetNode) throw new Error(`Failed to locate: 'targetSegment' [${targetSegment.type}]`);
361
+ newNode.next = targetNode.next;
362
+ newNode.prev = targetNode;
363
+ targetNode.next = newNode;
364
+ if ((_a = newNode.next) == null ? void 0 : _a.prev) newNode.next.prev = newNode;
365
+ if (targetNode === this.tail) this.tail = this.tail.next;
366
+ } else if (!this.head) {
367
+ this.head = newNode;
368
+ this.tail = newNode;
369
+ } else if (this.tail) {
370
+ newNode.prev = this.tail;
371
+ this.tail.next = newNode;
372
+ this.tail = this.tail.next;
373
+ } else {
374
+ throw new Error(`Failed to append segment: ${newSegment.type}`);
454
375
  }
455
- }
456
- export default HL7;
376
+ return newSegment;
377
+ }
378
+ prependSegmentNode(newSegment, targetSegment) {
379
+ var _a;
380
+ if (!newSegment || !(newSegment instanceof Segment))
381
+ throw new Error(`Invalid parameter: 'newSegment'`);
382
+ if (!targetSegment || !(targetSegment instanceof Segment))
383
+ throw new Error(`Invalid parameter: 'targetSegment'`);
384
+ const targetNode = this.getNode(targetSegment);
385
+ if (!targetNode) throw new Error(`Failed to locate: 'targetSegment' [${targetSegment.type}]`);
386
+ const newNode = new Node(newSegment);
387
+ newNode.next = targetNode;
388
+ newNode.prev = targetNode.prev;
389
+ targetNode.prev = newNode;
390
+ if ((_a = newNode.prev) == null ? void 0 : _a.next) newNode.prev.next = newNode;
391
+ if (targetNode === this.head) this.head = this.head.prev;
392
+ return newSegment;
393
+ }
394
+ };
395
+ var index_default = HL7;
396
+ export {
397
+ index_default as default
398
+ };
457
399
  //# sourceMappingURL=index.js.map