@rethinkhealth/hl7v2-parser 0.2.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/LICENSE +21 -0
- package/dist/constants.d.ts +11 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +276 -0
- package/dist/index.js.map +1 -0
- package/dist/parser.d.ts +8 -0
- package/dist/parser.d.ts.map +1 -0
- package/dist/processor.d.ts +6 -0
- package/dist/processor.d.ts.map +1 -0
- package/dist/types.d.ts +16 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/utils.d.ts +14 -0
- package/dist/utils.d.ts.map +1 -0
- package/package.json +54 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Rethink Health
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { HL7v2Node } from '@rethinkhealth/hl7v2-ast';
|
|
2
|
+
import type { HL7v2Delimiters } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Default HL7v2 delimiters
|
|
5
|
+
*/
|
|
6
|
+
export declare const DEFAULT_DELIMITERS: HL7v2Delimiters;
|
|
7
|
+
/**
|
|
8
|
+
* Empty message node
|
|
9
|
+
*/
|
|
10
|
+
export declare const EMPTY_MESSAGE: HL7v2Node;
|
|
11
|
+
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAC1D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE/C;;GAEG;AACH,eAAO,MAAM,kBAAkB,EAAE,eAOhC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,aAAa,EAAE,SAQ3B,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,aAAa,CAAC;AACrD,cAAc,SAAS,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
// src/constants.ts
|
|
2
|
+
var DEFAULT_DELIMITERS = {
|
|
3
|
+
field: "|",
|
|
4
|
+
component: "^",
|
|
5
|
+
subcomponent: "&",
|
|
6
|
+
repetition: "~",
|
|
7
|
+
escape: "\\",
|
|
8
|
+
segment: "\r"
|
|
9
|
+
// default to \r
|
|
10
|
+
};
|
|
11
|
+
var EMPTY_MESSAGE = {
|
|
12
|
+
type: "message",
|
|
13
|
+
delimiter: DEFAULT_DELIMITERS.segment,
|
|
14
|
+
children: [],
|
|
15
|
+
position: {
|
|
16
|
+
start: { line: 1, column: 1, offset: 0 },
|
|
17
|
+
end: { line: 1, column: 1, offset: 0 }
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// src/utils.ts
|
|
22
|
+
function detectDelimitersFromMSH(raw, segmentDelimiter) {
|
|
23
|
+
const firstLineEnd = raw.indexOf(segmentDelimiter);
|
|
24
|
+
const mshSegment = firstLineEnd >= 0 ? raw.slice(0, firstLineEnd) : raw;
|
|
25
|
+
if (!mshSegment.startsWith("MSH")) {
|
|
26
|
+
return {};
|
|
27
|
+
}
|
|
28
|
+
const fieldDelimiter = mshSegment[3];
|
|
29
|
+
if (!fieldDelimiter) {
|
|
30
|
+
return {};
|
|
31
|
+
}
|
|
32
|
+
const encodingChars = mshSegment.slice(4, 8);
|
|
33
|
+
if (encodingChars.length !== 4) {
|
|
34
|
+
return {};
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
field: fieldDelimiter,
|
|
38
|
+
component: encodingChars[0],
|
|
39
|
+
repetition: encodingChars[1],
|
|
40
|
+
escape: encodingChars[2],
|
|
41
|
+
subcomponent: encodingChars[3],
|
|
42
|
+
segment: segmentDelimiter
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function splitByString(text, delimiter) {
|
|
46
|
+
const result = [];
|
|
47
|
+
let lastIndex = 0;
|
|
48
|
+
let index = text.indexOf(delimiter);
|
|
49
|
+
while (index !== -1) {
|
|
50
|
+
result.push({
|
|
51
|
+
value: text.slice(lastIndex, index),
|
|
52
|
+
start: lastIndex,
|
|
53
|
+
end: index
|
|
54
|
+
});
|
|
55
|
+
lastIndex = index + delimiter.length;
|
|
56
|
+
index = text.indexOf(delimiter, lastIndex);
|
|
57
|
+
}
|
|
58
|
+
result.push({
|
|
59
|
+
value: text.slice(lastIndex),
|
|
60
|
+
start: lastIndex,
|
|
61
|
+
end: text.length
|
|
62
|
+
});
|
|
63
|
+
return result;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// src/parser.ts
|
|
67
|
+
function fromHL7v2(rawMessage, options = {}) {
|
|
68
|
+
if (!rawMessage.trim()) {
|
|
69
|
+
return EMPTY_MESSAGE;
|
|
70
|
+
}
|
|
71
|
+
const baseDelimiters = {
|
|
72
|
+
...DEFAULT_DELIMITERS,
|
|
73
|
+
...options.delimiters
|
|
74
|
+
};
|
|
75
|
+
let activeDelimiters = baseDelimiters;
|
|
76
|
+
if (options.autoDetectDelimiters !== false) {
|
|
77
|
+
const detected = detectDelimitersFromMSH(
|
|
78
|
+
rawMessage,
|
|
79
|
+
baseDelimiters.segment
|
|
80
|
+
);
|
|
81
|
+
activeDelimiters = { ...baseDelimiters, ...detected };
|
|
82
|
+
}
|
|
83
|
+
const segments = splitByString(rawMessage, activeDelimiters.segment);
|
|
84
|
+
const messageNode = {
|
|
85
|
+
type: "message",
|
|
86
|
+
delimiter: activeDelimiters.segment,
|
|
87
|
+
children: [],
|
|
88
|
+
position: {
|
|
89
|
+
start: { line: 1, column: 1, offset: 0 },
|
|
90
|
+
end: {
|
|
91
|
+
line: segments.length,
|
|
92
|
+
column: (segments.at(-1)?.value.length ?? 0) + 1,
|
|
93
|
+
offset: rawMessage.length
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
let currentLine = 1;
|
|
98
|
+
for (let i = 0; i < segments.length; i++) {
|
|
99
|
+
const seg = segments[i];
|
|
100
|
+
if (!seg) {
|
|
101
|
+
throw new Error("Invalid message");
|
|
102
|
+
}
|
|
103
|
+
if (!seg.value.trim()) {
|
|
104
|
+
currentLine++;
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
const segmentNode = parseSegment(
|
|
108
|
+
seg.value,
|
|
109
|
+
seg.start,
|
|
110
|
+
seg.end,
|
|
111
|
+
currentLine,
|
|
112
|
+
i,
|
|
113
|
+
activeDelimiters
|
|
114
|
+
);
|
|
115
|
+
if (segmentNode) {
|
|
116
|
+
if (!messageNode.children) {
|
|
117
|
+
messageNode.children = [];
|
|
118
|
+
}
|
|
119
|
+
messageNode.children.push(segmentNode);
|
|
120
|
+
}
|
|
121
|
+
currentLine++;
|
|
122
|
+
}
|
|
123
|
+
return messageNode;
|
|
124
|
+
}
|
|
125
|
+
function parseSegment(segmentText, segmentStart, segmentEnd, line, segmentIndex, delimiters) {
|
|
126
|
+
const fields = splitByString(segmentText, delimiters.field);
|
|
127
|
+
if (!fields[0] || fields.length === 0) {
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
const segmentNode = {
|
|
131
|
+
type: "segment",
|
|
132
|
+
name: fields[0].value,
|
|
133
|
+
index: segmentIndex,
|
|
134
|
+
delimiter: delimiters.field,
|
|
135
|
+
children: [],
|
|
136
|
+
position: {
|
|
137
|
+
start: { line, column: 1, offset: segmentStart },
|
|
138
|
+
end: { line, column: segmentText.length + 1, offset: segmentEnd }
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
for (let i = 0; i < fields.length; i++) {
|
|
142
|
+
const f = fields[i];
|
|
143
|
+
if (!f) {
|
|
144
|
+
throw new Error("Invalid message");
|
|
145
|
+
}
|
|
146
|
+
const node = i === 0 ? createHeaderNode(
|
|
147
|
+
f.value,
|
|
148
|
+
i,
|
|
149
|
+
segmentStart + f.start,
|
|
150
|
+
segmentStart + f.end,
|
|
151
|
+
line,
|
|
152
|
+
f.start + 1
|
|
153
|
+
) : createFieldNode(
|
|
154
|
+
f.value,
|
|
155
|
+
i,
|
|
156
|
+
segmentStart + f.start,
|
|
157
|
+
segmentStart + f.end,
|
|
158
|
+
line,
|
|
159
|
+
f.start + 1,
|
|
160
|
+
delimiters
|
|
161
|
+
);
|
|
162
|
+
segmentNode.children.push(node);
|
|
163
|
+
}
|
|
164
|
+
return segmentNode;
|
|
165
|
+
}
|
|
166
|
+
function createHeaderNode(headerText, headerIndex, headerStart, headerEnd, line, column) {
|
|
167
|
+
return {
|
|
168
|
+
type: "header",
|
|
169
|
+
index: headerIndex,
|
|
170
|
+
position: {
|
|
171
|
+
start: { line, column, offset: headerStart },
|
|
172
|
+
end: { line, column: column + headerText.length, offset: headerEnd }
|
|
173
|
+
},
|
|
174
|
+
value: headerText
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
function createFieldNode(fieldText, fieldIndex, fieldStart, fieldEnd, line, column, delimiters) {
|
|
178
|
+
const node = {
|
|
179
|
+
type: "field",
|
|
180
|
+
index: fieldIndex,
|
|
181
|
+
position: {
|
|
182
|
+
start: { line, column, offset: fieldStart },
|
|
183
|
+
end: { line, column: column + fieldText.length, offset: fieldEnd }
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
if (fieldText.includes(delimiters.component)) {
|
|
187
|
+
return {
|
|
188
|
+
...node,
|
|
189
|
+
delimiter: delimiters.component,
|
|
190
|
+
children: parseComponents(
|
|
191
|
+
fieldText,
|
|
192
|
+
fieldStart,
|
|
193
|
+
line,
|
|
194
|
+
column,
|
|
195
|
+
delimiters
|
|
196
|
+
)
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
return { ...node, value: fieldText };
|
|
200
|
+
}
|
|
201
|
+
function parseComponents(fieldText, fieldStart, line, column, delimiters) {
|
|
202
|
+
const comps = splitByString(fieldText, delimiters.component);
|
|
203
|
+
const nodes = [];
|
|
204
|
+
for (let i = 0; i < comps.length; i++) {
|
|
205
|
+
const c = comps[i];
|
|
206
|
+
if (!c) {
|
|
207
|
+
throw new Error("Invalid message");
|
|
208
|
+
}
|
|
209
|
+
const startColumn = column + c.start;
|
|
210
|
+
const base = {
|
|
211
|
+
type: "component",
|
|
212
|
+
index: i,
|
|
213
|
+
position: {
|
|
214
|
+
start: { line, column: startColumn, offset: fieldStart + c.start },
|
|
215
|
+
end: {
|
|
216
|
+
line,
|
|
217
|
+
column: startColumn + c.value.length,
|
|
218
|
+
offset: fieldStart + c.end
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
if (c.value.includes(delimiters.subcomponent)) {
|
|
223
|
+
nodes.push({
|
|
224
|
+
...base,
|
|
225
|
+
delimiter: delimiters.subcomponent,
|
|
226
|
+
children: parseSubcomponents(
|
|
227
|
+
c.value,
|
|
228
|
+
fieldStart + c.start,
|
|
229
|
+
line,
|
|
230
|
+
startColumn,
|
|
231
|
+
delimiters
|
|
232
|
+
)
|
|
233
|
+
});
|
|
234
|
+
} else {
|
|
235
|
+
nodes.push({ ...base, value: c.value });
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return nodes;
|
|
239
|
+
}
|
|
240
|
+
function parseSubcomponents(componentText, componentStart, line, column, delimiters) {
|
|
241
|
+
const subs = splitByString(componentText, delimiters.subcomponent);
|
|
242
|
+
if (!subs[0]) {
|
|
243
|
+
throw new Error("Invalid message");
|
|
244
|
+
}
|
|
245
|
+
return subs.map((s, i) => ({
|
|
246
|
+
type: "subcomponent",
|
|
247
|
+
index: i,
|
|
248
|
+
value: s.value,
|
|
249
|
+
position: {
|
|
250
|
+
start: {
|
|
251
|
+
line,
|
|
252
|
+
column: column + s.start,
|
|
253
|
+
offset: componentStart + s.start
|
|
254
|
+
},
|
|
255
|
+
end: {
|
|
256
|
+
line,
|
|
257
|
+
column: column + s.start + s.value.length,
|
|
258
|
+
offset: componentStart + s.end
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}));
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// src/processor.ts
|
|
265
|
+
var hl7v2Parser = function(options = {}) {
|
|
266
|
+
const self = this;
|
|
267
|
+
function parser(value) {
|
|
268
|
+
return fromHL7v2(value, options);
|
|
269
|
+
}
|
|
270
|
+
self.parser = parser;
|
|
271
|
+
};
|
|
272
|
+
var processor_default = hl7v2Parser;
|
|
273
|
+
export {
|
|
274
|
+
processor_default as hl7v2Parser
|
|
275
|
+
};
|
|
276
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/constants.ts","../src/utils.ts","../src/parser.ts","../src/processor.ts"],"sourcesContent":["import type { HL7v2Node } from '@rethinkhealth/hl7v2-ast';\nimport type { HL7v2Delimiters } from './types';\n\n/**\n * Default HL7v2 delimiters\n */\nexport const DEFAULT_DELIMITERS: HL7v2Delimiters = {\n field: '|',\n component: '^',\n subcomponent: '&',\n repetition: '~',\n escape: '\\\\',\n segment: '\\r', // default to \\r\n};\n\n/**\n * Empty message node\n */\nexport const EMPTY_MESSAGE: HL7v2Node = {\n type: 'message',\n delimiter: DEFAULT_DELIMITERS.segment,\n children: [],\n position: {\n start: { line: 1, column: 1, offset: 0 },\n end: { line: 1, column: 1, offset: 0 },\n },\n};\n","import type { HL7v2Delimiters } from './types';\n\n/**\n * Auto-detect custom delimiters from MSH-1 and MSH-2\n */\nexport function detectDelimitersFromMSH(\n raw: string,\n segmentDelimiter: string\n): Partial<HL7v2Delimiters> {\n const firstLineEnd = raw.indexOf(segmentDelimiter);\n const mshSegment = firstLineEnd >= 0 ? raw.slice(0, firstLineEnd) : raw;\n\n if (!mshSegment.startsWith('MSH')) {\n return {};\n }\n\n const fieldDelimiter = mshSegment[3];\n\n if (!fieldDelimiter) {\n return {};\n }\n\n const encodingChars = mshSegment.slice(4, 8);\n\n if (encodingChars.length !== 4) {\n return {};\n }\n\n return {\n field: fieldDelimiter,\n component: encodingChars[0],\n repetition: encodingChars[1],\n escape: encodingChars[2],\n subcomponent: encodingChars[3],\n segment: segmentDelimiter,\n };\n}\n\n/**\n * Split text by string delimiter, preserving positions\n */\nexport function splitByString(\n text: string,\n delimiter: string\n): Array<{ value: string; start: number; end: number }> {\n const result: { value: string; start: number; end: number }[] = [];\n let lastIndex = 0;\n let index = text.indexOf(delimiter);\n\n while (index !== -1) {\n result.push({\n value: text.slice(lastIndex, index),\n start: lastIndex,\n end: index,\n });\n lastIndex = index + delimiter.length;\n index = text.indexOf(delimiter, lastIndex);\n }\n\n result.push({\n value: text.slice(lastIndex),\n start: lastIndex,\n end: text.length,\n });\n return result;\n}\n","import type { HL7v2Node } from '@rethinkhealth/hl7v2-ast';\nimport { DEFAULT_DELIMITERS, EMPTY_MESSAGE } from './constants';\nimport type { HL7v2Delimiters, ParseOptions } from './types';\nimport { detectDelimitersFromMSH, splitByString } from './utils';\n\n/**\n * Parse an HL7v2 message into a Unist-compatible DOM tree.\n * Tracks lines/columns and preserves delimiters for round-tripping.\n */\nexport function fromHL7v2(\n rawMessage: string,\n options: ParseOptions = {}\n): HL7v2Node {\n if (!rawMessage.trim()) {\n return EMPTY_MESSAGE;\n }\n\n // Merge user-defined delimiters with defaults\n const baseDelimiters: HL7v2Delimiters = {\n ...DEFAULT_DELIMITERS,\n ...options.delimiters,\n };\n\n // Auto-detect from MSH-2 if enabled\n let activeDelimiters = baseDelimiters;\n if (options.autoDetectDelimiters !== false) {\n const detected = detectDelimitersFromMSH(\n rawMessage,\n baseDelimiters.segment\n );\n activeDelimiters = { ...baseDelimiters, ...detected };\n }\n\n // Split into segments\n const segments = splitByString(rawMessage, activeDelimiters.segment);\n\n const messageNode: HL7v2Node = {\n type: 'message',\n delimiter: activeDelimiters.segment,\n children: [],\n position: {\n start: { line: 1, column: 1, offset: 0 },\n end: {\n line: segments.length,\n column: (segments.at(-1)?.value.length ?? 0) + 1,\n offset: rawMessage.length,\n },\n },\n };\n\n let currentLine = 1;\n for (let i = 0; i < segments.length; i++) {\n const seg = segments[i];\n\n if (!seg) {\n // TODO: Handle this case with a better error message\n throw new Error('Invalid message');\n }\n\n if (!seg.value.trim()) {\n currentLine++;\n continue;\n }\n\n const segmentNode = parseSegment(\n seg.value,\n seg.start,\n seg.end,\n currentLine,\n i,\n activeDelimiters\n );\n if (segmentNode) {\n if (!messageNode.children) {\n messageNode.children = [];\n }\n\n messageNode.children.push(segmentNode);\n }\n currentLine++;\n }\n\n return messageNode;\n}\n\n/**\n * Parse a single segment into fields and components\n */\nfunction parseSegment(\n segmentText: string,\n segmentStart: number,\n segmentEnd: number,\n line: number,\n segmentIndex: number,\n delimiters: HL7v2Delimiters\n): HL7v2Node | null {\n // 1. Split into fields\n const fields = splitByString(segmentText, delimiters.field);\n\n // 2. If no fields, return null\n if (!fields[0] || fields.length === 0) {\n return null;\n }\n\n const segmentNode: HL7v2Node = {\n type: 'segment',\n name: fields[0].value,\n index: segmentIndex,\n delimiter: delimiters.field,\n children: [],\n position: {\n start: { line, column: 1, offset: segmentStart },\n end: { line, column: segmentText.length + 1, offset: segmentEnd },\n },\n };\n\n for (let i = 0; i < fields.length; i++) {\n const f = fields[i];\n\n if (!f) {\n // TODO: Handle this case with a better error message\n throw new Error('Invalid message');\n }\n\n // Create header node for first field (segment identifier), field node for others\n const node =\n i === 0\n ? createHeaderNode(\n f.value,\n i,\n segmentStart + f.start,\n segmentStart + f.end,\n line,\n f.start + 1\n )\n : createFieldNode(\n f.value,\n i,\n segmentStart + f.start,\n segmentStart + f.end,\n line,\n f.start + 1,\n delimiters\n );\n\n // biome-ignore lint/style/noNonNullAssertion: This is defined always\n segmentNode.children!.push(node);\n }\n\n return segmentNode;\n}\n\n/**\n * Create a header node for segment identifiers\n */\nfunction createHeaderNode(\n headerText: string,\n headerIndex: number,\n headerStart: number,\n headerEnd: number,\n line: number,\n column: number\n): HL7v2Node {\n return {\n type: 'header',\n index: headerIndex,\n position: {\n start: { line, column, offset: headerStart },\n end: { line, column: column + headerText.length, offset: headerEnd },\n },\n value: headerText,\n };\n}\n\n/**\n * Create a field node, parsing components if needed\n */\nfunction createFieldNode(\n fieldText: string,\n fieldIndex: number,\n fieldStart: number,\n fieldEnd: number,\n line: number,\n column: number,\n delimiters: HL7v2Delimiters\n): HL7v2Node {\n const node: HL7v2Node = {\n type: 'field',\n index: fieldIndex,\n position: {\n start: { line, column, offset: fieldStart },\n end: { line, column: column + fieldText.length, offset: fieldEnd },\n },\n };\n\n if (fieldText.includes(delimiters.component)) {\n return {\n ...node,\n delimiter: delimiters.component,\n children: parseComponents(\n fieldText,\n fieldStart,\n line,\n column,\n delimiters\n ),\n };\n }\n\n return { ...node, value: fieldText };\n}\n\n/**\n * Parse components within a field\n */\nfunction parseComponents(\n fieldText: string,\n fieldStart: number,\n line: number,\n column: number,\n delimiters: HL7v2Delimiters\n): HL7v2Node[] {\n const comps = splitByString(fieldText, delimiters.component);\n const nodes: HL7v2Node[] = [];\n\n for (let i = 0; i < comps.length; i++) {\n const c = comps[i];\n\n if (!c) {\n // TODO: Handle this case with a better error message\n throw new Error('Invalid message');\n }\n\n const startColumn = column + c.start;\n const base: HL7v2Node = {\n type: 'component',\n index: i,\n position: {\n start: { line, column: startColumn, offset: fieldStart + c.start },\n end: {\n line,\n column: startColumn + c.value.length,\n offset: fieldStart + c.end,\n },\n },\n };\n\n if (c.value.includes(delimiters.subcomponent)) {\n nodes.push({\n ...base,\n delimiter: delimiters.subcomponent,\n children: parseSubcomponents(\n c.value,\n fieldStart + c.start,\n line,\n startColumn,\n delimiters\n ),\n });\n } else {\n nodes.push({ ...base, value: c.value });\n }\n }\n\n return nodes;\n}\n\n/**\n * Parse subcomponents within a component\n */\nfunction parseSubcomponents(\n componentText: string,\n componentStart: number,\n line: number,\n column: number,\n delimiters: HL7v2Delimiters\n): HL7v2Node[] {\n const subs = splitByString(componentText, delimiters.subcomponent);\n\n if (!subs[0]) {\n // TODO: Handle this case with a better error message\n throw new Error('Invalid message');\n }\n\n return subs.map((s, i) => ({\n type: 'subcomponent',\n index: i,\n value: s.value,\n position: {\n start: {\n line,\n column: column + s.start,\n offset: componentStart + s.start,\n },\n end: {\n line,\n column: column + s.start + s.value.length,\n offset: componentStart + s.end,\n },\n },\n }));\n}\n","import type { HL7v2Node } from '@rethinkhealth/hl7v2-ast';\nimport type { Plugin } from 'unified';\nimport { fromHL7v2 } from './parser';\nimport type { ParseOptions } from './types';\n\nconst hl7v2Parser: Plugin<[ParseOptions?], undefined, HL7v2Node> = function (\n options: ParseOptions = {}\n): void {\n // biome-ignore lint/complexity/noUselessThisAlias: this is a plugin\n const self = this;\n\n function parser(this: unknown, value: string): HL7v2Node {\n return fromHL7v2(value, options);\n }\n\n self.parser = parser;\n};\n\nexport default hl7v2Parser;\n"],"mappings":";AAMO,IAAM,qBAAsC;AAAA,EACjD,OAAO;AAAA,EACP,WAAW;AAAA,EACX,cAAc;AAAA,EACd,YAAY;AAAA,EACZ,QAAQ;AAAA,EACR,SAAS;AAAA;AACX;AAKO,IAAM,gBAA2B;AAAA,EACtC,MAAM;AAAA,EACN,WAAW,mBAAmB;AAAA,EAC9B,UAAU,CAAC;AAAA,EACX,UAAU;AAAA,IACR,OAAO,EAAE,MAAM,GAAG,QAAQ,GAAG,QAAQ,EAAE;AAAA,IACvC,KAAK,EAAE,MAAM,GAAG,QAAQ,GAAG,QAAQ,EAAE;AAAA,EACvC;AACF;;;ACrBO,SAAS,wBACd,KACA,kBAC0B;AAC1B,QAAM,eAAe,IAAI,QAAQ,gBAAgB;AACjD,QAAM,aAAa,gBAAgB,IAAI,IAAI,MAAM,GAAG,YAAY,IAAI;AAEpE,MAAI,CAAC,WAAW,WAAW,KAAK,GAAG;AACjC,WAAO,CAAC;AAAA,EACV;AAEA,QAAM,iBAAiB,WAAW,CAAC;AAEnC,MAAI,CAAC,gBAAgB;AACnB,WAAO,CAAC;AAAA,EACV;AAEA,QAAM,gBAAgB,WAAW,MAAM,GAAG,CAAC;AAE3C,MAAI,cAAc,WAAW,GAAG;AAC9B,WAAO,CAAC;AAAA,EACV;AAEA,SAAO;AAAA,IACL,OAAO;AAAA,IACP,WAAW,cAAc,CAAC;AAAA,IAC1B,YAAY,cAAc,CAAC;AAAA,IAC3B,QAAQ,cAAc,CAAC;AAAA,IACvB,cAAc,cAAc,CAAC;AAAA,IAC7B,SAAS;AAAA,EACX;AACF;AAKO,SAAS,cACd,MAC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b,WAAS,OAAsB,OAA0B;AACvD,WAAO,UAAU,OAAO,OAAO;AAAA,EACjC;AAEA,OAAK,SAAS;AAChB;AAEA,IAAO,oBAAQ;","names":[]}
|
package/dist/parser.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { HL7v2Node } from '@rethinkhealth/hl7v2-ast';
|
|
2
|
+
import type { ParseOptions } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Parse an HL7v2 message into a Unist-compatible DOM tree.
|
|
5
|
+
* Tracks lines/columns and preserves delimiters for round-tripping.
|
|
6
|
+
*/
|
|
7
|
+
export declare function fromHL7v2(rawMessage: string, options?: ParseOptions): HL7v2Node;
|
|
8
|
+
//# sourceMappingURL=parser.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../src/parser.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAE1D,OAAO,KAAK,EAAmB,YAAY,EAAE,MAAM,SAAS,CAAC;AAG7D;;;GAGG;AACH,wBAAgB,SAAS,CACvB,UAAU,EAAE,MAAM,EAClB,OAAO,GAAE,YAAiB,GACzB,SAAS,CAuEX"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { HL7v2Node } from '@rethinkhealth/hl7v2-ast';
|
|
2
|
+
import type { Plugin } from 'unified';
|
|
3
|
+
import type { ParseOptions } from './types';
|
|
4
|
+
declare const hl7v2Parser: Plugin<[ParseOptions?], undefined, HL7v2Node>;
|
|
5
|
+
export default hl7v2Parser;
|
|
6
|
+
//# sourceMappingURL=processor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"processor.d.ts","sourceRoot":"","sources":["../src/processor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAC1D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAEtC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAE5C,QAAA,MAAM,WAAW,EAAE,MAAM,CAAC,CAAC,YAAY,CAAC,CAAC,EAAE,SAAS,EAAE,SAAS,CAW9D,CAAC;AAEF,eAAe,WAAW,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface ParseOptions {
|
|
2
|
+
delimiters?: Partial<HL7v2Delimiters>;
|
|
3
|
+
autoDetectDelimiters?: boolean;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* HL7v2 Delimiters type
|
|
7
|
+
*/
|
|
8
|
+
export interface HL7v2Delimiters {
|
|
9
|
+
field: string;
|
|
10
|
+
component: string;
|
|
11
|
+
subcomponent: string;
|
|
12
|
+
repetition: string;
|
|
13
|
+
escape: string;
|
|
14
|
+
segment: string;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,YAAY;IAC3B,UAAU,CAAC,EAAE,OAAO,CAAC,eAAe,CAAC,CAAC;IACtC,oBAAoB,CAAC,EAAE,OAAO,CAAC;CAChC;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,MAAM,CAAC;IAClB,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;CACjB"}
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { HL7v2Delimiters } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Auto-detect custom delimiters from MSH-1 and MSH-2
|
|
4
|
+
*/
|
|
5
|
+
export declare function detectDelimitersFromMSH(raw: string, segmentDelimiter: string): Partial<HL7v2Delimiters>;
|
|
6
|
+
/**
|
|
7
|
+
* Split text by string delimiter, preserving positions
|
|
8
|
+
*/
|
|
9
|
+
export declare function splitByString(text: string, delimiter: string): Array<{
|
|
10
|
+
value: string;
|
|
11
|
+
start: number;
|
|
12
|
+
end: number;
|
|
13
|
+
}>;
|
|
14
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE/C;;GAEG;AACH,wBAAgB,uBAAuB,CACrC,GAAG,EAAE,MAAM,EACX,gBAAgB,EAAE,MAAM,GACvB,OAAO,CAAC,eAAe,CAAC,CA4B1B;AAED;;GAEG;AACH,wBAAgB,aAAa,CAC3B,IAAI,EAAE,MAAM,EACZ,SAAS,EAAE,MAAM,GAChB,KAAK,CAAC;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,CAAC,CAqBtD"}
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rethinkhealth/hl7v2-parser",
|
|
3
|
+
"description": "hl7v2 plugin to parse hl7v2 messages",
|
|
4
|
+
"version": "0.2.0",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "Melek Somai",
|
|
8
|
+
"email": "melek@rethinkhealth.io"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"files": [
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
15
|
+
"exports": {
|
|
16
|
+
".": "./dist/index.js"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"unified": "11.0.1"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@types/node": "22.15.31",
|
|
23
|
+
"@types/unist": "^3.0.3",
|
|
24
|
+
"@vitest/coverage-c8": "^0.33.0",
|
|
25
|
+
"@vitest/coverage-v8": "^3.2.4",
|
|
26
|
+
"tsup": "8.5.0",
|
|
27
|
+
"typescript": "^5.8.3",
|
|
28
|
+
"vitest": "^3.2.4",
|
|
29
|
+
"@rethinkhealth/testing": "0.0.0",
|
|
30
|
+
"@rethinkhealth/tsconfig": "0.0.0",
|
|
31
|
+
"@rethinkhealth/hl7v2-ast": "0.1.0"
|
|
32
|
+
},
|
|
33
|
+
"repository": "rethinkhealth/hl7v2.git",
|
|
34
|
+
"homepage": "https://www.rethinkhealth.io/hl7v2/docs",
|
|
35
|
+
"keywords": [
|
|
36
|
+
"health",
|
|
37
|
+
"healthcare",
|
|
38
|
+
"hl7",
|
|
39
|
+
"hl7v2",
|
|
40
|
+
"nodejs",
|
|
41
|
+
"typescript"
|
|
42
|
+
],
|
|
43
|
+
"packageManager": "pnpm@10.12.1",
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"access": "public"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "tsup && tsc --emitDeclarationOnly",
|
|
49
|
+
"check-types": "tsc --noEmit",
|
|
50
|
+
"test": "vitest run",
|
|
51
|
+
"test:coverage": "vitest run --coverage",
|
|
52
|
+
"test:watch": "vitest"
|
|
53
|
+
}
|
|
54
|
+
}
|