Function: encodeComponent()
encodeComponent(
leaf,d,recordIndex?):string
Escape-encode one component leaf for spec-clean emit - the inverse of
decodeEscapes. The escape character itself is encoded first (& → &E&)
so a later delimiter substitution can never double-encode the & it just
introduced; then the field / component / repeat delimiters map to their
mnemonics.
A CR/LF in the leaf has no escape mnemonic and would break framing, so it
is rejected with an AstmSerializeError rather than emitted raw, as is a
delimiter set the result could not be read back with.
Parameters
leaf
string
One already-decoded component string.
d
The delimiters to emit against (canonical for spec-clean output).
recordIndex?
number
The enclosing record's index, for error context.
Returns
string
The escaped component text.
Throws
AstmSerializeError for a CR/LF in the leaf
(ASTM_EMIT_UNENCODABLE_VALUE), or for a delimiter set failing one of the
three conditions readback requires (ASTM_EMIT_INVALID_DELIMITERS) - which
are necessary, not sufficient, so a set can pass them and still not reverse.
Example
import { encodeComponent, CANONICAL_DELIMITERS } from "@cosyte/astm";
encodeComponent("1^40", CANONICAL_DELIMITERS); // "1&S&40"