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/README.md +235 -17
- package/dist/index.cjs +420 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +51 -0
- package/dist/index.d.ts +14 -9
- package/dist/index.js +375 -433
- package/dist/index.js.map +1 -1
- package/package.json +24 -6
- package/dist/index.d.ts.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,457 +1,399 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
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
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
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
|
-
|
|
143
|
-
|
|
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
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
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
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
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
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
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
|
-
|
|
244
|
-
segments.push(curr.data);
|
|
245
|
-
}
|
|
246
|
-
curr = curr.next;
|
|
158
|
+
}
|
|
247
159
|
}
|
|
248
|
-
|
|
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
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
const
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
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
|
-
|
|
185
|
+
fieldsArr.push(repeatingCompsArr.join(repeatingDelim));
|
|
186
|
+
}
|
|
187
|
+
segmentsStrArr.push(fieldsArr.join(fieldDelim));
|
|
272
188
|
}
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
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
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
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
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
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
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
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
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
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
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
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
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
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
|
-
|
|
391
|
-
|
|
392
|
-
|
|
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
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
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
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
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
|
-
|
|
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
|