Function: reescape()
reescape(
input,enc):string
Re-escape reserved characters back into their \X\ forms so the serializer
can emit spec-clean HL7. This is the inverse of unescape for every
delimiter-bearing character; round-trip cleanliness
(unescape(reescape(x, enc), enc, emit, pos) === x) is a documented
property covered by tests.
The characters re-escaped:
enc.escape → \E
enc.field → \F
enc.component → \S
enc.subcomponent → \T
enc.repetition → \R
enc.truncation → \P\ (only when MSH-2 declared one, v2.7+)
"\n" (LF) → .br
"\r" (CR) → \X0D\ (a decoded CR is the HL7 segment separator;
emitting it raw would corrupt wire framing, so
it re-encodes to its hex escape: see below)
Lossy by construction for the non-delimiter escape families. reescape
only knows about the reserved characters above: it cannot reconstruct a
recognize-and-preserve escape (\H\, formatting, charset, \Z..) or the
original bytes of a hex escape (\X41\ decoded to A; casing of \X0d),
because those decode to ordinary characters that carry no "I was an escape"
marker. Byte-verbatim emit for those families is the serializer's job via
the RawComponent.rawSubcomponents overlay (see escapeFidelityRaw);
reescape is the fallback for content that has no overlay (constructed
values, Field-level re-escapes).
Iteration uses for..of, which walks Unicode code points (not UTF-16 code
units), so user-supplied content containing non-BMP characters round-trips
correctly without special surrogate-pair handling.
Parameters
input
string
enc
Returns
string
Example
import { reescape, DEFAULT_ENCODING_CHARACTERS } from "@cosyte/hl7";
reescape("Smith|John", DEFAULT_ENCODING_CHARACTERS); // "Smith\\F\\John"
reescape("line1\nline2", DEFAULT_ENCODING_CHARACTERS); // "line1\\.br\\line2"