@cosyte/x12 0.0.14 → 0.0.16

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.
package/dist/index.mjs CHANGED
@@ -45,6 +45,12 @@ var DELIMITER_POSITIONS = {
45
45
  component: 104,
46
46
  segment: 105
47
47
  };
48
+ function isVisibleDelimiterChar(c) {
49
+ const code = c.charCodeAt(0);
50
+ if (code < 32 || code === 127) return false;
51
+ if (/\s/u.test(c)) return false;
52
+ return true;
53
+ }
48
54
  function detectDelimiters(raw) {
49
55
  const fatalPosition = { segmentIndex: 0, interchangeIndex: 0 };
50
56
  const snip = snippet(raw);
@@ -68,19 +74,13 @@ function detectDelimiters(raw) {
68
74
  const repetition = raw.charAt(DELIMITER_POSITIONS.repetition);
69
75
  const component = raw.charAt(DELIMITER_POSITIONS.component);
70
76
  const segment = raw.charAt(DELIMITER_POSITIONS.segment);
71
- const isValidDelimiterChar = (c) => {
72
- const code = c.charCodeAt(0);
73
- if (code < 32 || code === 127) return false;
74
- if (/\s/u.test(c)) return false;
75
- return true;
76
- };
77
77
  for (const [name, ch] of [
78
78
  ["element", element],
79
79
  ["repetition", repetition],
80
80
  ["component", component],
81
81
  ["segment", segment]
82
82
  ]) {
83
- if (!isValidDelimiterChar(ch)) {
83
+ if (!isVisibleDelimiterChar(ch)) {
84
84
  throw new X12ParseError(
85
85
  FATAL_CODES.X12_INVALID_DELIMITERS,
86
86
  `Delimiter "${name}" at ISA byte ${String(DELIMITER_POSITIONS[name] + 1)} is whitespace, control, or empty.`,
@@ -222,7 +222,7 @@ var WARNING_MESSAGES = {
222
222
  X12_MISSING_REQUIRED_LOOP_2000B: 'Missing required loop "2000B" (Subscriber HL): no Subscriber HL precedes the CLM.',
223
223
  X12_MISSING_REQUIRED_LOOP_2010BA: 'Missing required loop "2010BA" (Subscriber Name): no Subscriber Name follows the Subscriber HL.',
224
224
  X12_MISSING_REQUIRED_LOOP_2010BB: 'Missing required loop "2010BB" (Payer Name): no Payer Name follows the Subscriber HL.',
225
- X12_837_UNKNOWN_VARIANT: "837 variant could not be resolved: ST-03's implementation convention reference is absent, empty, or not an 837 Technical Report Type 3 identifier this reader recognises, and no SVx service-line segment was present to fall back on. The set of references it recognises covers the professional, institutional and dental 837 guides HIPAA adopts at 45 CFR 162.1102 together with their published errata; it is NOT claimed to be exhaustive, and this message deliberately does not enumerate it, because a list here goes stale the moment a guide is added. A reference outside that set is not asserted to be invalid - all this code reports is that this reader could not turn it into a variant and had nothing to fall back on. The verbatim reference is preserved on the model; read it there rather than inferring what the sender meant.",
225
+ X12_837_UNKNOWN_VARIANT: "837 variant could not be resolved: ST-03's implementation convention reference is absent, empty, or not an 837 Technical Report Type 3 identifier this reader recognises, and no SVx service-line segment was present to fall back on. The set of references it recognises covers the professional, institutional and dental 837 guides HIPAA adopts at 45 CFR 162.1102 together with their published errata; it is NOT claimed to be exhaustive, and this message deliberately does not enumerate it, because a list here goes stale the moment a guide is added. A reference outside that set is not asserted to be invalid - all this code reports is that this reader could not turn it into a variant and had nothing to fall back on.",
226
226
  X12_837_AMBIGUOUS_VARIANT: "837 variant resolved from a service segment while the transaction body carries service segments of more than one variant: no caller `type` option was supplied and ST-03's implementation convention reference is absent, empty, or not an 837 Technical Report Type 3 identifier this reader recognises, so this reader fell back to the FIRST SV1 / SV2 / SV3 in the body, and a later one names a different variant. `submission.variant` is therefore a guess between contradictory evidence, and every claim and service line in the submission was read against it. Which service segment is the stray one is NOT decided here and is not derivable from the TR3s: this reader cannot tell a stray service segment from a conformant one, and the fallback takes the first regardless of whether any Loop 2400 was open at it, so an orphan segment decides the variant like any other. Read the bound literally, as a property of the RESOLUTION rather than of the document: this reports the fallback's own ambiguity, so a caller-supplied `type`, or an ST-03 this reader does turn into a variant, means no guess was made and this code is NOT raised however mixed the body is. Whatever else this reader raised on a document that reaches this code, it still raises, at the same position, and this one is added beside them. It is NOT a list of what else you will see, and it does not promise that any particular loss on such a document is reported at all. Re-read with the `type` option to decode the document against a variant you trust. The verbatim segments are preserved on the transaction set; read them there before acting on the submission's type.",
227
227
  X12_UNKNOWN_CLAIM_STATUS_CATEGORY: "Unknown claim status category (CSCC): the STC composite's first component is outside the bundled snapshot. The verbatim code is preserved on the status; only its description is unavailable.",
228
228
  X12_UNKNOWN_CLAIM_STATUS: "Unknown claim status code (CSC): the STC composite's second component is outside the bundled snapshot. The verbatim code is preserved on the status; only its description is unavailable.",
@@ -866,8 +866,11 @@ function boundSegmentId(first) {
866
866
  return SEGMENT_ID_SHAPE_RE.test(first) ? first : NON_SPEC_SEGMENT_ID;
867
867
  }
868
868
  function decodeSegment(raw, delimiters, emit, position) {
869
- const elements = Object.freeze(splitWithRelease(raw, delimiters.element));
870
- if (raw.length > 0 && raw.endsWith(RELEASE_CHAR)) {
869
+ const degenerate = delimiters.element === RELEASE_CHAR;
870
+ const elements = Object.freeze(
871
+ degenerate ? raw.split(delimiters.element) : splitWithRelease(raw, delimiters.element)
872
+ );
873
+ if (!degenerate && raw.length > 0 && raw.endsWith(RELEASE_CHAR)) {
871
874
  let trailingRun = 0;
872
875
  for (let j = raw.length - 1; j >= 0 && raw.charAt(j) === RELEASE_CHAR; j -= 1) trailingRun += 1;
873
876
  if (trailingRun % 2 === 1) {
@@ -1095,7 +1098,10 @@ function splitSegments(text, start, delimiters) {
1095
1098
  return { segments, endIndex: lastEnd };
1096
1099
  }
1097
1100
  function splitElements(segment, delimiters) {
1098
- return Object.freeze(segment.split(delimiters.element));
1101
+ if (delimiters.element === RELEASE_CHAR) {
1102
+ return Object.freeze(segment.split(delimiters.element));
1103
+ }
1104
+ return Object.freeze(splitWithRelease(segment, delimiters.element));
1099
1105
  }
1100
1106
  function el(elements, index) {
1101
1107
  return elements[index] ?? "";
@@ -2701,10 +2707,70 @@ function requireCallerString(value, at, refuse) {
2701
2707
  `${at}: every element value must be a string, but received ${describeCallerValue(value)}. Values are never coerced - a JavaScript number cannot carry a leading-zero identifier, so converting here could emit a different value than the caller sent. Convert at the call site.`
2702
2708
  );
2703
2709
  }
2710
+ var DELIMITER_ROLES = [
2711
+ ["element", "element separator"],
2712
+ ["repetition", "repetition separator"],
2713
+ ["component", "component separator"],
2714
+ ["segment", "segment terminator"]
2715
+ ];
2716
+ function requireEscapableDelimiters(delimiters, at, refuse) {
2717
+ const degenerate = DELIMITER_ROLES.filter(([role]) => delimiters[role] === RELEASE_CHAR).map(
2718
+ ([, name]) => name
2719
+ );
2720
+ if (degenerate.length === 0) return;
2721
+ const roles = degenerate.length === 1 ? `the ${degenerate[0] ?? ""}` : `the ${degenerate.slice(0, -1).join(", the ")} and the ${degenerate[degenerate.length - 1] ?? ""}`;
2722
+ refuse(
2723
+ `${at}: "${RELEASE_CHAR}" is the X12 release character and cannot also be ${roles}. An escape is written by prefixing "${RELEASE_CHAR}" to the byte it protects, so on this set the protection is emitted as structure; and a composite or a repetition joined with "${RELEASE_CHAR}" is emitted as an escape. Neither reads back, and no element value avoids it. Declare a delimiter set in which no role is "${RELEASE_CHAR}".`
2724
+ );
2725
+ }
2726
+ function requireWellShapedDelimiters(delimiters, at, refuse) {
2727
+ const REMEDY = `A delimiter occupies ONE fixed position of the ISA (ISA-11, ISA-16 and the position after it), so this set cannot be transmitted and this library's own parser would reject a document declaring it. Declare four distinct, visible, single-character delimiters.`;
2728
+ for (const [role, name] of DELIMITER_ROLES) {
2729
+ const value = delimiters[role];
2730
+ if (typeof value !== "string") {
2731
+ refuse(
2732
+ `${at}: the ${name} must be a string, but received ${describeCallerValue(value)}. Values are never coerced - a coerced delimiter frames the document while the release escape still compares against what you passed, so no element value is protected from it. ${REMEDY}`
2733
+ );
2734
+ }
2735
+ if (value.length !== 1) {
2736
+ refuse(
2737
+ `${at}: the ${name} must be exactly one character, but the declared value is ${value.length === 0 ? "empty" : `${String(value.length)} characters long`}. ${REMEDY}`
2738
+ );
2739
+ }
2740
+ if (!isVisibleDelimiterChar(value)) {
2741
+ refuse(
2742
+ `${at}: the ${name} must be a visible character, but the declared value is whitespace or a control character. ${REMEDY}`
2743
+ );
2744
+ }
2745
+ }
2746
+ for (const [i, [role, name]] of DELIMITER_ROLES.entries()) {
2747
+ const earlier = DELIMITER_ROLES.slice(0, i).find(([r]) => delimiters[r] === delimiters[role]);
2748
+ if (earlier !== void 0) {
2749
+ refuse(
2750
+ `${at}: the ${name} and the ${earlier[1]} are the same character. A reader cannot tell which role a character is playing. ${REMEDY}`
2751
+ );
2752
+ }
2753
+ }
2754
+ }
2704
2755
  function makeCallerEscaper(delimiters, at, refuse) {
2756
+ requireEscapableDelimiters(delimiters, at, refuse);
2757
+ requireWellShapedDelimiters(delimiters, at, refuse);
2705
2758
  return (value) => escapeRelease(requireCallerString(value, at, refuse), delimiters);
2706
2759
  }
2707
2760
 
2761
+ // src/builder/caller-control-number.ts
2762
+ function requireControlNumber(value, slot, field, at, refuse) {
2763
+ if (typeof value !== "string") {
2764
+ refuse(
2765
+ `${at}: ${field} must be a string, but received ${describeCallerValue(value)}. ${slot} is a required control number and this builder never coerces one, so nothing is emitted. Convert ${field} at the call site.`
2766
+ );
2767
+ }
2768
+ if (value !== "") return value;
2769
+ refuse(
2770
+ `${at}: ${field} is empty. ${slot} is a required control number and this builder never invents one, so nothing is emitted. Supply ${field}.`
2771
+ );
2772
+ }
2773
+
2708
2774
  // src/transactions/remit/build-835.ts
2709
2775
  function refuseSpec(message) {
2710
2776
  throw new Remit835BuildError(REMIT_835_BUILD_ERROR_CODES.X12_835_BUILD_INVALID_SPEC, message);
@@ -2749,6 +2815,27 @@ function build835(spec) {
2749
2815
  const senderQualifier = envelope.senderQualifier ?? "ZZ";
2750
2816
  const receiverQualifier = envelope.receiverQualifier ?? "ZZ";
2751
2817
  const usageIndicator = envelope.usageIndicator ?? "P";
2818
+ requireControlNumber(
2819
+ envelope.interchangeControlNumber,
2820
+ "ISA-13 / IEA-02",
2821
+ "interchangeControlNumber",
2822
+ "build835",
2823
+ refuseSpec
2824
+ );
2825
+ requireControlNumber(
2826
+ envelope.groupControlNumber,
2827
+ "GS-06 / GE-02",
2828
+ "groupControlNumber",
2829
+ "build835",
2830
+ refuseSpec
2831
+ );
2832
+ requireControlNumber(
2833
+ envelope.transactionSetControlNumber,
2834
+ "ST-02 / SE-02",
2835
+ "transactionSetControlNumber",
2836
+ "build835",
2837
+ refuseSpec
2838
+ );
2752
2839
  const interchangeControlNumber = padControl(envelope.interchangeControlNumber, 9);
2753
2840
  const isa = [
2754
2841
  "ISA",
@@ -3326,6 +3413,14 @@ function wireLookup(entries) {
3326
3413
  return Object.freeze(table);
3327
3414
  }
3328
3415
 
3416
+ // src/transactions/shared/st03.ts
3417
+ var noopWarningSink = (_w) => {
3418
+ };
3419
+ function decodeSt03(raw, delimiters, position) {
3420
+ if (raw === void 0) return void 0;
3421
+ return unescapeRelease(raw, delimiters, noopWarningSink, position);
3422
+ }
3423
+
3329
3424
  // src/transactions/claim/address-segments.ts
3330
3425
  function emitsStreetLines(address) {
3331
3426
  return address.lines.length > 0;
@@ -3384,9 +3479,14 @@ function get837Claims(delimiters, tx, opts) {
3384
3479
  if (tx.st.elements[1] !== "837") return void 0;
3385
3480
  const warnings = [];
3386
3481
  const body = tx.se === void 0 ? tx.segments.slice(1) : tx.segments.slice(1, -1);
3387
- const implementationConventionReference = tx.st.elements[3];
3482
+ const implementationConventionReferenceRaw = tx.st.elements[3];
3483
+ const implementationConventionReference = decodeSt03(
3484
+ implementationConventionReferenceRaw,
3485
+ delimiters,
3486
+ { segmentIndex: 0, transactionIndex: 0 }
3487
+ );
3388
3488
  const explicitType = opts?.type;
3389
- const variantFromIcr = implementationConventionReference !== void 0 && implementationConventionReference !== "" ? VARIANT_BY_ICR[implementationConventionReference] : void 0;
3489
+ const variantFromIcr = implementationConventionReferenceRaw !== void 0 && implementationConventionReferenceRaw !== "" ? VARIANT_BY_ICR[implementationConventionReferenceRaw] : void 0;
3390
3490
  let variantFromSegment;
3391
3491
  let serviceSegmentVariantsConflict = false;
3392
3492
  if (explicitType === void 0 && variantFromIcr === void 0) {
@@ -4567,6 +4667,29 @@ var VERSION_BY_VARIANT = {
4567
4667
  I: "005010X223A3",
4568
4668
  D: "005010X224A2"
4569
4669
  };
4670
+ function resolveVersionRelease(variant, reference, esc) {
4671
+ if (reference === void 0) return VERSION_BY_VARIANT[variant];
4672
+ if (reference === "") {
4673
+ throw new Claim837BuildError(
4674
+ CLAIM_837_BUILD_ERROR_CODES.X12_837_BUILD_INVALID_SPEC,
4675
+ "build837: envelope.implementationConventionReference is empty. ST-03 and GS-08 are required elements and a trailing empty element is not emitted, so an empty reference would remove them rather than send them empty. Omit the field to take the builder's default."
4676
+ );
4677
+ }
4678
+ if (esc(reference) !== reference) {
4679
+ throw new Claim837BuildError(
4680
+ CLAIM_837_BUILD_ERROR_CODES.X12_837_BUILD_INVALID_SPEC,
4681
+ "build837: envelope.implementationConventionReference carries an active delimiter or the release character. Escaping does not make it safe here: the ST and GS segments are read by a splitter that is not release-aware, so a released delimiter still splits the segment and the declaration silently becomes two elements. State a reference that carries neither, or choose different delimiters."
4682
+ );
4683
+ }
4684
+ const named = Object.hasOwn(VARIANT_BY_ICR, reference) ? VARIANT_BY_ICR[reference] : void 0;
4685
+ if (named !== void 0 && named !== variant) {
4686
+ throw new Claim837BuildError(
4687
+ CLAIM_837_BUILD_ERROR_CODES.X12_837_BUILD_INVALID_SPEC,
4688
+ `build837: envelope.implementationConventionReference names an implementation guide this library reads as a DIFFERENT 837 variant, but this is build837${variant}. The emitted file would declare one variant and carry another's service segments, and this library's own reader would then decode none of its service lines. State a reference for the ${variant} 837, or build with the entry point that matches the reference.`
4689
+ );
4690
+ }
4691
+ return reference;
4692
+ }
4570
4693
  var X12_837_FUNCTIONAL_ID = "HC";
4571
4694
  var X12_AGENCY_CODE2 = "X";
4572
4695
  var HL_LEVEL = { BILLING: "20", SUBSCRIBER: "22", PATIENT: "23" };
@@ -4581,7 +4704,6 @@ function build837D(spec) {
4581
4704
  }
4582
4705
  function buildClaim837(variant, spec) {
4583
4706
  const { envelope } = spec;
4584
- const versionRelease = VERSION_BY_VARIANT[variant];
4585
4707
  enforceStructuralSpec2(variant, spec);
4586
4708
  const elementSeparator = envelope.elementSeparator ?? "*";
4587
4709
  const repetitionSeparator = envelope.repetitionSeparator ?? "^";
@@ -4607,9 +4729,35 @@ function buildClaim837(variant, spec) {
4607
4729
  return escaped.slice(0, end).join(componentSeparator);
4608
4730
  };
4609
4731
  const ctx = { seg, esc, comp };
4732
+ const versionRelease = resolveVersionRelease(
4733
+ variant,
4734
+ envelope.implementationConventionReference,
4735
+ esc
4736
+ );
4610
4737
  const senderQualifier = envelope.senderQualifier ?? "ZZ";
4611
4738
  const receiverQualifier = envelope.receiverQualifier ?? "ZZ";
4612
4739
  const usageIndicator = envelope.usageIndicator ?? "P";
4740
+ requireControlNumber(
4741
+ envelope.interchangeControlNumber,
4742
+ "ISA-13 / IEA-02",
4743
+ "interchangeControlNumber",
4744
+ "build837",
4745
+ refuseSpec2
4746
+ );
4747
+ requireControlNumber(
4748
+ envelope.groupControlNumber,
4749
+ "GS-06 / GE-02",
4750
+ "groupControlNumber",
4751
+ "build837",
4752
+ refuseSpec2
4753
+ );
4754
+ requireControlNumber(
4755
+ envelope.transactionSetControlNumber,
4756
+ "ST-02 / SE-02",
4757
+ "transactionSetControlNumber",
4758
+ "build837",
4759
+ refuseSpec2
4760
+ );
4613
4761
  const interchangeControlNumber = padControl2(envelope.interchangeControlNumber, 9);
4614
4762
  const isa = [
4615
4763
  "ISA",
@@ -5781,6 +5929,27 @@ function build271(spec) {
5781
5929
  const senderQualifier = envelope.senderQualifier ?? "ZZ";
5782
5930
  const receiverQualifier = envelope.receiverQualifier ?? "ZZ";
5783
5931
  const usageIndicator = envelope.usageIndicator ?? "P";
5932
+ requireControlNumber(
5933
+ envelope.interchangeControlNumber,
5934
+ "ISA-13 / IEA-02",
5935
+ "interchangeControlNumber",
5936
+ "build271",
5937
+ refuseSpec3
5938
+ );
5939
+ requireControlNumber(
5940
+ envelope.groupControlNumber,
5941
+ "GS-06 / GE-02",
5942
+ "groupControlNumber",
5943
+ "build271",
5944
+ refuseSpec3
5945
+ );
5946
+ requireControlNumber(
5947
+ envelope.transactionSetControlNumber,
5948
+ "ST-02 / SE-02",
5949
+ "transactionSetControlNumber",
5950
+ "build271",
5951
+ refuseSpec3
5952
+ );
5784
5953
  const interchangeControlNumber = padControl3(envelope.interchangeControlNumber, 9);
5785
5954
  const isa = [
5786
5955
  "ISA",
@@ -6151,8 +6320,13 @@ function get277CADisposition(delimiters, tx) {
6151
6320
  function walk277(delimiters, tx) {
6152
6321
  const warnings = [];
6153
6322
  const body = tx.se === void 0 ? tx.segments.slice(1) : tx.segments.slice(1, -1);
6154
- const implementationConventionReference = tx.st.elements[3];
6155
- const transactionType = implementationConventionReference === ICR_277CA ? "claim-acknowledgment" : "claim-status";
6323
+ const implementationConventionReferenceRaw = tx.st.elements[3];
6324
+ const implementationConventionReference = decodeSt03(
6325
+ implementationConventionReferenceRaw,
6326
+ delimiters,
6327
+ { segmentIndex: 0, transactionIndex: 0 }
6328
+ );
6329
+ const transactionType = implementationConventionReferenceRaw === ICR_277CA ? "claim-acknowledgment" : "claim-status";
6156
6330
  const hierarchies = [];
6157
6331
  const hlIndex = /* @__PURE__ */ new Map();
6158
6332
  const claims = [];
@@ -6507,6 +6681,27 @@ function buildClaimStatus(version, spec) {
6507
6681
  const senderQualifier = envelope.senderQualifier ?? "ZZ";
6508
6682
  const receiverQualifier = envelope.receiverQualifier ?? "ZZ";
6509
6683
  const usageIndicator = envelope.usageIndicator ?? "P";
6684
+ requireControlNumber(
6685
+ envelope.interchangeControlNumber,
6686
+ "ISA-13 / IEA-02",
6687
+ "interchangeControlNumber",
6688
+ "build277",
6689
+ refuseSpec4
6690
+ );
6691
+ requireControlNumber(
6692
+ envelope.groupControlNumber,
6693
+ "GS-06 / GE-02",
6694
+ "groupControlNumber",
6695
+ "build277",
6696
+ refuseSpec4
6697
+ );
6698
+ requireControlNumber(
6699
+ envelope.transactionSetControlNumber,
6700
+ "ST-02 / SE-02",
6701
+ "transactionSetControlNumber",
6702
+ "build277",
6703
+ refuseSpec4
6704
+ );
6510
6705
  const interchangeControlNumber = padControl4(envelope.interchangeControlNumber, 9);
6511
6706
  const isa = [
6512
6707
  "ISA",
@@ -7298,6 +7493,27 @@ function build820(spec) {
7298
7493
  const senderQualifier = envelope.senderQualifier ?? "ZZ";
7299
7494
  const receiverQualifier = envelope.receiverQualifier ?? "ZZ";
7300
7495
  const usageIndicator = envelope.usageIndicator ?? "P";
7496
+ requireControlNumber(
7497
+ envelope.interchangeControlNumber,
7498
+ "ISA-13 / IEA-02",
7499
+ "interchangeControlNumber",
7500
+ "build820",
7501
+ refuseSpec5
7502
+ );
7503
+ requireControlNumber(
7504
+ envelope.groupControlNumber,
7505
+ "GS-06 / GE-02",
7506
+ "groupControlNumber",
7507
+ "build820",
7508
+ refuseSpec5
7509
+ );
7510
+ requireControlNumber(
7511
+ envelope.transactionSetControlNumber,
7512
+ "ST-02 / SE-02",
7513
+ "transactionSetControlNumber",
7514
+ "build820",
7515
+ refuseSpec5
7516
+ );
7301
7517
  const interchangeControlNumber = padControl5(envelope.interchangeControlNumber, 9);
7302
7518
  const isa = [
7303
7519
  "ISA",
@@ -8005,6 +8221,27 @@ function build834(spec) {
8005
8221
  const senderQualifier = envelope.senderQualifier ?? "ZZ";
8006
8222
  const receiverQualifier = envelope.receiverQualifier ?? "ZZ";
8007
8223
  const usageIndicator = envelope.usageIndicator ?? "P";
8224
+ requireControlNumber(
8225
+ envelope.interchangeControlNumber,
8226
+ "ISA-13 / IEA-02",
8227
+ "interchangeControlNumber",
8228
+ "build834",
8229
+ refuseSpec6
8230
+ );
8231
+ requireControlNumber(
8232
+ envelope.groupControlNumber,
8233
+ "GS-06 / GE-02",
8234
+ "groupControlNumber",
8235
+ "build834",
8236
+ refuseSpec6
8237
+ );
8238
+ requireControlNumber(
8239
+ envelope.transactionSetControlNumber,
8240
+ "ST-02 / SE-02",
8241
+ "transactionSetControlNumber",
8242
+ "build834",
8243
+ refuseSpec6
8244
+ );
8008
8245
  const interchangeControlNumber = padControl6(envelope.interchangeControlNumber, 9);
8009
8246
  const isa = [
8010
8247
  "ISA",
@@ -8457,7 +8694,10 @@ function walk278(delimiters, tx, direction) {
8457
8694
  }
8458
8695
  }
8459
8696
  flushReview();
8460
- const icr = tx.st.elements[3];
8697
+ const icr = decodeSt03(tx.st.elements[3], delimiters, {
8698
+ segmentIndex: 0,
8699
+ transactionIndex: 0
8700
+ });
8461
8701
  return Object.freeze({
8462
8702
  direction,
8463
8703
  implementationConventionReference: icr === void 0 || icr === "" ? void 0 : icr,
@@ -8681,6 +8921,27 @@ function buildServicesReview(version, direction, spec) {
8681
8921
  const senderQualifier = envelope.senderQualifier ?? "ZZ";
8682
8922
  const receiverQualifier = envelope.receiverQualifier ?? "ZZ";
8683
8923
  const usageIndicator = envelope.usageIndicator ?? "P";
8924
+ requireControlNumber(
8925
+ envelope.interchangeControlNumber,
8926
+ "ISA-13 / IEA-02",
8927
+ "interchangeControlNumber",
8928
+ "build278",
8929
+ refuseSpec7
8930
+ );
8931
+ requireControlNumber(
8932
+ envelope.groupControlNumber,
8933
+ "GS-06 / GE-02",
8934
+ "groupControlNumber",
8935
+ "build278",
8936
+ refuseSpec7
8937
+ );
8938
+ requireControlNumber(
8939
+ envelope.transactionSetControlNumber,
8940
+ "ST-02 / SE-02",
8941
+ "transactionSetControlNumber",
8942
+ "build278",
8943
+ refuseSpec7
8944
+ );
8684
8945
  const interchangeControlNumber = padControl7(envelope.interchangeControlNumber, 9);
8685
8946
  const isa = [
8686
8947
  "ISA",
@@ -9388,11 +9649,12 @@ function parseTA1(interchange) {
9388
9649
  const ta1 = interchange.ta1Segments[0];
9389
9650
  if (ta1 === void 0) return void 0;
9390
9651
  const elements = ta1.elements;
9391
- const interchangeControlNumber = elements[1] ?? "";
9392
- const interchangeDate = elements[2] ?? "";
9393
- const interchangeTime = elements[3] ?? "";
9394
- const ackCodeRaw = elements[4] ?? "";
9395
- const noteCodeRaw = elements[5] ?? "";
9652
+ const value = (index) => unescapeRelease(elements[index] ?? "", interchange.delimiters, noop3, { segmentIndex: 0 });
9653
+ const interchangeControlNumber = value(1);
9654
+ const interchangeDate = value(2);
9655
+ const interchangeTime = value(3);
9656
+ const ackCodeRaw = value(4);
9657
+ const noteCodeRaw = value(5);
9396
9658
  const ackCode = narrowAckCode(ackCodeRaw) ?? TA1_ACK_CODES.R;
9397
9659
  const noteCode = narrowNoteCode(noteCodeRaw);
9398
9660
  return Object.freeze({
@@ -9405,6 +9667,8 @@ function parseTA1(interchange) {
9405
9667
  raw: ta1
9406
9668
  });
9407
9669
  }
9670
+ var noop3 = (_w) => {
9671
+ };
9408
9672
  function narrowAckCode(value) {
9409
9673
  switch (value) {
9410
9674
  case "A":
@@ -9446,6 +9710,27 @@ function build999(spec) {
9446
9710
  const senderQualifier = envelope.senderQualifier ?? "ZZ";
9447
9711
  const receiverQualifier = envelope.receiverQualifier ?? "ZZ";
9448
9712
  const usageIndicator = envelope.usageIndicator ?? "P";
9713
+ requireControlNumber(
9714
+ envelope.interchangeControlNumber,
9715
+ "ISA-13 / IEA-02",
9716
+ "interchangeControlNumber",
9717
+ "build999",
9718
+ refuseSpec8
9719
+ );
9720
+ requireControlNumber(
9721
+ envelope.groupControlNumber,
9722
+ "GS-06 / GE-02",
9723
+ "groupControlNumber",
9724
+ "build999",
9725
+ refuseSpec8
9726
+ );
9727
+ requireControlNumber(
9728
+ envelope.transactionSetControlNumber,
9729
+ "ST-02 / SE-02",
9730
+ "transactionSetControlNumber",
9731
+ "build999",
9732
+ refuseSpec8
9733
+ );
9449
9734
  const interchangeControlNumber = padControl8(envelope.interchangeControlNumber, 9);
9450
9735
  const isa = [
9451
9736
  "ISA",
@@ -9514,6 +9799,13 @@ function build999(spec) {
9514
9799
  elementSeparator,
9515
9800
  segmentTerminator
9516
9801
  );
9802
+ requireControlNumber(
9803
+ functionalGroup.groupControlNumber,
9804
+ "AK1-02",
9805
+ "functionalGroup.groupControlNumber",
9806
+ "build999",
9807
+ refuseSpec8
9808
+ );
9517
9809
  const ak1 = joinSeg(
9518
9810
  [
9519
9811
  "AK1",
@@ -9620,6 +9912,13 @@ function responseHasAnyError(response) {
9620
9912
  return false;
9621
9913
  }
9622
9914
  function buildAk2(response, esc, elementSeparator, segmentTerminator) {
9915
+ requireControlNumber(
9916
+ response.transactionSetControlNumber,
9917
+ "AK2-02",
9918
+ "transactionResponses[].transactionSetControlNumber",
9919
+ "build999",
9920
+ refuseSpec8
9921
+ );
9623
9922
  const parts = [
9624
9923
  "AK2",
9625
9924
  esc(response.transactionSetIdCode),
@@ -9731,18 +10030,49 @@ function expandYY8(yymmdd) {
9731
10030
  // src/transactions/ack/build-ta1.ts
9732
10031
  function buildTA1(spec, options = {}) {
9733
10032
  enforceAcceptIsClean2(spec);
9734
- const elementSeparator = options.elementSeparator ?? "*";
10033
+ const delimiters = {
10034
+ element: options.elementSeparator ?? "*",
10035
+ repetition: options.repetitionSeparator ?? "^",
10036
+ component: options.componentSeparator ?? ":",
10037
+ segment: options.segmentTerminator ?? "~"
10038
+ };
10039
+ const esc = makeCallerEscaper(delimiters, "buildTA1", refuseSpec9);
10040
+ requireControlNumber(
10041
+ spec.interchangeControlNumber,
10042
+ "TA1-01",
10043
+ "interchangeControlNumber",
10044
+ "buildTA1",
10045
+ refuseSpec9
10046
+ );
10047
+ const interchangeControlNumber = esc(spec.interchangeControlNumber);
10048
+ const interchangeDate = esc(spec.interchangeDate);
10049
+ const interchangeTime = esc(spec.interchangeTime);
10050
+ const ackCode = esc(spec.ackCode);
10051
+ const noteCode = esc(spec.noteCode);
10052
+ requireTa1Element(interchangeDate, "TA1-02", "interchangeDate");
10053
+ requireTa1Element(interchangeTime, "TA1-03", "interchangeTime");
10054
+ requireTa1Element(ackCode, "TA1-04", "ackCode");
10055
+ requireTa1Element(noteCode, "TA1-05", "noteCode");
9735
10056
  const elements = Object.freeze([
9736
10057
  "TA1",
9737
- spec.interchangeControlNumber,
9738
- spec.interchangeDate,
9739
- spec.interchangeTime,
9740
- spec.ackCode,
9741
- spec.noteCode
10058
+ interchangeControlNumber,
10059
+ interchangeDate,
10060
+ interchangeTime,
10061
+ ackCode,
10062
+ noteCode
9742
10063
  ]);
9743
- const raw = elements.join(elementSeparator);
10064
+ const raw = elements.join(delimiters.element);
9744
10065
  return Object.freeze({ raw, elements });
9745
10066
  }
10067
+ function refuseSpec9(message) {
10068
+ throw new AckBuildError(ACK_BUILD_ERROR_CODES.X12_ACK_INVALID_SPEC, message);
10069
+ }
10070
+ function requireTa1Element(emitted, slot, field) {
10071
+ if (emitted !== "") return;
10072
+ refuseSpec9(
10073
+ `buildTA1: ${field} is empty. ${slot} is a required element and this builder never invents one, so nothing is emitted. Supply ${field}.`
10074
+ );
10075
+ }
9746
10076
  function enforceAcceptIsClean2(spec) {
9747
10077
  if (spec.ackCode === TA1_ACK_CODES.A && spec.noteCode !== "000") {
9748
10078
  throw new AckBuildError(
@@ -9931,7 +10261,7 @@ var X12BuildError = class extends Error {
9931
10261
  };
9932
10262
 
9933
10263
  // src/builder/build-interchange.ts
9934
- function refuseSpec9(message) {
10264
+ function refuseSpec10(message) {
9935
10265
  throw new X12BuildError(X12_BUILD_ERROR_CODES.X12_BUILD_INVALID_SPEC, message);
9936
10266
  }
9937
10267
  function buildInterchange(spec) {
@@ -9945,11 +10275,18 @@ function buildInterchange(spec) {
9945
10275
  component: componentSeparator,
9946
10276
  segment: segmentTerminator
9947
10277
  };
9948
- const esc = makeCallerEscaper(delimiters, "buildInterchange", refuseSpec9);
10278
+ const esc = makeCallerEscaper(delimiters, "buildInterchange", refuseSpec10);
9949
10279
  const senderQualifier = spec.senderQualifier ?? "ZZ";
9950
10280
  const receiverQualifier = spec.receiverQualifier ?? "ZZ";
9951
10281
  const usageIndicator = spec.usageIndicator ?? "P";
9952
10282
  const version = spec.version ?? "00501";
10283
+ requireControlNumber(
10284
+ spec.interchangeControlNumber,
10285
+ "ISA-13 / IEA-02",
10286
+ "interchangeControlNumber",
10287
+ "buildInterchange",
10288
+ refuseSpec10
10289
+ );
9953
10290
  const interchangeControlNumber = padControl9(spec.interchangeControlNumber, 9);
9954
10291
  const isa = [
9955
10292
  "ISA",
@@ -9999,23 +10336,32 @@ function buildInterchange(spec) {
9999
10336
  return parseX12(raw);
10000
10337
  }
10001
10338
  function buildGroup(group, spec, esc, elementSeparator, segmentTerminator) {
10339
+ requireControlNumber(
10340
+ group.groupControlNumber,
10341
+ "GS-06 / GE-02",
10342
+ "groupControlNumber",
10343
+ "buildInterchange",
10344
+ refuseSpec10
10345
+ );
10002
10346
  const applicationSenderCode = group.applicationSenderCode ?? spec.senderId;
10003
10347
  const applicationReceiverCode = group.applicationReceiverCode ?? spec.receiverId;
10004
10348
  const groupDate = group.groupDate ?? expandYY9(spec.interchangeDate);
10005
10349
  const groupTime = group.groupTime ?? spec.interchangeTime;
10006
10350
  const responsibleAgencyCode = group.responsibleAgencyCode ?? "X";
10351
+ const gsParts = [
10352
+ "GS",
10353
+ group.functionalIdCode,
10354
+ applicationSenderCode,
10355
+ applicationReceiverCode,
10356
+ groupDate,
10357
+ groupTime,
10358
+ group.groupControlNumber,
10359
+ responsibleAgencyCode,
10360
+ group.versionRelease
10361
+ ];
10362
+ requireCallerSegment(gsParts, "buildInterchange", refuseSpec10);
10007
10363
  const gs = joinSeg2(
10008
- [
10009
- "GS",
10010
- esc(group.functionalIdCode),
10011
- esc(applicationSenderCode),
10012
- esc(applicationReceiverCode),
10013
- groupDate,
10014
- groupTime,
10015
- esc(group.groupControlNumber),
10016
- responsibleAgencyCode,
10017
- esc(group.versionRelease)
10018
- ],
10364
+ gsParts.map((value, index) => index === 0 ? value : esc(value)),
10019
10365
  elementSeparator,
10020
10366
  segmentTerminator
10021
10367
  );
@@ -10031,6 +10377,13 @@ function buildGroup(group, spec, esc, elementSeparator, segmentTerminator) {
10031
10377
  return gs + transactions + ge;
10032
10378
  }
10033
10379
  function buildTransaction(tx, esc, elementSeparator, segmentTerminator) {
10380
+ requireControlNumber(
10381
+ tx.transactionSetControlNumber,
10382
+ "ST-02 / SE-02",
10383
+ "transactionSetControlNumber",
10384
+ "buildInterchange",
10385
+ refuseSpec10
10386
+ );
10034
10387
  const stParts = ["ST", esc(tx.transactionSetIdCode), esc(tx.transactionSetControlNumber)];
10035
10388
  if (tx.implementationConventionReference !== void 0) {
10036
10389
  stParts.push(esc(tx.implementationConventionReference));
@@ -10059,7 +10412,7 @@ function buildTransaction(tx, esc, elementSeparator, segmentTerminator) {
10059
10412
  return st + bodySegments + se;
10060
10413
  }
10061
10414
  function joinSeg2(parts, elementSeparator, segmentTerminator) {
10062
- requireCallerSegment(parts, "buildInterchange", refuseSpec9);
10415
+ requireCallerSegment(parts, "buildInterchange", refuseSpec10);
10063
10416
  return parts.join(elementSeparator) + segmentTerminator;
10064
10417
  }
10065
10418
  function pad9(value, width) {
@@ -10368,7 +10721,7 @@ var profiles = Object.freeze({
10368
10721
  });
10369
10722
 
10370
10723
  // src/index.ts
10371
- var VERSION = "0.0.14";
10724
+ var VERSION = "0.0.16";
10372
10725
 
10373
10726
  export { ACK_BUILD_ERROR_CODES, ALL_WARNING_MESSAGES, AUTH_278_BUILD_ERROR_CODES, AUTH_278_LOOP_2000A, AUTH_278_LOOP_2000B, AUTH_278_LOOP_2000C, AUTH_278_LOOP_2000D, AUTH_278_LOOP_2000E, AUTH_278_LOOP_2000F, AckBuildError, BALANCE_INVARIANTS, BUILD_REFUSAL_VALUE_MAX_LENGTH, BUILD_REFUSAL_VALUE_MAX_RENDERED, CARC, CLAIM_837D_LOOP_2000A, CLAIM_837D_LOOP_2300, CLAIM_837D_LOOP_2400, CLAIM_837I_LOOP_2000A, CLAIM_837I_LOOP_2300, CLAIM_837I_LOOP_2400, CLAIM_837P_LOOP_2000A, CLAIM_837P_LOOP_2300, CLAIM_837P_LOOP_2400, CLAIM_837P_LOOP_2410, CLAIM_837_BUILD_ERROR_CODES, CLAIM_837_LOOP_1000A, CLAIM_837_LOOP_1000B, CLAIM_837_LOOP_2010AA, CLAIM_837_LOOP_2010BA, CLAIM_837_LOOP_2010BB, CLAIM_837_LOOP_2010CA, CLAIM_837_LOOP_2430, CLAIM_ADJUSTMENT_GROUP_CODES, CLAIM_STATUS_277_BUILD_ERROR_CODES, CLAIM_STATUS_CATEGORY_CODES, CLAIM_STATUS_CODES, CLP_STATUS, CONTROL_NUMBER_PAIRS, Claim837BuildError, ClaimStatus277BuildError, DELIMITER_POSITIONS, ELIGIBILITY_271_BUILD_ERROR_CODES, ELIGIBILITY_271_LOOP_2000A, ELIGIBILITY_271_LOOP_2000B, ELIGIBILITY_271_LOOP_2000C, ELIGIBILITY_271_LOOP_2000D, ELIGIBILITY_271_LOOP_2100C, ELIGIBILITY_271_LOOP_2100D, ELIGIBILITY_271_LOOP_2110, ENROLLMENT_834_BUILD_ERROR_CODES, ENROLLMENT_834_LOOP_1000A, ENROLLMENT_834_LOOP_1000B, ENROLLMENT_834_LOOP_2000, ENROLLMENT_834_LOOP_2100A, ENROLLMENT_834_LOOP_2300, ENROLLMENT_834_LOOP_2320, Eligibility271BuildError, Enrollment834BuildError, FATAL_CODES, HI_QUALIFIERS, HL_LEVEL_CODES, IK3_SYNTAX_ERROR_CODES, IK4_SYNTAX_ERROR_CODES, ISA_MIN_LENGTH, LoopSpecDefinitionError, MAINTENANCE_TYPE_CODES, NM1_QUALIFIERS, NON_SPEC_SEGMENT_ID, PREMIUM_820_BUILD_ERROR_CODES, PREMIUM_820_LOOP_1000A, PREMIUM_820_LOOP_1000B, PREMIUM_820_LOOP_2000A, PREMIUM_820_LOOP_2100A, PREMIUM_820_LOOP_2300A, PREMIUM_820_LOOP_2310A, Premium820BuildError, RARC, RELEASE_CHAR, REMIT_835_BUILD_ERROR_CODES, REMIT_835_LOOP_1000A, REMIT_835_LOOP_1000B, REMIT_835_LOOP_2000, REMIT_835_LOOP_2100, REMIT_835_LOOP_2110, REQUIRED_LOOPS, Remit835BuildError, SERVICE_TYPE_CODES, STATUS_277_LOOP_2000A, STATUS_277_LOOP_2000B, STATUS_277_LOOP_2000C, STATUS_277_LOOP_2000D, STATUS_277_LOOP_2000E, STATUS_277_LOOP_2200, STATUS_277_LOOP_2220, ServicesReview278BuildError, TA1_ACK_CODES, TA1_NOTE_CODES, UNEXPECTED_SEGMENT_CONTEXTS, VERSION, WARNING_CODES, X12BuildError, X12Decimal, X12ParseError, X12ProfileError, X12_ACK_DISPOSITION_CODES, X12_BUILD_ERROR_CODES, ambiguous837Variant, amountRowDropped, balanceNotEvaluable, build271, build277, build277CA, build278Request, build278Response, build820, build834, build835, build837D, build837I, build837P, build999, buildInterchange, buildTA1, checkClaimBalance, checkRemitTotalBalance, checkServiceLineBalance, collectElementValues, componentOptional, controlNumberMismatch, danglingReleaseChar, decodeSegment, defineLoopSpec, defineProfile, detectDelimiters, elementDecimal, elementDecimalOrZero, elementOptional, elementValue, entitySegmentDiscardedAfterLx, escapeRelease, get271Eligibility, get277CADisposition, get277Status, get278Request, get278Response, get820Payments, get834Enrollments, get834Header, get835, get837Claims, getAllSegmentValues, getDefaultProfile, getSegmentValue, groupCountMismatch, hlParentLevelInvalid, hlParentMismatch, isAcceptDisposition, isClaimAdjustmentGroupCode, isDiagnosisQualifier, isProcedureQualifier, lookupCarc, lookupClaimStatus, lookupClaimStatusCategory, lookupClpStatus, lookupMaintenanceType, lookupRarc, lookupServiceType, missingGe, missingIea, missingRequiredLoop, missingSe, parse999, parseTA1, parseX12, partitionWarnings, payToAddressRepeated, pre005010, profiles, readElementDecimal, remitBalanceMismatch, renderCallerValue, resolveHiQualifier, segmentCountMismatch, serializeX12, serviceLineDropped, serviceLineNotDecoded, serviceSegmentRepeated, serviceSegmentWithoutLx, setDefaultProfile, statedAmountDiscarded, trailingGarbage, transactionCountMismatch, unescapeRelease, unexpectedSegment, unknown837Variant, unknownCarc, unknownClaimStatus, unknownClaimStatusCategory, unknownHiQualifier, unknownMaintenanceType, unknownRarc, unparseableDecimal };
10374
10727
  //# sourceMappingURL=index.mjs.map