Skip to main content
Version: v0.0.2

Function: decodeSegment()

decodeSegment(raw, delimiters, emit, position): X12Segment

Decode a raw segment string into an X12Segment. Splits on the detected element separator (honouring the ?-release-character escape), preserves the verbatim raw text, and surfaces dangling-release warnings via the supplied emitter with positional context tied to the segment.

Never throws - malformed input produces a best-effort segment and any issues surface as Tier-2 warnings via emit. An empty input string decodes to an empty segment (id: "", elements: [""]); callers that care can guard.

Parameters

raw

string

delimiters

Delimiters

emit

(w) => void

position

X12Position

Returns

X12Segment

Example

import { decodeSegment } from "@cosyte/x12";
const d = { element: "*", repetition: "^", component: ":", segment: "~" };
const seg = decodeSegment("NM1*IL*1*DOE*JANE", d, () => {}, { segmentIndex: 5 });
seg.id; // "NM1"
seg.elements[3]; // "DOE"