Skip to main content
Version: v0.0.3

Function: splitBatch()

Internal

implementation signature; overloads above carry the public JSDoc.

Call Signature

splitBatch(raw): BatchSplitResult

Split a raw HL7 v2 batch / file stream into its individual messages plus the envelope metadata. Demarcates by MSH boundaries inside the optional [FHS] { [BHS] { MSH… } [BTS] } [FTS] frame (HL7 v2 Ch. 2 §2.10.3):

  • handles a file with multiple batches and a batch with multiple messages;
  • a bare single message (no envelope) passes straight through as one entry;
  • a malformed message mid-batch is isolated (returned as a typed failure entry): its siblings are still returned, the tail is never dropped;
  • reconciles BTS-1 (batch message count) and FTS-1 (file batch count) and emits batchCountMismatch on a mismatch: counts only, never PHI;
  • emits batchMissingTrailer when a BHS/FHS header opens a scope no BTS/FTS closes: a warning, never a throw (the caller decides to reject).

The second argument, when given, is forwarded verbatim to parseHL7 for each message (profile, strict, charset, dateFormats, …). Under strict, a message that would warn surfaces as a failure entry (still isolated). splitBatch itself never throws: an empty stream yields an empty result.

Parameters

raw

string | Buffer<ArrayBufferLike>

Returns

BatchSplitResult

Example

import { splitBatch } from "@cosyte/hl7";

const { messages, warnings } = splitBatch(
"FHS|^~\\&|SENDER\r" +
"BHS|^~\\&|SENDER\r" +
"MSH|^~\\&|A|F|B|G|20260101||ADT^A01|1|P|2.5\rPID|||1\r" +
"MSH|^~\\&|A|F|B|G|20260101||ADT^A01|2|P|2.5\rPID|||2\r" +
"BTS|2\rFTS|1\r",
);
console.log(messages.length); // 2
for (const entry of messages) {
if (entry.ok) console.log(entry.message.version); // "2.5"
}
console.log(warnings.length); // 0: declared counts match

Call Signature

splitBatch(raw, profile): BatchSplitResult

Split a raw HL7 v2 batch / file stream into its individual messages plus the envelope metadata. Demarcates by MSH boundaries inside the optional [FHS] { [BHS] { MSH… } [BTS] } [FTS] frame (HL7 v2 Ch. 2 §2.10.3):

  • handles a file with multiple batches and a batch with multiple messages;
  • a bare single message (no envelope) passes straight through as one entry;
  • a malformed message mid-batch is isolated (returned as a typed failure entry): its siblings are still returned, the tail is never dropped;
  • reconciles BTS-1 (batch message count) and FTS-1 (file batch count) and emits batchCountMismatch on a mismatch: counts only, never PHI;
  • emits batchMissingTrailer when a BHS/FHS header opens a scope no BTS/FTS closes: a warning, never a throw (the caller decides to reject).

The second argument, when given, is forwarded verbatim to parseHL7 for each message (profile, strict, charset, dateFormats, …). Under strict, a message that would warn surfaces as a failure entry (still isolated). splitBatch itself never throws: an empty stream yields an empty result.

Parameters

raw

string | Buffer<ArrayBufferLike>

profile

Profile

Returns

BatchSplitResult

Example

import { splitBatch } from "@cosyte/hl7";

const { messages, warnings } = splitBatch(
"FHS|^~\\&|SENDER\r" +
"BHS|^~\\&|SENDER\r" +
"MSH|^~\\&|A|F|B|G|20260101||ADT^A01|1|P|2.5\rPID|||1\r" +
"MSH|^~\\&|A|F|B|G|20260101||ADT^A01|2|P|2.5\rPID|||2\r" +
"BTS|2\rFTS|1\r",
);
console.log(messages.length); // 2
for (const entry of messages) {
if (entry.ok) console.log(entry.message.version); // "2.5"
}
console.log(warnings.length); // 0: declared counts match

Call Signature

splitBatch(raw, options): BatchSplitResult

Split a raw HL7 v2 batch / file stream into its individual messages plus the envelope metadata. Demarcates by MSH boundaries inside the optional [FHS] { [BHS] { MSH… } [BTS] } [FTS] frame (HL7 v2 Ch. 2 §2.10.3):

  • handles a file with multiple batches and a batch with multiple messages;
  • a bare single message (no envelope) passes straight through as one entry;
  • a malformed message mid-batch is isolated (returned as a typed failure entry): its siblings are still returned, the tail is never dropped;
  • reconciles BTS-1 (batch message count) and FTS-1 (file batch count) and emits batchCountMismatch on a mismatch: counts only, never PHI;
  • emits batchMissingTrailer when a BHS/FHS header opens a scope no BTS/FTS closes: a warning, never a throw (the caller decides to reject).

The second argument, when given, is forwarded verbatim to parseHL7 for each message (profile, strict, charset, dateFormats, …). Under strict, a message that would warn surfaces as a failure entry (still isolated). splitBatch itself never throws: an empty stream yields an empty result.

Parameters

raw

string | Buffer<ArrayBufferLike>

options

ParseOptions

Returns

BatchSplitResult

Example

import { splitBatch } from "@cosyte/hl7";

const { messages, warnings } = splitBatch(
"FHS|^~\\&|SENDER\r" +
"BHS|^~\\&|SENDER\r" +
"MSH|^~\\&|A|F|B|G|20260101||ADT^A01|1|P|2.5\rPID|||1\r" +
"MSH|^~\\&|A|F|B|G|20260101||ADT^A01|2|P|2.5\rPID|||2\r" +
"BTS|2\rFTS|1\r",
);
console.log(messages.length); // 2
for (const entry of messages) {
if (entry.ok) console.log(entry.message.version); // "2.5"
}
console.log(warnings.length); // 0: declared counts match