@pritiranjan/hl7v2 0.1.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.
@@ -0,0 +1,309 @@
1
+ import { H as HL7Message, a as HL7Segment, E as EncodingChars } from './schema-qr-scwmL.cjs';
2
+ export { D as DEFAULT_ENCODING, b as HL7Field, M as MessageType } from './schema-qr-scwmL.cjs';
3
+
4
+ interface ParseOptions {
5
+ /**
6
+ * When `true`, HL7 escape sequences within field values are decoded to their
7
+ * plain-text equivalents (e.g. `\F\` → `|`).
8
+ *
9
+ * Defaults to `false` so that `encode(parse(raw))` is byte-identical
10
+ * to the original input — essential for ACK generation and message forwarding.
11
+ */
12
+ decodeEscapes?: boolean;
13
+ }
14
+ /**
15
+ * Parse an HL7 v2.x message string into a structured {@link HL7Message}.
16
+ *
17
+ * ### Supported inputs
18
+ * - HL7 versions 2.1 – 2.8
19
+ * - Line endings: `\r` (canonical), `\n`, or `\r\n` — all normalised to `\r`
20
+ * - Custom encoding characters (non-default separators)
21
+ * - Messages with blank lines (skipped silently)
22
+ *
23
+ * ### Round-trip fidelity
24
+ * By default (`decodeEscapes: false`), field values retain their raw HL7
25
+ * escape sequences, making `encode(parse(raw)) === raw` hold true.
26
+ * Use `decodeEscapes: true` only when you need to display or process the
27
+ * human-readable values and do not intend to re-encode the message.
28
+ *
29
+ * @throws {InvalidHL7Error} When the input is empty or does not start with MSH
30
+ * @throws {HL7ParseError} When a segment identifier or field structure is invalid
31
+ *
32
+ * @example
33
+ * import { parse } from 'hl7v2';
34
+ *
35
+ * const msg = parse(rawString);
36
+ * console.log(msg.messageType); // { type: 'ADT', event: 'A01', structure: 'ADT_A01' }
37
+ * console.log(msg.version); // '2.5.1'
38
+ */
39
+ declare function parse(raw: string, options?: ParseOptions): HL7Message;
40
+ /**
41
+ * Return `true` if the input looks like an HL7 v2 message.
42
+ * This is a fast heuristic check — it does not fully parse the message.
43
+ */
44
+ declare function isHL7(input: string): boolean;
45
+
46
+ interface EncodeOptions {
47
+ /**
48
+ * Line ending to use between segments.
49
+ * HL7 v2 canonical is `'\r'`. Some systems expect `'\r\n'` or `'\n'`.
50
+ * @default '\r'
51
+ */
52
+ lineEnding?: '\r' | '\n' | '\r\n';
53
+ /**
54
+ * When `true`, a trailing line ending is appended after the last segment.
55
+ * @default false
56
+ */
57
+ trailingNewline?: boolean;
58
+ }
59
+ declare function encodeSegment(seg: HL7Segment, enc: EncodingChars): string;
60
+ /**
61
+ * Encode an {@link HL7Message} back into a raw HL7 v2 string.
62
+ *
63
+ * ### Round-trip guarantee
64
+ * When called on a message produced by `parse(raw)` with default options
65
+ * (i.e., `decodeEscapes` was **not** set to `true`), this function produces
66
+ * output that is byte-identical to the original input, modulo line endings.
67
+ *
68
+ * ### Encoding characters
69
+ * The message's own `encoding` field is used. Override via `options.encoding`
70
+ * if you need to re-encode to a different separator set (rare).
71
+ *
72
+ * @param msg - The message to encode
73
+ * @param options - Optional output configuration
74
+ *
75
+ * @example
76
+ * import { parse, encode } from 'hl7v2';
77
+ *
78
+ * const msg = parse(rawString);
79
+ * const back = encode(msg); // byte-identical to rawString (modulo line endings)
80
+ *
81
+ * // Modify a field and re-encode
82
+ * const mutable = structuredClone(msg) as HL7Message;
83
+ * // ... set fields ...
84
+ * const updated = encode(mutable);
85
+ */
86
+ declare function encode(msg: HL7Message, options?: EncodeOptions): string;
87
+
88
+ /**
89
+ * Return the first segment with the given identifier.
90
+ *
91
+ * When a message contains multiple segments with the same identifier (e.g.
92
+ * repeating OBX groups), pass `{ segmentIndex: N }` (1-based) to select which
93
+ * occurrence to return. Defaults to the first occurrence (`segmentIndex: 1`).
94
+ *
95
+ * @throws {SegmentNotFoundError} When no segment with `id` exists at the given index.
96
+ *
97
+ * @example
98
+ * const pid = segment(msg, 'PID');
99
+ * const obx1 = segment(msg, 'OBX', { segmentIndex: 1 }); // first OBX
100
+ * const obx2 = segment(msg, 'OBX', { segmentIndex: 2 }); // second OBX
101
+ */
102
+ declare function segment(msg: HL7Message, id: string, options?: {
103
+ segmentIndex?: number;
104
+ }): HL7Segment;
105
+ /**
106
+ * Return all segments with the given identifier.
107
+ * Returns an empty array (never throws) when none are found.
108
+ *
109
+ * Useful for repeating segments such as OBX (lab results), NTE (notes),
110
+ * or DG1 (diagnoses).
111
+ *
112
+ * @example
113
+ * const allOBX = segments(msg, 'OBX');
114
+ * for (const obx of allOBX) {
115
+ * console.log(get(msg, obx, 5)); // OBX.5 — observation value
116
+ * }
117
+ */
118
+ declare function segments(msg: HL7Message, id: string): HL7Segment[];
119
+ /**
120
+ * Return `true` if the message contains at least one segment with the given identifier.
121
+ *
122
+ * @example
123
+ * if (hasSegment(msg, 'PV1')) {
124
+ * const pv1 = segment(msg, 'PV1');
125
+ * }
126
+ */
127
+ declare function hasSegment(msg: HL7Message, id: string): boolean;
128
+ interface GetOptions {
129
+ /**
130
+ * 1-based repetition number. Defaults to `1` (first repetition).
131
+ *
132
+ * @example
133
+ * // PID.3 can repeat — get the second identifier
134
+ * get(msg, 'PID', 3, undefined, undefined, { repetition: 2 });
135
+ */
136
+ repetition?: number;
137
+ /**
138
+ * When `true`, decode HL7 escape sequences in the returned value.
139
+ * Defaults to `false`.
140
+ */
141
+ decode?: boolean;
142
+ /**
143
+ * 1-based index when the message contains multiple segments with the same
144
+ * identifier. Defaults to `1` (first occurrence).
145
+ *
146
+ * @example
147
+ * // Get OBX.5 from the third OBX segment
148
+ * get(msg, 'OBX', 5, undefined, undefined, { segmentIndex: 3 });
149
+ */
150
+ segmentIndex?: number;
151
+ }
152
+ /**
153
+ * Extract a scalar string value from a message field using 1-based HL7 addressing.
154
+ *
155
+ * Returns an empty string `''` rather than throwing when the field, component,
156
+ * or sub-component does not exist — reflecting HL7's rule that absent elements
157
+ * are equivalent to empty strings.
158
+ *
159
+ * HL7 addressing reference (1-based throughout):
160
+ * - `get(msg, 'PID', 3)` → PID.3 (first component, first repetition)
161
+ * - `get(msg, 'PID', 5, 1)` → PID.5.1 (family name component)
162
+ * - `get(msg, 'PID', 5, 2)` → PID.5.2 (given name component)
163
+ * - `get(msg, 'PID', 11, 1, 1)` → PID.11.1.1 (sub-component)
164
+ * - `get(msg, 'OBX', 5, 1, 1, { repetition: 2 })` → OBX.5[2].1.1 (second repetition)
165
+ *
166
+ * @param msg - Parsed HL7 message
167
+ * @param segmentId - Three-letter segment identifier (case-insensitive)
168
+ * @param field - 1-based field number
169
+ * @param component - 1-based component number (defaults to 1)
170
+ * @param subComponent - 1-based sub-component number (defaults to 1)
171
+ * @param options - See {@link GetOptions}
172
+ *
173
+ * @example
174
+ * import { parse, get } from 'hl7v2';
175
+ *
176
+ * const msg = parse(raw);
177
+ * const mrn = get(msg, 'PID', 3); // PID.3 — medical record number
178
+ * const lastName = get(msg, 'PID', 5, 1); // PID.5.1 — family name
179
+ * const firstName = get(msg, 'PID', 5, 2); // PID.5.2 — given name
180
+ */
181
+ declare function get(msg: HL7Message, segmentId: string, field: number, component?: number, subComponent?: number, options?: GetOptions): string;
182
+ /**
183
+ * Extract a field value from a known segment reference (returned by {@link segment}
184
+ * or {@link segments}).
185
+ *
186
+ * Equivalent to {@link get} but accepts an `HL7Segment` directly — useful when
187
+ * iterating over repeating segments.
188
+ *
189
+ * @example
190
+ * for (const obx of segments(msg, 'OBX')) {
191
+ * const value = getFromSegment(obx, msg, 5); // OBX.5 for each OBX segment
192
+ * }
193
+ */
194
+ declare function getFromSegment(seg: HL7Segment, msg: HL7Message, field: number, component?: number, subComponent?: number, options?: Omit<GetOptions, 'segmentIndex'>): string;
195
+ /**
196
+ * Return all repetitions of a field as a `string[][][]` structure (one entry
197
+ * per repetition, preserving the `[component][subComponent]` shape).
198
+ *
199
+ * Useful for fields like PID.3 (patient identifier list) that carry multiple
200
+ * typed identifiers — each with its own components — across repetitions.
201
+ *
202
+ * @example
203
+ * const ids = getRepetitions(msg, 'PID', 3);
204
+ * ids[0]?.[0]?.[0] // → first identifier value (PID.3[1].1)
205
+ * ids[0]?.[3]?.[0] // → first identifier's assigning authority (PID.3[1].4)
206
+ * ids[1]?.[0]?.[0] // → second identifier value (PID.3[2].1)
207
+ */
208
+ declare function getRepetitions(msg: HL7Message, segmentId: string, field: number, options?: Omit<GetOptions, 'repetition'>): string[][][];
209
+
210
+ /**
211
+ * Decode HL7 v2 escape sequences within a field value.
212
+ *
213
+ * Handles all standard sequences defined in the HL7 v2.x specification:
214
+ * - `\F\` → field separator
215
+ * - `\S\` → component separator
216
+ * - `\T\` → sub-component separator
217
+ * - `\R\` → repetition separator
218
+ * - `\E\` → escape character
219
+ * - `\H\` → start highlighting (stripped — presentation concern)
220
+ * - `\N\` → normal text / end highlighting (stripped)
221
+ * - `\.br\` → carriage return / line break → `\n`
222
+ * - `\Xhh…\` → hex-encoded bytes → UTF-8 string
223
+ *
224
+ * Unknown or locally-defined sequences (`\Zxxx\`) are preserved as-is.
225
+ *
226
+ * @param value - Raw field value that may contain escape sequences
227
+ * @param enc - Encoding characters from the message's MSH segment
228
+ */
229
+ declare function decodeEscapes(value: string, enc: EncodingChars): string;
230
+ /**
231
+ * Encode a plain string value for safe inclusion in an HL7 v2 field,
232
+ * escaping any characters that have special meaning in the given encoding.
233
+ *
234
+ * Characters are escaped in this order to avoid double-escaping:
235
+ * 1. Escape character itself (`\E\`)
236
+ * 2. Field separator (`\F\`)
237
+ * 3. Component separator (`\S\`)
238
+ * 4. Repetition separator(`\R\`)
239
+ * 5. Sub-component sep (`\T\`)
240
+ *
241
+ * @param value - Plain string to encode
242
+ * @param enc - Encoding characters from the target message
243
+ */
244
+ declare function encodeEscapes(value: string, enc: EncodingChars): string;
245
+
246
+ /**
247
+ * Parse an HL7 v2 Date/Time string into a JavaScript `Date`.
248
+ *
249
+ * Supported formats (per HL7 v2 spec section 2.8.21):
250
+ * - `YYYY`
251
+ * - `YYYYMM`
252
+ * - `YYYYMMDD`
253
+ * - `YYYYMMDDHHMM`
254
+ * - `YYYYMMDDHHMMSS`
255
+ * - `YYYYMMDDHHMMSS.S[SSS]`
256
+ * - Any of the above with `+HHMM` or `-HHMM` timezone offset suffix
257
+ *
258
+ * @returns A `Date` object in UTC, or `undefined` if the value is empty.
259
+ * @throws {HL7ParseError} If the string is non-empty but unparseable.
260
+ */
261
+ declare function parseHL7DateTime(raw: string): Date | undefined;
262
+ /**
263
+ * Format a JavaScript `Date` as an HL7 v2 timestamp string in UTC.
264
+ * Output format: `YYYYMMDDHHMMSS`
265
+ */
266
+ declare function formatHL7DateTime(date: Date): string;
267
+ /**
268
+ * Format a JavaScript `Date` as an HL7 v2 date-only string in UTC.
269
+ * Output format: `YYYYMMDD`
270
+ */
271
+ declare function formatHL7Date(date: Date): string;
272
+
273
+ /**
274
+ * Thrown when an HL7 v2 message string cannot be parsed.
275
+ *
276
+ * The `line` and `segmentId` properties help pinpoint the exact failure location
277
+ * when debugging multi-segment messages from production systems.
278
+ */
279
+ declare class HL7ParseError extends Error {
280
+ /** 1-based line number in the source string where the error occurred. */
281
+ readonly line: number | undefined;
282
+ /** Segment identifier that was being parsed when the error occurred. */
283
+ readonly segmentId: string | undefined;
284
+ constructor(message: string, options?: {
285
+ line?: number;
286
+ segmentId?: string;
287
+ cause?: unknown;
288
+ });
289
+ }
290
+ /**
291
+ * Thrown when a required segment is not present in the message.
292
+ *
293
+ * @example
294
+ * // Thrown by segment() when the segment is not found
295
+ * const pid = segment(msg, 'PID'); // throws if no PID segment
296
+ */
297
+ declare class SegmentNotFoundError extends Error {
298
+ readonly segmentId: string;
299
+ constructor(segmentId: string);
300
+ }
301
+ /**
302
+ * Thrown when an HL7 message cannot be identified as valid HL7 v2.
303
+ * Usually means the input is empty, truncated, or a completely different format.
304
+ */
305
+ declare class InvalidHL7Error extends Error {
306
+ constructor(reason: string);
307
+ }
308
+
309
+ export { type EncodeOptions, EncodingChars, type GetOptions, HL7Message, HL7ParseError, HL7Segment, InvalidHL7Error, type ParseOptions, SegmentNotFoundError, decodeEscapes, encode, encodeEscapes, encodeSegment, formatHL7Date, formatHL7DateTime, get, getFromSegment, getRepetitions, hasSegment, isHL7, parse, parseHL7DateTime, segment, segments };
@@ -0,0 +1,309 @@
1
+ import { H as HL7Message, a as HL7Segment, E as EncodingChars } from './schema-qr-scwmL.js';
2
+ export { D as DEFAULT_ENCODING, b as HL7Field, M as MessageType } from './schema-qr-scwmL.js';
3
+
4
+ interface ParseOptions {
5
+ /**
6
+ * When `true`, HL7 escape sequences within field values are decoded to their
7
+ * plain-text equivalents (e.g. `\F\` → `|`).
8
+ *
9
+ * Defaults to `false` so that `encode(parse(raw))` is byte-identical
10
+ * to the original input — essential for ACK generation and message forwarding.
11
+ */
12
+ decodeEscapes?: boolean;
13
+ }
14
+ /**
15
+ * Parse an HL7 v2.x message string into a structured {@link HL7Message}.
16
+ *
17
+ * ### Supported inputs
18
+ * - HL7 versions 2.1 – 2.8
19
+ * - Line endings: `\r` (canonical), `\n`, or `\r\n` — all normalised to `\r`
20
+ * - Custom encoding characters (non-default separators)
21
+ * - Messages with blank lines (skipped silently)
22
+ *
23
+ * ### Round-trip fidelity
24
+ * By default (`decodeEscapes: false`), field values retain their raw HL7
25
+ * escape sequences, making `encode(parse(raw)) === raw` hold true.
26
+ * Use `decodeEscapes: true` only when you need to display or process the
27
+ * human-readable values and do not intend to re-encode the message.
28
+ *
29
+ * @throws {InvalidHL7Error} When the input is empty or does not start with MSH
30
+ * @throws {HL7ParseError} When a segment identifier or field structure is invalid
31
+ *
32
+ * @example
33
+ * import { parse } from 'hl7v2';
34
+ *
35
+ * const msg = parse(rawString);
36
+ * console.log(msg.messageType); // { type: 'ADT', event: 'A01', structure: 'ADT_A01' }
37
+ * console.log(msg.version); // '2.5.1'
38
+ */
39
+ declare function parse(raw: string, options?: ParseOptions): HL7Message;
40
+ /**
41
+ * Return `true` if the input looks like an HL7 v2 message.
42
+ * This is a fast heuristic check — it does not fully parse the message.
43
+ */
44
+ declare function isHL7(input: string): boolean;
45
+
46
+ interface EncodeOptions {
47
+ /**
48
+ * Line ending to use between segments.
49
+ * HL7 v2 canonical is `'\r'`. Some systems expect `'\r\n'` or `'\n'`.
50
+ * @default '\r'
51
+ */
52
+ lineEnding?: '\r' | '\n' | '\r\n';
53
+ /**
54
+ * When `true`, a trailing line ending is appended after the last segment.
55
+ * @default false
56
+ */
57
+ trailingNewline?: boolean;
58
+ }
59
+ declare function encodeSegment(seg: HL7Segment, enc: EncodingChars): string;
60
+ /**
61
+ * Encode an {@link HL7Message} back into a raw HL7 v2 string.
62
+ *
63
+ * ### Round-trip guarantee
64
+ * When called on a message produced by `parse(raw)` with default options
65
+ * (i.e., `decodeEscapes` was **not** set to `true`), this function produces
66
+ * output that is byte-identical to the original input, modulo line endings.
67
+ *
68
+ * ### Encoding characters
69
+ * The message's own `encoding` field is used. Override via `options.encoding`
70
+ * if you need to re-encode to a different separator set (rare).
71
+ *
72
+ * @param msg - The message to encode
73
+ * @param options - Optional output configuration
74
+ *
75
+ * @example
76
+ * import { parse, encode } from 'hl7v2';
77
+ *
78
+ * const msg = parse(rawString);
79
+ * const back = encode(msg); // byte-identical to rawString (modulo line endings)
80
+ *
81
+ * // Modify a field and re-encode
82
+ * const mutable = structuredClone(msg) as HL7Message;
83
+ * // ... set fields ...
84
+ * const updated = encode(mutable);
85
+ */
86
+ declare function encode(msg: HL7Message, options?: EncodeOptions): string;
87
+
88
+ /**
89
+ * Return the first segment with the given identifier.
90
+ *
91
+ * When a message contains multiple segments with the same identifier (e.g.
92
+ * repeating OBX groups), pass `{ segmentIndex: N }` (1-based) to select which
93
+ * occurrence to return. Defaults to the first occurrence (`segmentIndex: 1`).
94
+ *
95
+ * @throws {SegmentNotFoundError} When no segment with `id` exists at the given index.
96
+ *
97
+ * @example
98
+ * const pid = segment(msg, 'PID');
99
+ * const obx1 = segment(msg, 'OBX', { segmentIndex: 1 }); // first OBX
100
+ * const obx2 = segment(msg, 'OBX', { segmentIndex: 2 }); // second OBX
101
+ */
102
+ declare function segment(msg: HL7Message, id: string, options?: {
103
+ segmentIndex?: number;
104
+ }): HL7Segment;
105
+ /**
106
+ * Return all segments with the given identifier.
107
+ * Returns an empty array (never throws) when none are found.
108
+ *
109
+ * Useful for repeating segments such as OBX (lab results), NTE (notes),
110
+ * or DG1 (diagnoses).
111
+ *
112
+ * @example
113
+ * const allOBX = segments(msg, 'OBX');
114
+ * for (const obx of allOBX) {
115
+ * console.log(get(msg, obx, 5)); // OBX.5 — observation value
116
+ * }
117
+ */
118
+ declare function segments(msg: HL7Message, id: string): HL7Segment[];
119
+ /**
120
+ * Return `true` if the message contains at least one segment with the given identifier.
121
+ *
122
+ * @example
123
+ * if (hasSegment(msg, 'PV1')) {
124
+ * const pv1 = segment(msg, 'PV1');
125
+ * }
126
+ */
127
+ declare function hasSegment(msg: HL7Message, id: string): boolean;
128
+ interface GetOptions {
129
+ /**
130
+ * 1-based repetition number. Defaults to `1` (first repetition).
131
+ *
132
+ * @example
133
+ * // PID.3 can repeat — get the second identifier
134
+ * get(msg, 'PID', 3, undefined, undefined, { repetition: 2 });
135
+ */
136
+ repetition?: number;
137
+ /**
138
+ * When `true`, decode HL7 escape sequences in the returned value.
139
+ * Defaults to `false`.
140
+ */
141
+ decode?: boolean;
142
+ /**
143
+ * 1-based index when the message contains multiple segments with the same
144
+ * identifier. Defaults to `1` (first occurrence).
145
+ *
146
+ * @example
147
+ * // Get OBX.5 from the third OBX segment
148
+ * get(msg, 'OBX', 5, undefined, undefined, { segmentIndex: 3 });
149
+ */
150
+ segmentIndex?: number;
151
+ }
152
+ /**
153
+ * Extract a scalar string value from a message field using 1-based HL7 addressing.
154
+ *
155
+ * Returns an empty string `''` rather than throwing when the field, component,
156
+ * or sub-component does not exist — reflecting HL7's rule that absent elements
157
+ * are equivalent to empty strings.
158
+ *
159
+ * HL7 addressing reference (1-based throughout):
160
+ * - `get(msg, 'PID', 3)` → PID.3 (first component, first repetition)
161
+ * - `get(msg, 'PID', 5, 1)` → PID.5.1 (family name component)
162
+ * - `get(msg, 'PID', 5, 2)` → PID.5.2 (given name component)
163
+ * - `get(msg, 'PID', 11, 1, 1)` → PID.11.1.1 (sub-component)
164
+ * - `get(msg, 'OBX', 5, 1, 1, { repetition: 2 })` → OBX.5[2].1.1 (second repetition)
165
+ *
166
+ * @param msg - Parsed HL7 message
167
+ * @param segmentId - Three-letter segment identifier (case-insensitive)
168
+ * @param field - 1-based field number
169
+ * @param component - 1-based component number (defaults to 1)
170
+ * @param subComponent - 1-based sub-component number (defaults to 1)
171
+ * @param options - See {@link GetOptions}
172
+ *
173
+ * @example
174
+ * import { parse, get } from 'hl7v2';
175
+ *
176
+ * const msg = parse(raw);
177
+ * const mrn = get(msg, 'PID', 3); // PID.3 — medical record number
178
+ * const lastName = get(msg, 'PID', 5, 1); // PID.5.1 — family name
179
+ * const firstName = get(msg, 'PID', 5, 2); // PID.5.2 — given name
180
+ */
181
+ declare function get(msg: HL7Message, segmentId: string, field: number, component?: number, subComponent?: number, options?: GetOptions): string;
182
+ /**
183
+ * Extract a field value from a known segment reference (returned by {@link segment}
184
+ * or {@link segments}).
185
+ *
186
+ * Equivalent to {@link get} but accepts an `HL7Segment` directly — useful when
187
+ * iterating over repeating segments.
188
+ *
189
+ * @example
190
+ * for (const obx of segments(msg, 'OBX')) {
191
+ * const value = getFromSegment(obx, msg, 5); // OBX.5 for each OBX segment
192
+ * }
193
+ */
194
+ declare function getFromSegment(seg: HL7Segment, msg: HL7Message, field: number, component?: number, subComponent?: number, options?: Omit<GetOptions, 'segmentIndex'>): string;
195
+ /**
196
+ * Return all repetitions of a field as a `string[][][]` structure (one entry
197
+ * per repetition, preserving the `[component][subComponent]` shape).
198
+ *
199
+ * Useful for fields like PID.3 (patient identifier list) that carry multiple
200
+ * typed identifiers — each with its own components — across repetitions.
201
+ *
202
+ * @example
203
+ * const ids = getRepetitions(msg, 'PID', 3);
204
+ * ids[0]?.[0]?.[0] // → first identifier value (PID.3[1].1)
205
+ * ids[0]?.[3]?.[0] // → first identifier's assigning authority (PID.3[1].4)
206
+ * ids[1]?.[0]?.[0] // → second identifier value (PID.3[2].1)
207
+ */
208
+ declare function getRepetitions(msg: HL7Message, segmentId: string, field: number, options?: Omit<GetOptions, 'repetition'>): string[][][];
209
+
210
+ /**
211
+ * Decode HL7 v2 escape sequences within a field value.
212
+ *
213
+ * Handles all standard sequences defined in the HL7 v2.x specification:
214
+ * - `\F\` → field separator
215
+ * - `\S\` → component separator
216
+ * - `\T\` → sub-component separator
217
+ * - `\R\` → repetition separator
218
+ * - `\E\` → escape character
219
+ * - `\H\` → start highlighting (stripped — presentation concern)
220
+ * - `\N\` → normal text / end highlighting (stripped)
221
+ * - `\.br\` → carriage return / line break → `\n`
222
+ * - `\Xhh…\` → hex-encoded bytes → UTF-8 string
223
+ *
224
+ * Unknown or locally-defined sequences (`\Zxxx\`) are preserved as-is.
225
+ *
226
+ * @param value - Raw field value that may contain escape sequences
227
+ * @param enc - Encoding characters from the message's MSH segment
228
+ */
229
+ declare function decodeEscapes(value: string, enc: EncodingChars): string;
230
+ /**
231
+ * Encode a plain string value for safe inclusion in an HL7 v2 field,
232
+ * escaping any characters that have special meaning in the given encoding.
233
+ *
234
+ * Characters are escaped in this order to avoid double-escaping:
235
+ * 1. Escape character itself (`\E\`)
236
+ * 2. Field separator (`\F\`)
237
+ * 3. Component separator (`\S\`)
238
+ * 4. Repetition separator(`\R\`)
239
+ * 5. Sub-component sep (`\T\`)
240
+ *
241
+ * @param value - Plain string to encode
242
+ * @param enc - Encoding characters from the target message
243
+ */
244
+ declare function encodeEscapes(value: string, enc: EncodingChars): string;
245
+
246
+ /**
247
+ * Parse an HL7 v2 Date/Time string into a JavaScript `Date`.
248
+ *
249
+ * Supported formats (per HL7 v2 spec section 2.8.21):
250
+ * - `YYYY`
251
+ * - `YYYYMM`
252
+ * - `YYYYMMDD`
253
+ * - `YYYYMMDDHHMM`
254
+ * - `YYYYMMDDHHMMSS`
255
+ * - `YYYYMMDDHHMMSS.S[SSS]`
256
+ * - Any of the above with `+HHMM` or `-HHMM` timezone offset suffix
257
+ *
258
+ * @returns A `Date` object in UTC, or `undefined` if the value is empty.
259
+ * @throws {HL7ParseError} If the string is non-empty but unparseable.
260
+ */
261
+ declare function parseHL7DateTime(raw: string): Date | undefined;
262
+ /**
263
+ * Format a JavaScript `Date` as an HL7 v2 timestamp string in UTC.
264
+ * Output format: `YYYYMMDDHHMMSS`
265
+ */
266
+ declare function formatHL7DateTime(date: Date): string;
267
+ /**
268
+ * Format a JavaScript `Date` as an HL7 v2 date-only string in UTC.
269
+ * Output format: `YYYYMMDD`
270
+ */
271
+ declare function formatHL7Date(date: Date): string;
272
+
273
+ /**
274
+ * Thrown when an HL7 v2 message string cannot be parsed.
275
+ *
276
+ * The `line` and `segmentId` properties help pinpoint the exact failure location
277
+ * when debugging multi-segment messages from production systems.
278
+ */
279
+ declare class HL7ParseError extends Error {
280
+ /** 1-based line number in the source string where the error occurred. */
281
+ readonly line: number | undefined;
282
+ /** Segment identifier that was being parsed when the error occurred. */
283
+ readonly segmentId: string | undefined;
284
+ constructor(message: string, options?: {
285
+ line?: number;
286
+ segmentId?: string;
287
+ cause?: unknown;
288
+ });
289
+ }
290
+ /**
291
+ * Thrown when a required segment is not present in the message.
292
+ *
293
+ * @example
294
+ * // Thrown by segment() when the segment is not found
295
+ * const pid = segment(msg, 'PID'); // throws if no PID segment
296
+ */
297
+ declare class SegmentNotFoundError extends Error {
298
+ readonly segmentId: string;
299
+ constructor(segmentId: string);
300
+ }
301
+ /**
302
+ * Thrown when an HL7 message cannot be identified as valid HL7 v2.
303
+ * Usually means the input is empty, truncated, or a completely different format.
304
+ */
305
+ declare class InvalidHL7Error extends Error {
306
+ constructor(reason: string);
307
+ }
308
+
309
+ export { type EncodeOptions, EncodingChars, type GetOptions, HL7Message, HL7ParseError, HL7Segment, InvalidHL7Error, type ParseOptions, SegmentNotFoundError, decodeEscapes, encode, encodeEscapes, encodeSegment, formatHL7Date, formatHL7DateTime, get, getFromSegment, getRepetitions, hasSegment, isHL7, parse, parseHL7DateTime, segment, segments };