@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,457 @@
1
+ import { a as HL7Segment, E as EncodingChars, b as HL7Field, M as MessageType } from '../schema-qr-scwmL.js';
2
+
3
+ /**
4
+ * Base class for typed HL7 v2 segment accessors.
5
+ *
6
+ * Typed segments wrap an {@link HL7Segment} and provide strongly-typed,
7
+ * documented accessor methods for each field — eliminating the need to
8
+ * remember field numbers or component positions.
9
+ *
10
+ * @example
11
+ * import { PID } from 'hl7v2/segments';
12
+ * import { parse, segment } from 'hl7v2';
13
+ *
14
+ * const msg = parse(rawString);
15
+ * const pid = new PID(segment(msg, 'PID'), msg.encoding);
16
+ *
17
+ * pid.patientName() // { family: 'Doe', given: 'John', middle: 'A' }
18
+ * pid.dateOfBirth() // Date | undefined
19
+ * pid.sex() // 'M' | 'F' | 'O' | 'U' | undefined
20
+ */
21
+ declare abstract class TypedSegment {
22
+ protected readonly seg: HL7Segment;
23
+ protected readonly enc: EncodingChars;
24
+ constructor(seg: HL7Segment, enc: EncodingChars);
25
+ /** The raw underlying {@link HL7Segment}. */
26
+ get raw(): HL7Segment;
27
+ /** Return the raw field structure at the given 1-based field number. */
28
+ protected field(num: number): HL7Field | undefined;
29
+ /**
30
+ * Extract a scalar string value from the segment.
31
+ * All arguments are 1-based. Missing elements return `''`.
32
+ * Escape sequences are decoded automatically.
33
+ */
34
+ protected str(fieldNum: number, component?: number, subComponent?: number, repetition?: number): string;
35
+ /**
36
+ * Extract a string value from the segment without escape decoding.
37
+ * Useful when you need to preserve the raw HL7 encoding.
38
+ */
39
+ protected rawStr(fieldNum: number, component?: number, subComponent?: number, repetition?: number): string;
40
+ /** Extract a Date from an HL7 date/time field. Returns `undefined` if empty. */
41
+ protected date(fieldNum: number, component?: number): Date | undefined;
42
+ /** Extract a numeric value from a field. Returns `undefined` if empty or non-numeric. */
43
+ protected num(fieldNum: number, component?: number): number | undefined;
44
+ /** Return all repetitions of a field as an array of first-component strings. */
45
+ protected repetitions(fieldNum: number, component?: number): string[];
46
+ }
47
+
48
+ /** Processing ID values defined in HL7 v2 table 0103. */
49
+ type ProcessingId = 'P' | 'D' | 'T';
50
+ /**
51
+ * Typed accessor for the MSH (Message Header) segment.
52
+ *
53
+ * MSH is mandatory and always the first segment in any HL7 v2 message.
54
+ * It identifies the message type, version, sender, receiver, and timestamp.
55
+ *
56
+ * @example
57
+ * import { parse, segment } from 'hl7v2';
58
+ * import { MSH } from 'hl7v2/segments';
59
+ *
60
+ * const msg = parse(raw);
61
+ * const msh = new MSH(segment(msg, 'MSH'), msg.encoding);
62
+ *
63
+ * msh.sendingApplication() // 'HOSPITAL_ADT'
64
+ * msh.messageType() // { type: 'ADT', event: 'A01', structure: 'ADT_A01' }
65
+ * msh.messageControlId() // 'MSG000001'
66
+ * msh.version() // '2.5.1'
67
+ */
68
+ declare class MSH extends TypedSegment {
69
+ constructor(seg: HL7Segment, enc: EncodingChars);
70
+ /** MSH.3 — Sending Application */
71
+ sendingApplication(): string;
72
+ /** MSH.4 — Sending Facility */
73
+ sendingFacility(): string;
74
+ /** MSH.5 — Receiving Application */
75
+ receivingApplication(): string;
76
+ /** MSH.6 — Receiving Facility */
77
+ receivingFacility(): string;
78
+ /** MSH.7 — Date/Time of Message */
79
+ dateTimeOfMessage(): Date | undefined;
80
+ /** MSH.9 — Message Type (e.g., `{ type: 'ADT', event: 'A01', structure: 'ADT_A01' }`). */
81
+ messageType(): MessageType;
82
+ /** MSH.10 — Message Control ID (used for ACK correlation). */
83
+ messageControlId(): string;
84
+ /**
85
+ * MSH.11 — Processing ID.
86
+ * `'P'` = Production, `'D'` = Debugging, `'T'` = Training.
87
+ */
88
+ processingId(): ProcessingId | string;
89
+ /** MSH.12 — HL7 version (e.g., `'2.5.1'`). */
90
+ version(): string;
91
+ /** MSH.15 — Accept Acknowledgement Type. */
92
+ acceptAcknowledgementType(): string;
93
+ /** MSH.16 — Application Acknowledgement Type. */
94
+ applicationAcknowledgementType(): string;
95
+ /** MSH.17 — Country Code (ISO 3166). */
96
+ countryCode(): string;
97
+ }
98
+
99
+ /** Administrative sex codes from HL7 v2 table 0001. */
100
+ type AdministrativeSex = 'M' | 'F' | 'O' | 'U' | 'A' | 'N' | 'C';
101
+ /** A structured patient name following HL7 XPN (Extended Person Name). */
102
+ interface PersonName {
103
+ /** Family (last) name. */
104
+ family: string;
105
+ /** Given (first) name. */
106
+ given: string;
107
+ /** Middle name or initial. */
108
+ middle: string;
109
+ /** Name suffix (e.g., `'JR'`, `'SR'`, `'III'`). */
110
+ suffix: string;
111
+ /** Name prefix (e.g., `'DR'`, `'MR'`, `'MS'`). */
112
+ prefix: string;
113
+ /** Degree (e.g., `'MD'`, `'PHD'`). */
114
+ degree: string;
115
+ }
116
+ /** A structured address following HL7 XAD (Extended Address). */
117
+ interface Address {
118
+ streetAddress: string;
119
+ otherDesignation: string;
120
+ city: string;
121
+ state: string;
122
+ postalCode: string;
123
+ country: string;
124
+ addressType: string;
125
+ }
126
+ /** A patient identifier from PID.3 (Patient Identifier List). */
127
+ interface PatientIdentifier {
128
+ /** The identifier value (e.g., MRN, SSN). */
129
+ id: string;
130
+ /** Check digit. */
131
+ checkDigit: string;
132
+ /** Assigning authority (e.g., `'HOSPITAL'`). */
133
+ assigningAuthority: string;
134
+ /** Identifier type code (e.g., `'MR'` = Medical Record, `'PI'` = Patient Internal). */
135
+ identifierTypeCode: string;
136
+ }
137
+ /**
138
+ * Typed accessor for the PID (Patient Identification) segment.
139
+ *
140
+ * PID carries all core patient demographic information and appears in
141
+ * virtually every HL7 v2 message type (ADT, ORU, ORM, SIU, etc.).
142
+ *
143
+ * @example
144
+ * import { parse, segment } from 'hl7v2';
145
+ * import { PID } from 'hl7v2/segments';
146
+ *
147
+ * const msg = parse(raw);
148
+ * const pid = new PID(segment(msg, 'PID'), msg.encoding);
149
+ *
150
+ * pid.patientName() // { family: 'Doe', given: 'John', middle: 'A', ... }
151
+ * pid.dateOfBirth() // Date | undefined
152
+ * pid.sex() // 'M' | 'F' | ...
153
+ * pid.patientIdentifiers() // PatientIdentifier[]
154
+ */
155
+ declare class PID extends TypedSegment {
156
+ constructor(seg: HL7Segment, enc: EncodingChars);
157
+ /** PID.1 — Set ID (1-based sequence number within the message). */
158
+ setId(): number | undefined;
159
+ /**
160
+ * PID.3 — Patient Identifier List.
161
+ *
162
+ * Returns all patient identifiers (MRN, SSN, national ID, etc.).
163
+ * Each identifier has its own type code and assigning authority.
164
+ *
165
+ * @example
166
+ * pid.patientIdentifiers()
167
+ * // [{ id: 'MRN123', assigningAuthority: 'HOSPITAL', identifierTypeCode: 'MR' }]
168
+ */
169
+ patientIdentifiers(): PatientIdentifier[];
170
+ /**
171
+ * PID.3 — Primary patient identifier (first in the list).
172
+ * Returns the raw identifier string (e.g., `'MRN123'`).
173
+ */
174
+ patientId(): string;
175
+ /**
176
+ * PID.5 — Patient Name (first/primary name).
177
+ *
178
+ * Patient names follow the XPN (Extended Person Name) data type.
179
+ * Components: family ^ given ^ middle ^ suffix ^ prefix ^ degree
180
+ */
181
+ patientName(): PersonName;
182
+ /**
183
+ * PID.5 — All patient names (legal, alias, display, etc.).
184
+ * Most messages contain a single name; some include aliases or prior names.
185
+ */
186
+ allPatientNames(): PersonName[];
187
+ /**
188
+ * PID.7 — Date/Time of Birth.
189
+ * Returns `undefined` if absent or unparseable.
190
+ */
191
+ dateOfBirth(): Date | undefined;
192
+ /**
193
+ * PID.8 — Administrative Sex (HL7 table 0001).
194
+ * `'M'` Male · `'F'` Female · `'O'` Other · `'U'` Unknown
195
+ * `'A'` Ambiguous · `'N'` Not applicable · `'C'` Complex
196
+ */
197
+ sex(): AdministrativeSex | string;
198
+ /**
199
+ * PID.10 — Race (HL7 table 0005, CWE data type).
200
+ * Returns the race code (first component).
201
+ */
202
+ race(): string;
203
+ /**
204
+ * PID.11 — Patient Address (first address).
205
+ * Addresses follow the XAD (Extended Address) data type.
206
+ */
207
+ address(): Address;
208
+ /** PID.13 — Phone Number - Home (first number, XTN data type). */
209
+ homePhone(): string;
210
+ /** PID.14 — Phone Number - Business (first number, XTN data type). */
211
+ workPhone(): string;
212
+ /**
213
+ * PID.15 — Primary Language (CE data type).
214
+ * Returns the language code (e.g., `'ENG'`, `'SPA'`).
215
+ */
216
+ primaryLanguage(): string;
217
+ /** PID.16 — Marital Status (HL7 table 0002). */
218
+ maritalStatus(): string;
219
+ /** PID.17 — Religion (HL7 table 0006). */
220
+ religion(): string;
221
+ /**
222
+ * PID.18 — Patient Account Number.
223
+ * The billing/visit account number (distinct from the medical record number).
224
+ */
225
+ accountNumber(): string;
226
+ /** PID.19 — SSN Number - Patient (US Social Security Number). */
227
+ ssn(): string;
228
+ /** PID.22 — Ethnic Group (HL7 table 0189, CWE data type). */
229
+ ethnicGroup(): string;
230
+ /** PID.29 — Patient Death Date and Time. `undefined` if patient is not deceased. */
231
+ deathDateTime(): Date | undefined;
232
+ /**
233
+ * PID.30 — Patient Death Indicator.
234
+ * `'Y'` = deceased, `'N'` = not deceased.
235
+ */
236
+ deathIndicator(): 'Y' | 'N' | string;
237
+ }
238
+
239
+ /** OBX.2 — Value type codes from HL7 v2 table 0125. */
240
+ type ObservationValueType = 'NM' | 'ST' | 'TX' | 'FT' | 'CWE' | 'CE' | 'DT' | 'TM' | 'TS' | 'DTM' | 'SN' | 'RP' | 'ED' | string;
241
+ /** OBX.11 — Observation Result Status codes from HL7 v2 table 0085. */
242
+ type ResultStatus = 'F' | 'P' | 'C' | 'X' | 'I' | 'R' | 'S' | 'D' | 'N' | 'W' | string;
243
+ /** Abnormal flags from HL7 v2 table 0078. */
244
+ type AbnormalFlag = 'H' | 'HH' | 'L' | 'LL' | 'A' | 'AA' | 'N' | 'IND' | 'NEG' | 'POS' | string;
245
+ /** A coded observation identifier (LOINC, SNOMED, local). */
246
+ interface ObservationIdentifier {
247
+ /** Identifier code (e.g., LOINC code `'718-7'`). */
248
+ code: string;
249
+ /** Human-readable description (e.g., `'Hemoglobin'`). */
250
+ description: string;
251
+ /** Coding system (e.g., `'LN'` for LOINC, `'SCT'` for SNOMED CT). */
252
+ codingSystem: string;
253
+ }
254
+ /**
255
+ * Typed accessor for the OBX (Observation/Result) segment.
256
+ *
257
+ * OBX carries a single clinical observation — a lab result, vital sign,
258
+ * pathology finding, or any structured clinical measurement. A message
259
+ * typically contains multiple OBX segments, one per result.
260
+ *
261
+ * @example
262
+ * import { parse, segments } from 'hl7v2';
263
+ * import { OBX } from 'hl7v2/segments';
264
+ *
265
+ * const msg = parse(raw);
266
+ * const obxs = segments(msg, 'OBX').map(s => new OBX(s, msg.encoding));
267
+ *
268
+ * for (const obx of obxs) {
269
+ * console.log(obx.observationIdentifier().description); // 'Hemoglobin'
270
+ * console.log(obx.numericValue()); // 13.5
271
+ * console.log(obx.units()); // 'g/dL'
272
+ * console.log(obx.referenceRange()); // '13.5-17.5'
273
+ * console.log(obx.abnormalFlags()); // ['H']
274
+ * console.log(obx.resultStatus()); // 'F'
275
+ * }
276
+ */
277
+ declare class OBX extends TypedSegment {
278
+ constructor(seg: HL7Segment, enc: EncodingChars);
279
+ /** OBX.1 — Set ID (sequence number within the message, 1-based). */
280
+ setId(): number | undefined;
281
+ /**
282
+ * OBX.2 — Value Type.
283
+ * Indicates the data type of the observation value in OBX.5.
284
+ * Common values: `'NM'` (numeric), `'ST'` (string), `'CWE'` (coded).
285
+ */
286
+ valueType(): ObservationValueType;
287
+ /**
288
+ * OBX.3 — Observation Identifier.
289
+ * Typically a LOINC code identifying what was measured.
290
+ *
291
+ * @example
292
+ * obx.observationIdentifier()
293
+ * // { code: '718-7', description: 'Hemoglobin [Mass/volume] in Blood', codingSystem: 'LN' }
294
+ */
295
+ observationIdentifier(): ObservationIdentifier;
296
+ /** OBX.4 — Observation Sub-ID (groups related OBX segments). */
297
+ observationSubId(): string;
298
+ /**
299
+ * OBX.5 — Observation Value (raw string).
300
+ *
301
+ * The interpretation depends on OBX.2 (value type).
302
+ * Use {@link numericValue} for `NM`, {@link codedValue} for `CWE`/`CE`.
303
+ */
304
+ observationValue(): string;
305
+ /**
306
+ * OBX.5 — Observation value interpreted as a number.
307
+ * Returns `undefined` if the value type is not `NM` or the value is absent.
308
+ *
309
+ * For structured numerics (SN type like `'>10.5'`), use {@link observationValue}.
310
+ */
311
+ numericValue(): number | undefined;
312
+ /**
313
+ * OBX.5 — Observation value as a coded element (CWE/CE value type).
314
+ * Returns `{ code, description, codingSystem }`.
315
+ */
316
+ codedValue(): ObservationIdentifier;
317
+ /**
318
+ * OBX.6 — Units (CWE data type).
319
+ * Returns the unit code (e.g., `'g/dL'`, `'10*3/uL'`, `'mmHg'`).
320
+ */
321
+ units(): string;
322
+ /**
323
+ * OBX.7 — References Range.
324
+ * The normal range as a string (e.g., `'3.5-5.0'`, `'<200'`).
325
+ */
326
+ referenceRange(): string;
327
+ /**
328
+ * OBX.8 — Interpretation Codes (formerly Abnormal Flags).
329
+ * Returns all flags for this result (a field can have multiple repetitions).
330
+ *
331
+ * Common values: `'H'` high, `'L'` low, `'HH'` critical high, `'LL'` critical low,
332
+ * `'N'` normal, `'A'` abnormal, `'POS'` positive, `'NEG'` negative.
333
+ */
334
+ abnormalFlags(): AbnormalFlag[];
335
+ /**
336
+ * OBX.8 — Primary interpretation code (first flag).
337
+ * Returns `''` if the result has no flags (i.e., it is normal / in-range).
338
+ */
339
+ primaryAbnormalFlag(): AbnormalFlag;
340
+ /** OBX.11 — Observation Result Status (HL7 table 0085). */
341
+ resultStatus(): ResultStatus;
342
+ /** OBX.14 — Date/Time of Observation. */
343
+ observationDateTime(): Date | undefined;
344
+ /** OBX.15 — Producer's ID (the lab or device that produced the result). */
345
+ producerId(): string;
346
+ /** OBX.16 — Responsible Observer (clinician who verified the result). */
347
+ responsibleObserver(): string;
348
+ /** OBX.19 — Date/Time of Analysis. */
349
+ analysisDateTime(): Date | undefined;
350
+ /**
351
+ * Convenience: return `true` if this result has a critical abnormal flag
352
+ * (`'HH'` or `'LL'`).
353
+ */
354
+ isCritical(): boolean;
355
+ /**
356
+ * Convenience: return `true` if the result is finalised (`resultStatus() === 'F'`).
357
+ */
358
+ isFinal(): boolean;
359
+ }
360
+
361
+ /** Patient class codes from HL7 v2 table 0004. */
362
+ type PatientClass = 'E' | 'I' | 'O' | 'P' | 'R' | 'B' | 'C' | 'N' | 'U' | string;
363
+ /** A structured provider/clinician name and identifier. */
364
+ interface Provider {
365
+ /** Provider identifier (e.g., NPI number, employee ID). */
366
+ id: string;
367
+ /** Family (last) name. */
368
+ family: string;
369
+ /** Given (first) name. */
370
+ given: string;
371
+ /** Middle name or initial. */
372
+ middle: string;
373
+ /** Credential suffix (e.g., `'MD'`, `'DO'`, `'NP'`). */
374
+ credential: string;
375
+ }
376
+ /** A patient location — room, bed, facility. */
377
+ interface PatientLocation {
378
+ pointOfCare: string;
379
+ room: string;
380
+ bed: string;
381
+ facility: string;
382
+ buildingCode: string;
383
+ floor: string;
384
+ }
385
+ /**
386
+ * Typed accessor for the PV1 (Patient Visit) segment.
387
+ *
388
+ * PV1 describes the context of a patient's current or historical visit:
389
+ * the patient class (inpatient/outpatient), assigned location, attending
390
+ * and referring physicians, and admission/discharge timestamps.
391
+ *
392
+ * @example
393
+ * import { parse, segment } from 'hl7v2';
394
+ * import { PV1 } from 'hl7v2/segments';
395
+ *
396
+ * const msg = parse(raw);
397
+ * const pv1 = new PV1(segment(msg, 'PV1'), msg.encoding);
398
+ *
399
+ * pv1.patientClass() // 'I' (Inpatient)
400
+ * pv1.assignedLocation() // { pointOfCare: 'CARDIOLOGY', room: '4A', bed: '101', ... }
401
+ * pv1.attendingDoctor() // { id: '1234567', family: 'Smith', given: 'Richard', ... }
402
+ * pv1.admitDateTime() // Date | undefined
403
+ */
404
+ declare class PV1 extends TypedSegment {
405
+ constructor(seg: HL7Segment, enc: EncodingChars);
406
+ /** PV1.1 — Set ID. */
407
+ setId(): number | undefined;
408
+ /**
409
+ * PV1.2 — Patient Class (HL7 table 0004).
410
+ * `'I'` Inpatient · `'O'` Outpatient · `'E'` Emergency · `'P'` Preadmit
411
+ */
412
+ patientClass(): PatientClass;
413
+ /**
414
+ * PV1.3 — Assigned Patient Location.
415
+ * The bed the patient is currently assigned to.
416
+ */
417
+ assignedLocation(): PatientLocation;
418
+ /** PV1.4 — Admission Type (e.g., `'A'` = accident, `'E'` = emergency). */
419
+ admissionType(): string;
420
+ /**
421
+ * PV1.7 — Attending Doctor.
422
+ * The physician primarily responsible for the patient's care.
423
+ */
424
+ attendingDoctor(): Provider;
425
+ /**
426
+ * PV1.8 — Referring Doctor.
427
+ * The physician who referred the patient.
428
+ */
429
+ referringDoctor(): Provider;
430
+ /**
431
+ * PV1.9 — Consulting Doctor.
432
+ * Returns all consulting doctors (field can repeat).
433
+ */
434
+ consultingDoctors(): Provider[];
435
+ /** PV1.10 — Hospital Service (e.g., `'MED'`, `'SUR'`, `'CAR'`). */
436
+ hospitalService(): string;
437
+ /** PV1.14 — Admit Source (HL7 table 0023). */
438
+ admitSource(): string;
439
+ /** PV1.18 — Patient Type. */
440
+ patientType(): string;
441
+ /** PV1.19 — Visit Number (account number for this specific visit). */
442
+ visitNumber(): string;
443
+ /** PV1.36 — Discharge Disposition (HL7 table 0112). */
444
+ dischargeDisposition(): string;
445
+ /**
446
+ * PV1.44 — Admit Date/Time.
447
+ * When the patient was admitted to this care setting.
448
+ */
449
+ admitDateTime(): Date | undefined;
450
+ /**
451
+ * PV1.45 — Discharge Date/Time.
452
+ * `undefined` if the patient has not yet been discharged.
453
+ */
454
+ dischargeDateTime(): Date | undefined;
455
+ }
456
+
457
+ export { type AbnormalFlag, type Address, type AdministrativeSex, MSH, OBX, type ObservationIdentifier, type ObservationValueType, PID, PV1, type PatientClass, type PatientIdentifier, type PatientLocation, type PersonName, type ProcessingId, type Provider, type ResultStatus, TypedSegment };