hl7-tstd 0.2.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.cjs +420 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +51 -0
- package/dist/index.d.ts +5 -4
- package/dist/index.js +375 -423
- package/dist/index.js.map +1 -1
- package/package.json +22 -4
- package/dist/index.d.ts.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,447 +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
|
-
|
|
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));
|
|
41
50
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
return repeatingFieldsArr.join(repeatingDelim);
|
|
57
|
-
}
|
|
58
|
-
else {
|
|
59
|
-
if (!this.data)
|
|
60
|
-
return null;
|
|
61
|
-
const fieldsArr = [this.type];
|
|
62
|
-
for (const fieldKey in this.data) {
|
|
63
|
-
const repeatingCompsArr = [];
|
|
64
|
-
for (const repeatingField of this.data[fieldKey]) {
|
|
65
|
-
const compsArr = [];
|
|
66
|
-
for (const compKey in repeatingField) {
|
|
67
|
-
compsArr.push((_k = repeatingField[compKey]) === null || _k === void 0 ? void 0 : _k.join(subCompDelim));
|
|
68
|
-
}
|
|
69
|
-
repeatingCompsArr.push(compsArr.join(componentDelim));
|
|
70
|
-
}
|
|
71
|
-
fieldsArr.push(repeatingCompsArr.join(repeatingDelim));
|
|
72
|
-
}
|
|
73
|
-
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));
|
|
74
65
|
}
|
|
66
|
+
fieldsArr.push(repeatingCompsArr.join(repeatingDelim));
|
|
67
|
+
}
|
|
68
|
+
return fieldsArr.join(fieldDelim);
|
|
75
69
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}
|
|
97
|
-
for (let i = 0; i <= repeatingIndex; i++) {
|
|
98
|
-
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)
|
|
99
|
-
this.data[`${type}.${fieldIdx}`][i] = { [`${type}.${fieldIdx}.${1}`]: [''] };
|
|
100
|
-
}
|
|
101
|
-
if (compIdx) {
|
|
102
|
-
for (let i = 1; i <= parseInt(compIdx, 10); i++) {
|
|
103
|
-
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)
|
|
104
|
-
this.data[`${type}.${fieldIdx}`][repeatingIndex][`${type}.${fieldIdx}.${i}`] = [''];
|
|
105
|
-
}
|
|
106
|
-
for (let i = 0; i <= subComponentIndex; i++) {
|
|
107
|
-
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)
|
|
108
|
-
this.data[`${type}.${fieldIdx}`][repeatingIndex][`${type}.${fieldIdx}.${compIdx}`][i] =
|
|
109
|
-
'';
|
|
110
|
-
}
|
|
111
|
-
this.data[`${type}.${fieldIdx}`][repeatingIndex][`${type}.${fieldIdx}.${compIdx}`][subComponentIndex] = value;
|
|
112
|
-
}
|
|
113
|
-
else {
|
|
114
|
-
this.data[`${type}.${fieldIdx}`][repeatingIndex] = {
|
|
115
|
-
[`${type}.${fieldIdx}.${1}`]: [value],
|
|
116
|
-
};
|
|
117
|
-
}
|
|
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}`]: [""] }];
|
|
118
91
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
var _a, _b, _c, _d, _e, _f;
|
|
123
|
-
this.head = null;
|
|
124
|
-
this.tail = null;
|
|
125
|
-
this.raw = typeof hl7Msg === 'string' ? hl7Msg : '';
|
|
126
|
-
this.parseOptions = {
|
|
127
|
-
fieldDelim: (_a = parseOptions === null || parseOptions === void 0 ? void 0 : parseOptions.fieldDelim) !== null && _a !== void 0 ? _a : '|',
|
|
128
|
-
repeatingDelim: (_b = parseOptions === null || parseOptions === void 0 ? void 0 : parseOptions.repeatingDelim) !== null && _b !== void 0 ? _b : '~',
|
|
129
|
-
componentDelim: (_c = parseOptions === null || parseOptions === void 0 ? void 0 : parseOptions.componentDelim) !== null && _c !== void 0 ? _c : '^',
|
|
130
|
-
subCompDelim: (_d = parseOptions === null || parseOptions === void 0 ? void 0 : parseOptions.subCompDelim) !== null && _d !== void 0 ? _d : '&',
|
|
131
|
-
eolDelim: (_e = parseOptions === null || parseOptions === void 0 ? void 0 : parseOptions.eolDelim) !== null && _e !== void 0 ? _e : '\r?\n|\r',
|
|
132
|
-
buildEolChar: (_f = parseOptions === null || parseOptions === void 0 ? void 0 : parseOptions.buildEolChar) !== null && _f !== void 0 ? _f : '\r\n',
|
|
133
|
-
};
|
|
134
|
-
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}`]: [""] };
|
|
135
95
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
const fields = segmentStr.split(fieldDelim);
|
|
151
|
-
segment.data = {};
|
|
152
|
-
for (let i = 1; i < fields.length; i++) {
|
|
153
|
-
const fieldIdx = type === 'MSH' ? i + 1 : i;
|
|
154
|
-
const repeatingComps = fields[i].split(repeatingDelim);
|
|
155
|
-
segment.data[`${type}.${fieldIdx}`] = [];
|
|
156
|
-
for (let j = 0; j < repeatingComps.length; j++) {
|
|
157
|
-
const components = repeatingComps[j].split(componentDelim);
|
|
158
|
-
segment.data[`${type}.${fieldIdx}`][j] = {};
|
|
159
|
-
for (let k = 0; k < components.length; k++) {
|
|
160
|
-
const subComps = components[k].split(subCompDelim);
|
|
161
|
-
segment.data[`${type}.${fieldIdx}`][j][`${type}.${fieldIdx}.${k + 1}`] = [];
|
|
162
|
-
for (let l = 0; l < subComps.length; l++) {
|
|
163
|
-
segment.data[`${type}.${fieldIdx}`][j][`${type}.${fieldIdx}.${k + 1}`][l] =
|
|
164
|
-
subComps[l];
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
if (type === 'MSH') {
|
|
170
|
-
segment.data['MSH.2'] = [
|
|
171
|
-
{ 'MSH.2.1': [`${componentDelim}${repeatingDelim}\\${subCompDelim}`] },
|
|
172
|
-
];
|
|
173
|
-
}
|
|
174
|
-
this.appendSegmentNode(segment);
|
|
175
|
-
}
|
|
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
|
+
};
|
|
176
110
|
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
while (curr) {
|
|
204
|
-
if (curr.data.type === type)
|
|
205
|
-
return curr.data;
|
|
206
|
-
curr = curr.next;
|
|
207
|
-
}
|
|
208
|
-
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
|
+
);
|
|
209
137
|
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
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];
|
|
219
157
|
}
|
|
220
|
-
|
|
221
|
-
segments.push(curr.data);
|
|
222
|
-
}
|
|
223
|
-
curr = curr.next;
|
|
158
|
+
}
|
|
224
159
|
}
|
|
225
|
-
|
|
160
|
+
}
|
|
161
|
+
if (type === "MSH") {
|
|
162
|
+
segment.data["MSH.2"] = [
|
|
163
|
+
{ "MSH.2.1": [`${componentDelim}${repeatingDelim}\\${subCompDelim}`] }
|
|
164
|
+
];
|
|
165
|
+
}
|
|
166
|
+
this.appendSegmentNode(segment);
|
|
226
167
|
}
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
const
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
break;
|
|
244
|
-
if (curr.data.type === type)
|
|
245
|
-
res.push(curr.data);
|
|
246
|
-
else if (consecutive && res.length)
|
|
247
|
-
break;
|
|
248
|
-
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));
|
|
249
184
|
}
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
if (!/^[A-Z\d]{3}$/.test(type))
|
|
254
|
-
throw new Error(`Invalid parameter: 'type' [${type}]`);
|
|
255
|
-
return this.appendSegmentNode(new Segment(type, null, this.parseOptions));
|
|
185
|
+
fieldsArr.push(repeatingCompsArr.join(repeatingDelim));
|
|
186
|
+
}
|
|
187
|
+
segmentsStrArr.push(fieldsArr.join(fieldDelim));
|
|
256
188
|
}
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
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;
|
|
263
197
|
}
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
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;
|
|
270
211
|
}
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
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;
|
|
275
231
|
}
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
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'`);
|
|
297
259
|
}
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
if (!node)
|
|
308
|
-
throw new Error(`Failed to locate: 'segment' [${segment.type}]`);
|
|
309
|
-
const targetNode = this.getNode(targetSegment);
|
|
310
|
-
if (!targetNode)
|
|
311
|
-
throw new Error(`Failed to locate: 'targetSegment' [${targetSegment.type}]`);
|
|
312
|
-
if (node === this.head)
|
|
313
|
-
this.head = this.head.next;
|
|
314
|
-
if ((_a = node.prev) === null || _a === void 0 ? void 0 : _a.next)
|
|
315
|
-
node.prev.next = node.next;
|
|
316
|
-
if ((_b = node.next) === null || _b === void 0 ? void 0 : _b.prev)
|
|
317
|
-
node.next.prev = node.prev;
|
|
318
|
-
node.next = targetNode.next;
|
|
319
|
-
node.prev = targetNode;
|
|
320
|
-
targetNode.next = node;
|
|
321
|
-
if ((_c = node.next) === null || _c === void 0 ? void 0 : _c.prev)
|
|
322
|
-
node.next.prev = node;
|
|
323
|
-
if (targetNode === this.tail)
|
|
324
|
-
this.tail = this.tail.next;
|
|
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;
|
|
325
269
|
}
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
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;
|
|
353
324
|
}
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
indexMap[key] = startIndex;
|
|
365
|
-
}
|
|
366
|
-
while (curr) {
|
|
367
|
-
const segment = curr.data;
|
|
368
|
-
if (segment.type in resetRules)
|
|
369
|
-
segment.set(`${segment.type}.${field}`, String(indexMap[segment.type]++));
|
|
370
|
-
for (const [segmentType, resetTriggers] of Object.entries(resetRules)) {
|
|
371
|
-
for (const resetTrigger of resetTriggers) {
|
|
372
|
-
if (segment.type === resetTrigger) {
|
|
373
|
-
indexMap[segmentType] = startIndex;
|
|
374
|
-
break;
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
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
|
+
}
|
|
379
335
|
}
|
|
336
|
+
}
|
|
337
|
+
curr = curr.next;
|
|
380
338
|
}
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
let curr = this.head;
|
|
386
|
-
while (curr) {
|
|
387
|
-
if (curr.data === segment)
|
|
388
|
-
return curr;
|
|
389
|
-
curr = curr.next;
|
|
390
|
-
}
|
|
391
|
-
return curr;
|
|
339
|
+
}
|
|
340
|
+
getNode(segment) {
|
|
341
|
+
if (!segment || !(segment instanceof Segment)) {
|
|
342
|
+
throw new Error(`Invalid parameter: 'segment'`);
|
|
392
343
|
}
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
if (targetSegment && !(targetSegment instanceof Segment))
|
|
398
|
-
throw new Error(`Invalid parameter: 'targetSegment'`);
|
|
399
|
-
const newNode = new Node(newSegment);
|
|
400
|
-
if (targetSegment) {
|
|
401
|
-
const targetNode = this.getNode(targetSegment);
|
|
402
|
-
if (!targetNode)
|
|
403
|
-
throw new Error(`Failed to locate: 'targetSegment' [${targetSegment.type}]`);
|
|
404
|
-
newNode.next = targetNode.next;
|
|
405
|
-
newNode.prev = targetNode;
|
|
406
|
-
targetNode.next = newNode;
|
|
407
|
-
if ((_a = newNode.next) === null || _a === void 0 ? void 0 : _a.prev)
|
|
408
|
-
newNode.next.prev = newNode;
|
|
409
|
-
if (targetNode === this.tail)
|
|
410
|
-
this.tail = this.tail.next;
|
|
411
|
-
}
|
|
412
|
-
else if (!this.head) {
|
|
413
|
-
this.head = newNode;
|
|
414
|
-
this.tail = newNode;
|
|
415
|
-
}
|
|
416
|
-
else if (this.tail) {
|
|
417
|
-
newNode.prev = this.tail;
|
|
418
|
-
this.tail.next = newNode;
|
|
419
|
-
this.tail = this.tail.next;
|
|
420
|
-
}
|
|
421
|
-
else {
|
|
422
|
-
throw new Error(`Failed to append segment: ${newSegment.type}`);
|
|
423
|
-
}
|
|
424
|
-
return newSegment;
|
|
344
|
+
let curr = this.head;
|
|
345
|
+
while (curr) {
|
|
346
|
+
if (curr.data === segment) return curr;
|
|
347
|
+
curr = curr.next;
|
|
425
348
|
}
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
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}`);
|
|
444
375
|
}
|
|
445
|
-
|
|
446
|
-
|
|
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
|
+
};
|
|
447
399
|
//# sourceMappingURL=index.js.map
|