hl7v2 1.2.0 → 1.2.2
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/cjs/hl7-component.js +4 -3
- package/cjs/hl7-message.js +81 -55
- package/cjs/hl7-sub-component.js +5 -5
- package/esm/hl7-component.js +4 -3
- package/esm/hl7-message.js +81 -55
- package/esm/hl7-sub-component.js +5 -5
- package/package.json +2 -2
- package/types/hl7-message.d.ts +17 -1
package/cjs/hl7-component.js
CHANGED
|
@@ -77,8 +77,7 @@ class Hl7Component {
|
|
|
77
77
|
this.subcomp(subComponent || 1).value = value;
|
|
78
78
|
return this;
|
|
79
79
|
}
|
|
80
|
-
|
|
81
|
-
this._data.value = decode && value != null ? decode(value) : value;
|
|
80
|
+
this._data.value = value;
|
|
82
81
|
return this;
|
|
83
82
|
}
|
|
84
83
|
fromHL7String(value) {
|
|
@@ -100,7 +99,7 @@ class Hl7Component {
|
|
|
100
99
|
let segmentIndex = this.segment.index;
|
|
101
100
|
if (segmentIndex < 0)
|
|
102
101
|
segmentIndex = this.message.segments.length;
|
|
103
|
-
|
|
102
|
+
const err = new hl7_error_js_1.HL7Error(`The field (${location}) contained data of the wrong data type. ${e.message}.`, {
|
|
104
103
|
segmentType: this.segment.segmentType,
|
|
105
104
|
segmentSequence: segmentIndex,
|
|
106
105
|
fieldPosition: this.field.position,
|
|
@@ -108,6 +107,8 @@ class Hl7Component {
|
|
|
108
107
|
repetitionIndex: this.repetition.index,
|
|
109
108
|
hl7ErrorCode: 102,
|
|
110
109
|
});
|
|
110
|
+
err.stack = e.stack;
|
|
111
|
+
throw err;
|
|
111
112
|
}
|
|
112
113
|
return;
|
|
113
114
|
}
|
package/cjs/hl7-message.js
CHANGED
|
@@ -16,7 +16,6 @@ class HL7Message {
|
|
|
16
16
|
this.subComponentSeparator = constants_js_1.SUBCOMPONENT_SEPARATOR;
|
|
17
17
|
this.repetitionSeparator = constants_js_1.REPETITION_SEPARATOR;
|
|
18
18
|
this.escapeCharacter = constants_js_1.ESCAPE_CHARACTER;
|
|
19
|
-
this.carriageReturn = constants_js_1.CR;
|
|
20
19
|
this._dictionaries = dictionaries || hl7v2_dictionary_1.dictionaries;
|
|
21
20
|
this._version = version;
|
|
22
21
|
this.clear(version);
|
|
@@ -40,12 +39,7 @@ class HL7Message {
|
|
|
40
39
|
return this.header.field(hl7v2_dictionary_1.MSHSegment.MessageControlID).toHL7String();
|
|
41
40
|
}
|
|
42
41
|
clear(version) {
|
|
43
|
-
|
|
44
|
-
version = this._version || hl7v2_dictionary_1.HL7Version.v2_7_1;
|
|
45
|
-
this._version = version;
|
|
46
|
-
const nearestVersion = (0, hl7v2_dictionary_1.findNearestHL7Version)(version);
|
|
47
|
-
this._dictionary = this._dictionaries[nearestVersion];
|
|
48
|
-
this._segments = [];
|
|
42
|
+
this._clear(version);
|
|
49
43
|
const msh = this.addSegment('MSH');
|
|
50
44
|
msh.field(hl7v2_dictionary_1.MSHSegment.DateTimeOfMessage).setValue(new Date());
|
|
51
45
|
msh.field(hl7v2_dictionary_1.MSHSegment.MessageControlID).setValue((0, uid_1.uid)(8));
|
|
@@ -92,51 +86,18 @@ class HL7Message {
|
|
|
92
86
|
return (this.segments
|
|
93
87
|
.map(segment => segment.toHL7String(options))
|
|
94
88
|
.filter(v => v)
|
|
95
|
-
.join(
|
|
89
|
+
.join(constants_js_1.CR) + constants_js_1.CR);
|
|
96
90
|
}
|
|
97
91
|
parse(input) {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
if (crIdx < 0)
|
|
108
|
-
crIdx = input.length;
|
|
109
|
-
headerStr = input.substring(0, crIdx);
|
|
110
|
-
} /* c8 ignore else */
|
|
111
|
-
else {
|
|
112
|
-
throw new TypeError('You must provide string or Buffer argument');
|
|
113
|
-
}
|
|
114
|
-
if (headerStr.startsWith(constants_js_1.VT))
|
|
115
|
-
headerStr = headerStr.substring(1);
|
|
116
|
-
if (!headerStr.startsWith('MSH'))
|
|
117
|
-
throw new hl7_error_js_1.HL7Error('Message must start with (MSH) segment', {
|
|
118
|
-
line: 1,
|
|
119
|
-
hl7ErrorCode: 100,
|
|
120
|
-
});
|
|
121
|
-
const fieldSeparator = headerStr[3];
|
|
122
|
-
/* Detect version and charset */
|
|
123
|
-
const headerItems = headerStr.split(fieldSeparator);
|
|
124
|
-
const version = headerItems[11];
|
|
125
|
-
const encoding = headerItems[17]?.split('^')[0] || 'UTF-8';
|
|
126
|
-
let str = Buffer.isBuffer(input) ? iconv_lite_1.default.decode(input, encoding) : input;
|
|
127
|
-
if (str.startsWith(constants_js_1.VT))
|
|
128
|
-
str = str.substring(1);
|
|
129
|
-
const k = str.indexOf(constants_js_1.FS);
|
|
130
|
-
if (k >= 0)
|
|
131
|
-
str = str.substring(0, k);
|
|
132
|
-
this.clear(version);
|
|
133
|
-
this.fieldSeparator = fieldSeparator;
|
|
134
|
-
this.componentSeparator = headerItems[1][0];
|
|
135
|
-
this.repetitionSeparator = headerItems[1][1];
|
|
136
|
-
this.escapeCharacter = headerItems[1][2];
|
|
137
|
-
this.subComponentSeparator = headerItems[1][3];
|
|
138
|
-
this._segments = [];
|
|
139
|
-
const lines = str.split(constants_js_1.CR);
|
|
92
|
+
const raw = HL7Message.parseRaw(input);
|
|
93
|
+
this._clear(raw.version);
|
|
94
|
+
this.fieldSeparator = raw.fieldSeparator;
|
|
95
|
+
this.componentSeparator = raw.componentSeparator;
|
|
96
|
+
this.repetitionSeparator = raw.repetitionSeparator;
|
|
97
|
+
this.escapeCharacter = raw.escapeCharacter;
|
|
98
|
+
this.subComponentSeparator = raw.subComponentSeparator;
|
|
99
|
+
const lines = raw.data.split(constants_js_1.CR);
|
|
100
|
+
const headerItems = raw.header.split(this.fieldSeparator);
|
|
140
101
|
for (const [i, line] of lines.entries()) {
|
|
141
102
|
if (!line)
|
|
142
103
|
continue;
|
|
@@ -158,9 +119,9 @@ class HL7Message {
|
|
|
158
119
|
this.header
|
|
159
120
|
.field(hl7v2_dictionary_1.MSHSegment.ReceivingFacility)
|
|
160
121
|
.setValue(headerItems[5]);
|
|
161
|
-
this.header.field(hl7v2_dictionary_1.MSHSegment.EncodingCharacters).setValue(encoding);
|
|
122
|
+
this.header.field(hl7v2_dictionary_1.MSHSegment.EncodingCharacters).setValue(raw.encoding);
|
|
162
123
|
this.header.field(hl7v2_dictionary_1.MSHSegment.MessageControlID).setValue(headerItems[9]);
|
|
163
|
-
this.header.field(hl7v2_dictionary_1.MSHSegment.VersionID).setValue(version);
|
|
124
|
+
this.header.field(hl7v2_dictionary_1.MSHSegment.VersionID).setValue(raw.version);
|
|
164
125
|
/* c8 ignore next */
|
|
165
126
|
const e1 = e instanceof hl7_error_js_1.HL7Error ? e : new hl7_error_js_1.HL7Error(e.message);
|
|
166
127
|
if (e1.segmentType) {
|
|
@@ -174,7 +135,7 @@ class HL7Message {
|
|
|
174
135
|
}
|
|
175
136
|
}
|
|
176
137
|
createAck(ackCode = 'AA', textMessage) {
|
|
177
|
-
const out = new HL7Message(this.version);
|
|
138
|
+
const out = new HL7Message(this.version, this._dictionaries);
|
|
178
139
|
const msh = out.header;
|
|
179
140
|
// Sending Application
|
|
180
141
|
msh
|
|
@@ -201,7 +162,7 @@ class HL7Message {
|
|
|
201
162
|
return out;
|
|
202
163
|
}
|
|
203
164
|
createNak(errors) {
|
|
204
|
-
const out = new HL7Message(this.version);
|
|
165
|
+
const out = new HL7Message(this.version, this._dictionaries);
|
|
205
166
|
const msh = out.header;
|
|
206
167
|
// Sending Application
|
|
207
168
|
msh
|
|
@@ -224,7 +185,7 @@ class HL7Message {
|
|
|
224
185
|
.field(hl7v2_dictionary_1.MSASegment.MessageControlID)
|
|
225
186
|
.setValue(this.header.field(hl7v2_dictionary_1.MSHSegment.MessageControlID).getValue());
|
|
226
187
|
for (const error of errors) {
|
|
227
|
-
|
|
188
|
+
out.addError(error);
|
|
228
189
|
}
|
|
229
190
|
return out;
|
|
230
191
|
}
|
|
@@ -274,6 +235,19 @@ class HL7Message {
|
|
|
274
235
|
}
|
|
275
236
|
}
|
|
276
237
|
}
|
|
238
|
+
_clear(version) {
|
|
239
|
+
if (!version)
|
|
240
|
+
version = this._version || hl7v2_dictionary_1.HL7Version.v2_7_1;
|
|
241
|
+
this._version = version;
|
|
242
|
+
const nearestVersion = (0, hl7v2_dictionary_1.findNearestHL7Version)(version);
|
|
243
|
+
this._dictionary = this._dictionaries[nearestVersion];
|
|
244
|
+
this._segments = [];
|
|
245
|
+
this.fieldSeparator = constants_js_1.FIELD_SEPARATOR;
|
|
246
|
+
this.componentSeparator = constants_js_1.COMPONENT_SEPARATOR;
|
|
247
|
+
this.subComponentSeparator = constants_js_1.SUBCOMPONENT_SEPARATOR;
|
|
248
|
+
this.repetitionSeparator = constants_js_1.REPETITION_SEPARATOR;
|
|
249
|
+
this.escapeCharacter = constants_js_1.ESCAPE_CHARACTER;
|
|
250
|
+
}
|
|
277
251
|
/**
|
|
278
252
|
*
|
|
279
253
|
* @static
|
|
@@ -283,5 +257,57 @@ class HL7Message {
|
|
|
283
257
|
message.parse(input);
|
|
284
258
|
return message;
|
|
285
259
|
}
|
|
260
|
+
/**
|
|
261
|
+
*
|
|
262
|
+
* @static
|
|
263
|
+
*/
|
|
264
|
+
static parseRaw(input) {
|
|
265
|
+
let headerStr = '';
|
|
266
|
+
if (Buffer.isBuffer(input)) {
|
|
267
|
+
let crIdx = input.indexOf(constants_js_1.CR);
|
|
268
|
+
if (crIdx < 0)
|
|
269
|
+
crIdx = input.length;
|
|
270
|
+
headerStr = input.toString('utf8', 0, crIdx);
|
|
271
|
+
}
|
|
272
|
+
else if (typeof input === 'string') {
|
|
273
|
+
let crIdx = input.indexOf(constants_js_1.CR);
|
|
274
|
+
if (crIdx < 0)
|
|
275
|
+
crIdx = input.length;
|
|
276
|
+
headerStr = input.substring(0, crIdx);
|
|
277
|
+
} /* c8 ignore else */
|
|
278
|
+
else {
|
|
279
|
+
throw new TypeError('You must provide string or Buffer argument');
|
|
280
|
+
}
|
|
281
|
+
if (headerStr.startsWith(constants_js_1.VT))
|
|
282
|
+
headerStr = headerStr.substring(1);
|
|
283
|
+
if (!headerStr.startsWith('MSH'))
|
|
284
|
+
throw new hl7_error_js_1.HL7Error('Message must start with (MSH) segment', {
|
|
285
|
+
line: 1,
|
|
286
|
+
hl7ErrorCode: 100,
|
|
287
|
+
});
|
|
288
|
+
const fieldSeparator = headerStr[3];
|
|
289
|
+
/* Detect version and charset */
|
|
290
|
+
const headerItems = headerStr.split(fieldSeparator);
|
|
291
|
+
const version = headerItems[11];
|
|
292
|
+
const encoding = headerItems[17]?.split('^')[0] || 'UTF-8';
|
|
293
|
+
let str = Buffer.isBuffer(input) ? iconv_lite_1.default.decode(input, encoding) : input;
|
|
294
|
+
if (str.startsWith(constants_js_1.VT))
|
|
295
|
+
str = str.substring(1);
|
|
296
|
+
const k = str.indexOf(constants_js_1.FS);
|
|
297
|
+
if (k >= 0)
|
|
298
|
+
str = str.substring(0, k);
|
|
299
|
+
return {
|
|
300
|
+
fieldSeparator,
|
|
301
|
+
componentSeparator: headerItems[1][0],
|
|
302
|
+
repetitionSeparator: headerItems[1][1],
|
|
303
|
+
escapeCharacter: headerItems[1][2],
|
|
304
|
+
subComponentSeparator: headerItems[1][3],
|
|
305
|
+
messageType: headerItems[8],
|
|
306
|
+
header: headerStr,
|
|
307
|
+
data: str,
|
|
308
|
+
encoding,
|
|
309
|
+
version: version,
|
|
310
|
+
};
|
|
311
|
+
}
|
|
286
312
|
}
|
|
287
313
|
exports.HL7Message = HL7Message;
|
package/cjs/hl7-sub-component.js
CHANGED
|
@@ -37,17 +37,17 @@ class Hl7SubComponent {
|
|
|
37
37
|
this._value = decode && value != null ? decode(value) : value;
|
|
38
38
|
}
|
|
39
39
|
fromHL7String(value) {
|
|
40
|
-
|
|
41
|
-
if (!value)
|
|
42
|
-
this.value = undefined;
|
|
40
|
+
this._value = undefined;
|
|
41
|
+
if (!value)
|
|
43
42
|
return;
|
|
44
|
-
}
|
|
45
43
|
const unescaped = (0, hl7_escape_js_1.hl7UnEscape)(value, this.field.message);
|
|
46
44
|
try {
|
|
47
45
|
if (Buffer.isBuffer(unescaped) || unescaped == null)
|
|
48
46
|
this.value = unescaped;
|
|
49
|
-
else
|
|
47
|
+
else {
|
|
48
|
+
const decode = this.definition.decode || this.typeDef.decode;
|
|
50
49
|
this.value = decode ? decode(unescaped) : unescaped;
|
|
50
|
+
}
|
|
51
51
|
}
|
|
52
52
|
catch (e) {
|
|
53
53
|
const location = `${this.segment.segmentType}.${this.field.position}.${this.component.position}.${this.position}[${this.component.repetition.index}]`;
|
package/esm/hl7-component.js
CHANGED
|
@@ -74,8 +74,7 @@ export class Hl7Component {
|
|
|
74
74
|
this.subcomp(subComponent || 1).value = value;
|
|
75
75
|
return this;
|
|
76
76
|
}
|
|
77
|
-
|
|
78
|
-
this._data.value = decode && value != null ? decode(value) : value;
|
|
77
|
+
this._data.value = value;
|
|
79
78
|
return this;
|
|
80
79
|
}
|
|
81
80
|
fromHL7String(value) {
|
|
@@ -97,7 +96,7 @@ export class Hl7Component {
|
|
|
97
96
|
let segmentIndex = this.segment.index;
|
|
98
97
|
if (segmentIndex < 0)
|
|
99
98
|
segmentIndex = this.message.segments.length;
|
|
100
|
-
|
|
99
|
+
const err = new HL7Error(`The field (${location}) contained data of the wrong data type. ${e.message}.`, {
|
|
101
100
|
segmentType: this.segment.segmentType,
|
|
102
101
|
segmentSequence: segmentIndex,
|
|
103
102
|
fieldPosition: this.field.position,
|
|
@@ -105,6 +104,8 @@ export class Hl7Component {
|
|
|
105
104
|
repetitionIndex: this.repetition.index,
|
|
106
105
|
hl7ErrorCode: 102,
|
|
107
106
|
});
|
|
107
|
+
err.stack = e.stack;
|
|
108
|
+
throw err;
|
|
108
109
|
}
|
|
109
110
|
return;
|
|
110
111
|
}
|
package/esm/hl7-message.js
CHANGED
|
@@ -12,7 +12,6 @@ export class HL7Message {
|
|
|
12
12
|
this.subComponentSeparator = SUBCOMPONENT_SEPARATOR;
|
|
13
13
|
this.repetitionSeparator = REPETITION_SEPARATOR;
|
|
14
14
|
this.escapeCharacter = ESCAPE_CHARACTER;
|
|
15
|
-
this.carriageReturn = CR;
|
|
16
15
|
this._dictionaries = dictionaries || defaultDictionaries;
|
|
17
16
|
this._version = version;
|
|
18
17
|
this.clear(version);
|
|
@@ -36,12 +35,7 @@ export class HL7Message {
|
|
|
36
35
|
return this.header.field(MSHSegment.MessageControlID).toHL7String();
|
|
37
36
|
}
|
|
38
37
|
clear(version) {
|
|
39
|
-
|
|
40
|
-
version = this._version || HL7Version.v2_7_1;
|
|
41
|
-
this._version = version;
|
|
42
|
-
const nearestVersion = findNearestHL7Version(version);
|
|
43
|
-
this._dictionary = this._dictionaries[nearestVersion];
|
|
44
|
-
this._segments = [];
|
|
38
|
+
this._clear(version);
|
|
45
39
|
const msh = this.addSegment('MSH');
|
|
46
40
|
msh.field(MSHSegment.DateTimeOfMessage).setValue(new Date());
|
|
47
41
|
msh.field(MSHSegment.MessageControlID).setValue(uid(8));
|
|
@@ -88,51 +82,18 @@ export class HL7Message {
|
|
|
88
82
|
return (this.segments
|
|
89
83
|
.map(segment => segment.toHL7String(options))
|
|
90
84
|
.filter(v => v)
|
|
91
|
-
.join(
|
|
85
|
+
.join(CR) + CR);
|
|
92
86
|
}
|
|
93
87
|
parse(input) {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
if (crIdx < 0)
|
|
104
|
-
crIdx = input.length;
|
|
105
|
-
headerStr = input.substring(0, crIdx);
|
|
106
|
-
} /* c8 ignore else */
|
|
107
|
-
else {
|
|
108
|
-
throw new TypeError('You must provide string or Buffer argument');
|
|
109
|
-
}
|
|
110
|
-
if (headerStr.startsWith(VT))
|
|
111
|
-
headerStr = headerStr.substring(1);
|
|
112
|
-
if (!headerStr.startsWith('MSH'))
|
|
113
|
-
throw new HL7Error('Message must start with (MSH) segment', {
|
|
114
|
-
line: 1,
|
|
115
|
-
hl7ErrorCode: 100,
|
|
116
|
-
});
|
|
117
|
-
const fieldSeparator = headerStr[3];
|
|
118
|
-
/* Detect version and charset */
|
|
119
|
-
const headerItems = headerStr.split(fieldSeparator);
|
|
120
|
-
const version = headerItems[11];
|
|
121
|
-
const encoding = headerItems[17]?.split('^')[0] || 'UTF-8';
|
|
122
|
-
let str = Buffer.isBuffer(input) ? iconv.decode(input, encoding) : input;
|
|
123
|
-
if (str.startsWith(VT))
|
|
124
|
-
str = str.substring(1);
|
|
125
|
-
const k = str.indexOf(FS);
|
|
126
|
-
if (k >= 0)
|
|
127
|
-
str = str.substring(0, k);
|
|
128
|
-
this.clear(version);
|
|
129
|
-
this.fieldSeparator = fieldSeparator;
|
|
130
|
-
this.componentSeparator = headerItems[1][0];
|
|
131
|
-
this.repetitionSeparator = headerItems[1][1];
|
|
132
|
-
this.escapeCharacter = headerItems[1][2];
|
|
133
|
-
this.subComponentSeparator = headerItems[1][3];
|
|
134
|
-
this._segments = [];
|
|
135
|
-
const lines = str.split(CR);
|
|
88
|
+
const raw = HL7Message.parseRaw(input);
|
|
89
|
+
this._clear(raw.version);
|
|
90
|
+
this.fieldSeparator = raw.fieldSeparator;
|
|
91
|
+
this.componentSeparator = raw.componentSeparator;
|
|
92
|
+
this.repetitionSeparator = raw.repetitionSeparator;
|
|
93
|
+
this.escapeCharacter = raw.escapeCharacter;
|
|
94
|
+
this.subComponentSeparator = raw.subComponentSeparator;
|
|
95
|
+
const lines = raw.data.split(CR);
|
|
96
|
+
const headerItems = raw.header.split(this.fieldSeparator);
|
|
136
97
|
for (const [i, line] of lines.entries()) {
|
|
137
98
|
if (!line)
|
|
138
99
|
continue;
|
|
@@ -154,9 +115,9 @@ export class HL7Message {
|
|
|
154
115
|
this.header
|
|
155
116
|
.field(MSHSegment.ReceivingFacility)
|
|
156
117
|
.setValue(headerItems[5]);
|
|
157
|
-
this.header.field(MSHSegment.EncodingCharacters).setValue(encoding);
|
|
118
|
+
this.header.field(MSHSegment.EncodingCharacters).setValue(raw.encoding);
|
|
158
119
|
this.header.field(MSHSegment.MessageControlID).setValue(headerItems[9]);
|
|
159
|
-
this.header.field(MSHSegment.VersionID).setValue(version);
|
|
120
|
+
this.header.field(MSHSegment.VersionID).setValue(raw.version);
|
|
160
121
|
/* c8 ignore next */
|
|
161
122
|
const e1 = e instanceof HL7Error ? e : new HL7Error(e.message);
|
|
162
123
|
if (e1.segmentType) {
|
|
@@ -170,7 +131,7 @@ export class HL7Message {
|
|
|
170
131
|
}
|
|
171
132
|
}
|
|
172
133
|
createAck(ackCode = 'AA', textMessage) {
|
|
173
|
-
const out = new HL7Message(this.version);
|
|
134
|
+
const out = new HL7Message(this.version, this._dictionaries);
|
|
174
135
|
const msh = out.header;
|
|
175
136
|
// Sending Application
|
|
176
137
|
msh
|
|
@@ -197,7 +158,7 @@ export class HL7Message {
|
|
|
197
158
|
return out;
|
|
198
159
|
}
|
|
199
160
|
createNak(errors) {
|
|
200
|
-
const out = new HL7Message(this.version);
|
|
161
|
+
const out = new HL7Message(this.version, this._dictionaries);
|
|
201
162
|
const msh = out.header;
|
|
202
163
|
// Sending Application
|
|
203
164
|
msh
|
|
@@ -220,7 +181,7 @@ export class HL7Message {
|
|
|
220
181
|
.field(MSASegment.MessageControlID)
|
|
221
182
|
.setValue(this.header.field(MSHSegment.MessageControlID).getValue());
|
|
222
183
|
for (const error of errors) {
|
|
223
|
-
|
|
184
|
+
out.addError(error);
|
|
224
185
|
}
|
|
225
186
|
return out;
|
|
226
187
|
}
|
|
@@ -270,6 +231,19 @@ export class HL7Message {
|
|
|
270
231
|
}
|
|
271
232
|
}
|
|
272
233
|
}
|
|
234
|
+
_clear(version) {
|
|
235
|
+
if (!version)
|
|
236
|
+
version = this._version || HL7Version.v2_7_1;
|
|
237
|
+
this._version = version;
|
|
238
|
+
const nearestVersion = findNearestHL7Version(version);
|
|
239
|
+
this._dictionary = this._dictionaries[nearestVersion];
|
|
240
|
+
this._segments = [];
|
|
241
|
+
this.fieldSeparator = FIELD_SEPARATOR;
|
|
242
|
+
this.componentSeparator = COMPONENT_SEPARATOR;
|
|
243
|
+
this.subComponentSeparator = SUBCOMPONENT_SEPARATOR;
|
|
244
|
+
this.repetitionSeparator = REPETITION_SEPARATOR;
|
|
245
|
+
this.escapeCharacter = ESCAPE_CHARACTER;
|
|
246
|
+
}
|
|
273
247
|
/**
|
|
274
248
|
*
|
|
275
249
|
* @static
|
|
@@ -279,4 +253,56 @@ export class HL7Message {
|
|
|
279
253
|
message.parse(input);
|
|
280
254
|
return message;
|
|
281
255
|
}
|
|
256
|
+
/**
|
|
257
|
+
*
|
|
258
|
+
* @static
|
|
259
|
+
*/
|
|
260
|
+
static parseRaw(input) {
|
|
261
|
+
let headerStr = '';
|
|
262
|
+
if (Buffer.isBuffer(input)) {
|
|
263
|
+
let crIdx = input.indexOf(CR);
|
|
264
|
+
if (crIdx < 0)
|
|
265
|
+
crIdx = input.length;
|
|
266
|
+
headerStr = input.toString('utf8', 0, crIdx);
|
|
267
|
+
}
|
|
268
|
+
else if (typeof input === 'string') {
|
|
269
|
+
let crIdx = input.indexOf(CR);
|
|
270
|
+
if (crIdx < 0)
|
|
271
|
+
crIdx = input.length;
|
|
272
|
+
headerStr = input.substring(0, crIdx);
|
|
273
|
+
} /* c8 ignore else */
|
|
274
|
+
else {
|
|
275
|
+
throw new TypeError('You must provide string or Buffer argument');
|
|
276
|
+
}
|
|
277
|
+
if (headerStr.startsWith(VT))
|
|
278
|
+
headerStr = headerStr.substring(1);
|
|
279
|
+
if (!headerStr.startsWith('MSH'))
|
|
280
|
+
throw new HL7Error('Message must start with (MSH) segment', {
|
|
281
|
+
line: 1,
|
|
282
|
+
hl7ErrorCode: 100,
|
|
283
|
+
});
|
|
284
|
+
const fieldSeparator = headerStr[3];
|
|
285
|
+
/* Detect version and charset */
|
|
286
|
+
const headerItems = headerStr.split(fieldSeparator);
|
|
287
|
+
const version = headerItems[11];
|
|
288
|
+
const encoding = headerItems[17]?.split('^')[0] || 'UTF-8';
|
|
289
|
+
let str = Buffer.isBuffer(input) ? iconv.decode(input, encoding) : input;
|
|
290
|
+
if (str.startsWith(VT))
|
|
291
|
+
str = str.substring(1);
|
|
292
|
+
const k = str.indexOf(FS);
|
|
293
|
+
if (k >= 0)
|
|
294
|
+
str = str.substring(0, k);
|
|
295
|
+
return {
|
|
296
|
+
fieldSeparator,
|
|
297
|
+
componentSeparator: headerItems[1][0],
|
|
298
|
+
repetitionSeparator: headerItems[1][1],
|
|
299
|
+
escapeCharacter: headerItems[1][2],
|
|
300
|
+
subComponentSeparator: headerItems[1][3],
|
|
301
|
+
messageType: headerItems[8],
|
|
302
|
+
header: headerStr,
|
|
303
|
+
data: str,
|
|
304
|
+
encoding,
|
|
305
|
+
version: version,
|
|
306
|
+
};
|
|
307
|
+
}
|
|
282
308
|
}
|
package/esm/hl7-sub-component.js
CHANGED
|
@@ -34,17 +34,17 @@ export class Hl7SubComponent {
|
|
|
34
34
|
this._value = decode && value != null ? decode(value) : value;
|
|
35
35
|
}
|
|
36
36
|
fromHL7String(value) {
|
|
37
|
-
|
|
38
|
-
if (!value)
|
|
39
|
-
this.value = undefined;
|
|
37
|
+
this._value = undefined;
|
|
38
|
+
if (!value)
|
|
40
39
|
return;
|
|
41
|
-
}
|
|
42
40
|
const unescaped = hl7UnEscape(value, this.field.message);
|
|
43
41
|
try {
|
|
44
42
|
if (Buffer.isBuffer(unescaped) || unescaped == null)
|
|
45
43
|
this.value = unescaped;
|
|
46
|
-
else
|
|
44
|
+
else {
|
|
45
|
+
const decode = this.definition.decode || this.typeDef.decode;
|
|
47
46
|
this.value = decode ? decode(unescaped) : unescaped;
|
|
47
|
+
}
|
|
48
48
|
}
|
|
49
49
|
catch (e) {
|
|
50
50
|
const location = `${this.segment.segmentType}.${this.field.position}.${this.component.position}.${this.position}[${this.component.repetition.index}]`;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hl7v2",
|
|
3
3
|
"description": "HL7 v2 parser, serializer, validator for NodeJS",
|
|
4
|
-
"version": "1.2.
|
|
4
|
+
"version": "1.2.2",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"dependencies": {
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"uid": "^2.0.2"
|
|
16
16
|
},
|
|
17
17
|
"peerDependencies": {
|
|
18
|
-
"hl7v2-dictionary": "^1.2.
|
|
18
|
+
"hl7v2-dictionary": "^1.2.2"
|
|
19
19
|
},
|
|
20
20
|
"type": "module",
|
|
21
21
|
"exports": {
|
package/types/hl7-message.d.ts
CHANGED
|
@@ -10,7 +10,6 @@ export declare class HL7Message {
|
|
|
10
10
|
subComponentSeparator: string;
|
|
11
11
|
repetitionSeparator: string;
|
|
12
12
|
escapeCharacter: string;
|
|
13
|
-
carriageReturn: string;
|
|
14
13
|
constructor(version?: HL7Version, dictionaries?: Record<string, HL7Dictionary>);
|
|
15
14
|
get dictionary(): HL7Dictionary;
|
|
16
15
|
get segments(): HL7Segment[];
|
|
@@ -33,11 +32,28 @@ export declare class HL7Message {
|
|
|
33
32
|
createAck(ackCode?: AcknowledgmentCode, textMessage?: string): HL7Message;
|
|
34
33
|
createNak(errors: (Error | string)[]): HL7Message;
|
|
35
34
|
addError(error: Error | string): void;
|
|
35
|
+
protected _clear(version?: HL7Version): void;
|
|
36
36
|
/**
|
|
37
37
|
*
|
|
38
38
|
* @static
|
|
39
39
|
*/
|
|
40
40
|
static parse(input: string | Buffer, dictionaries?: Record<string, HL7Dictionary>): HL7Message;
|
|
41
|
+
/**
|
|
42
|
+
*
|
|
43
|
+
* @static
|
|
44
|
+
*/
|
|
45
|
+
static parseRaw(input: string | Buffer): {
|
|
46
|
+
header: string;
|
|
47
|
+
fieldSeparator: string;
|
|
48
|
+
componentSeparator: string;
|
|
49
|
+
repetitionSeparator: string;
|
|
50
|
+
escapeCharacter: string;
|
|
51
|
+
subComponentSeparator: string;
|
|
52
|
+
encoding: string;
|
|
53
|
+
version: HL7Version;
|
|
54
|
+
messageType: string;
|
|
55
|
+
data: string;
|
|
56
|
+
};
|
|
41
57
|
}
|
|
42
58
|
export interface HL7MessageSerializeOptions extends Hl7SegmentSerializeOptions {
|
|
43
59
|
}
|