@cosyte/synth 0.1.8 → 0.1.10

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.
Files changed (55) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/README.md +28 -17
  3. package/dist/animal-code-Bdl_9Jma.d.cts +30 -0
  4. package/dist/animal-code-Bdl_9Jma.d.ts +30 -0
  5. package/dist/astm/index.cjs +150 -15
  6. package/dist/astm/index.cjs.map +1 -1
  7. package/dist/astm/index.d.cts +22 -9
  8. package/dist/astm/index.d.ts +22 -9
  9. package/dist/astm/index.mjs +150 -15
  10. package/dist/astm/index.mjs.map +1 -1
  11. package/dist/ccda/index.cjs +150 -15
  12. package/dist/ccda/index.cjs.map +1 -1
  13. package/dist/ccda/index.d.cts +12 -3
  14. package/dist/ccda/index.d.ts +12 -3
  15. package/dist/ccda/index.mjs +150 -15
  16. package/dist/ccda/index.mjs.map +1 -1
  17. package/dist/deid/index.cjs +540 -28
  18. package/dist/deid/index.cjs.map +1 -1
  19. package/dist/deid/index.d.cts +2 -1
  20. package/dist/deid/index.d.ts +2 -1
  21. package/dist/deid/index.mjs +540 -28
  22. package/dist/deid/index.mjs.map +1 -1
  23. package/dist/fhir/index.cjs +145 -12
  24. package/dist/fhir/index.cjs.map +1 -1
  25. package/dist/fhir/index.d.cts +4 -1
  26. package/dist/fhir/index.d.ts +4 -1
  27. package/dist/fhir/index.mjs +145 -12
  28. package/dist/fhir/index.mjs.map +1 -1
  29. package/dist/hl7/index.cjs +569 -37
  30. package/dist/hl7/index.cjs.map +1 -1
  31. package/dist/hl7/index.d.cts +402 -111
  32. package/dist/hl7/index.d.ts +402 -111
  33. package/dist/hl7/index.mjs +560 -38
  34. package/dist/hl7/index.mjs.map +1 -1
  35. package/dist/index.cjs +124 -13
  36. package/dist/index.cjs.map +1 -1
  37. package/dist/index.d.cts +183 -10
  38. package/dist/index.d.ts +183 -10
  39. package/dist/index.mjs +115 -14
  40. package/dist/index.mjs.map +1 -1
  41. package/dist/ncpdp/index.cjs +145 -12
  42. package/dist/ncpdp/index.cjs.map +1 -1
  43. package/dist/ncpdp/index.d.cts +4 -1
  44. package/dist/ncpdp/index.d.ts +4 -1
  45. package/dist/ncpdp/index.mjs +145 -12
  46. package/dist/ncpdp/index.mjs.map +1 -1
  47. package/dist/{quirk-BfObsiWV.d.ts → quirk-CzknuYUo.d.ts} +16 -5
  48. package/dist/{quirk-BCSs1jPt.d.cts → quirk-Q5AxzHSC.d.cts} +16 -5
  49. package/dist/x12/index.cjs +145 -12
  50. package/dist/x12/index.cjs.map +1 -1
  51. package/dist/x12/index.d.cts +4 -1
  52. package/dist/x12/index.d.ts +4 -1
  53. package/dist/x12/index.mjs +145 -12
  54. package/dist/x12/index.mjs.map +1 -1
  55. package/package.json +17 -6
@@ -73,7 +73,11 @@ var SYNTH_FATAL_CODES = Object.freeze({
73
73
  SYNTH_INVALID_DECIMAL: "SYNTH_INVALID_DECIMAL",
74
74
  /** An integer range was requested with its maximum below its minimum. Fatal. */
75
75
  SYNTH_INVALID_RANGE: "SYNTH_INVALID_RANGE",
76
- /** A value was drawn from an empty pool. Fatal, never a fabricated substitute. */
76
+ /**
77
+ * A value was drawn from an empty pool. Fatal, never a fabricated substitute. A corpus given an
78
+ * empty `mix`, `triggers` or `quirks` list (or a profile whose `quirks` is an empty list) raises it
79
+ * too: there is no kind to cycle through, and an empty list is never read as the default.
80
+ */
77
81
  SYNTH_EMPTY_POOL: "SYNTH_EMPTY_POOL",
78
82
  /** A `defineSynthProfile` spec was not usable (a missing or blank `name`). Fatal. */
79
83
  SYNTH_INVALID_PROFILE: "SYNTH_INVALID_PROFILE",
@@ -92,7 +96,21 @@ var SYNTH_FATAL_CODES = Object.freeze({
92
96
  * apart without matching message text. Fatal, and raised **before** anything is generated: a
93
97
  * mislabelled artifact is worse than no artifact.
94
98
  */
95
- SYNTH_PROFILE_NOT_GENERATED: "SYNTH_PROFILE_NOT_GENERATED"
99
+ SYNTH_PROFILE_NOT_GENERATED: "SYNTH_PROFILE_NOT_GENERATED",
100
+ /**
101
+ * The sibling parser a generator builds through is not installed. Raised on the first call that
102
+ * needs it, by both builds, only when the missing package is the parser itself: a missing
103
+ * dependency of an installed parser is thrown as Node.js raised it. The error carries the parser's
104
+ * package name on `peer`, a message from {@link SYNTH_PEER_MESSAGES} that names its install
105
+ * command, and the error Node.js raised on `cause`.
106
+ */
107
+ SYNTH_PEER_NOT_INSTALLED: "SYNTH_PEER_NOT_INSTALLED",
108
+ /**
109
+ * An HL7 v2 `ACK` was asked for an inbound message that carries no MSH-10 message control id.
110
+ * MSA-2 must echo that id, and an acknowledgment that cannot be matched to the message it answers
111
+ * is not a fixture worth having, so nothing is generated.
112
+ */
113
+ SYNTH_ACK_UNCORRELATED: "SYNTH_ACK_UNCORRELATED"
96
114
  });
97
115
  var SYNTH_FATAL_MESSAGES = Object.freeze({
98
116
  SYNTH_UNSUPPORTED_FORMAT: "The requested format is not generable by this build. A generator has no byte fallback: it builds through a parser's own serializer or it refuses.",
@@ -105,18 +123,38 @@ var SYNTH_FATAL_MESSAGES = Object.freeze({
105
123
  SYNTH_EMPTY_POOL: "A value was drawn from an empty pool.",
106
124
  SYNTH_INVALID_PROFILE: "defineSynthProfile requires a non-empty string name.",
107
125
  SYNTH_UNSUPPORTED_KIND: "The requested kind, document type, corpus mix entry, variant or profile is not one this generator supports. The supported set is the exported union for that option.",
108
- SYNTH_PROFILE_NOT_GENERATED: "The requested profile is published by the adopted implementation guide, and this build does not generate it. The coverage surface reports, per adopted profile, whether it is generated."
126
+ SYNTH_PROFILE_NOT_GENERATED: "The requested profile is published by the adopted implementation guide, and this build does not generate it. The coverage surface reports, per adopted profile, whether it is generated.",
127
+ SYNTH_PEER_NOT_INSTALLED: "A sibling @cosyte parser this generator builds through is not installed. Install it beside @cosyte/synth.",
128
+ SYNTH_ACK_UNCORRELATED: (
129
+ // Stryker disable next-line all: a module-level table runs once, at import; the docs test holds it.
130
+ "The inbound message has no MSH-10 control id for an ACK to echo in MSA-2."
131
+ )
132
+ });
133
+ var SYNTH_PEER_MESSAGES = Object.freeze({
134
+ "@cosyte/hl7": "@cosyte/hl7 is not installed, and this generator builds through it. Install it beside @cosyte/synth: npm install @cosyte/hl7",
135
+ "@cosyte/fhir": "@cosyte/fhir is not installed, and this generator builds through it. Install it beside @cosyte/synth: npm install @cosyte/fhir",
136
+ "@cosyte/ccda": "@cosyte/ccda is not installed, and this generator builds through it. Install it beside @cosyte/synth: npm install @cosyte/ccda",
137
+ "@cosyte/x12": "@cosyte/x12 is not installed, and this generator builds through it. Install it beside @cosyte/synth: npm install @cosyte/x12",
138
+ "@cosyte/ncpdp": "@cosyte/ncpdp is not installed, and this generator builds through it. Install it beside @cosyte/synth: npm install @cosyte/ncpdp",
139
+ "@cosyte/astm": "@cosyte/astm is not installed, and this generator builds through it. Install it beside @cosyte/synth: npm install @cosyte/astm",
140
+ "@cosyte/deid": "@cosyte/deid is not installed, and this generator builds through it. Install it beside @cosyte/synth: npm install @cosyte/deid"
109
141
  });
110
142
  var SynthError = class extends Error {
111
143
  /** The stable fatal code. */
112
144
  code;
113
145
  /**
114
146
  * @param code - The stable {@link SynthFatalCode}. The message comes from the frozen registry.
147
+ * @param options - The error that caused this one, and for a missing parser its package.
115
148
  */
116
- constructor(code) {
117
- super(SYNTH_FATAL_MESSAGES[code]);
149
+ constructor(code, options = {}) {
150
+ const peer = code === SYNTH_FATAL_CODES.SYNTH_PEER_NOT_INSTALLED ? options.peer : void 0;
151
+ super(
152
+ peer === void 0 ? SYNTH_FATAL_MESSAGES[code] : SYNTH_PEER_MESSAGES[peer],
153
+ "cause" in options ? { cause: options.cause } : void 0
154
+ );
118
155
  this.name = "SynthError";
119
156
  this.code = code;
157
+ if (peer !== void 0) Object.defineProperty(this, "peer", { value: peer, enumerable: true });
120
158
  }
121
159
  };
122
160
 
@@ -224,7 +262,7 @@ function luhnMod10(digits) {
224
262
  if (d < 0 || d > 9) continue;
225
263
  if (double) {
226
264
  d *= 2;
227
- if (d > 9) d -= 9;
265
+ if (d >= 10) d -= 9;
228
266
  }
229
267
  sum += d;
230
268
  double = !double;
@@ -321,7 +359,7 @@ var SYNTHETIC_CITY_NAMES = Object.freeze([
321
359
  ]);
322
360
 
323
361
  // src/safe/providers.ts
324
- function ssn(rng, block = "never-issued") {
362
+ function ssn(rng, block) {
325
363
  if (block === "advertising") {
326
364
  return `987-00-432${String(rng.int(0, 9))}`;
327
365
  }
@@ -448,6 +486,63 @@ var US_STATES = Object.freeze([
448
486
  "WY"
449
487
  ]);
450
488
 
489
+ // src/safe/animal-code.ts
490
+ var ANIMAL_CODE_SYSTEM = "99COSYTE";
491
+ function animalCode(value, text) {
492
+ return Object.freeze({ code: value, text, system: ANIMAL_CODE_SYSTEM });
493
+ }
494
+ function animalSpecies(value, text, breeds) {
495
+ return Object.freeze({ ...animalCode(value, text), breeds: Object.freeze([...breeds]) });
496
+ }
497
+
498
+ // src/safe/animal.ts
499
+ var SYNTHETIC_ANIMAL_SPECIES = Object.freeze([
500
+ animalSpecies("DOG", "Dog", [
501
+ animalCode("DOG-LAB", "Labrador Retriever"),
502
+ animalCode("DOG-BGL", "Beagle"),
503
+ animalCode("DOG-MIX", "Mixed breed dog")
504
+ ]),
505
+ animalSpecies("CAT", "Cat", [animalCode("CAT-DSH", "Domestic Shorthair"), animalCode("CAT-SIA", "Siamese")]),
506
+ animalSpecies("HORSE", "Horse", [animalCode("HRS-QTR", "Quarter Horse"), animalCode("HRS-TB", "Thoroughbred")]),
507
+ animalSpecies("CATTLE", "Cattle", [animalCode("CTL-HOL", "Holstein"), animalCode("CTL-ANG", "Angus")])
508
+ ]);
509
+ var SYNTHETIC_PET_NAMES = Object.freeze([
510
+ "Fixturepaw",
511
+ "Mockwhisker",
512
+ "Stubtail",
513
+ "Seedpup",
514
+ "Testbiscuit",
515
+ "Placepurr",
516
+ "Synthfur",
517
+ "Dummymane",
518
+ "Examplehoof",
519
+ "Fauxfluff",
520
+ "Samplesnout",
521
+ "Voidwag"
522
+ ]);
523
+ var MICROCHIP_TEST_PREFIX = "999";
524
+ function petName(rng) {
525
+ return rng.pick(SYNTHETIC_PET_NAMES);
526
+ }
527
+ function animalBreed(rng) {
528
+ const picked = rng.pick(SYNTHETIC_ANIMAL_SPECIES);
529
+ const { breeds, ...bare } = picked;
530
+ return { species: Object.freeze(bare), breed: rng.pick(breeds) };
531
+ }
532
+ function microchip(rng) {
533
+ return `${MICROCHIP_TEST_PREFIX}${rng.digits(12)}`;
534
+ }
535
+ function animalBirthDate(rng, onDate) {
536
+ const year = Number(onDate.slice(0, 4));
537
+ const month = Number(onDate.slice(4, 6)) - 1;
538
+ const day = Number(onDate.slice(6, 8));
539
+ const latest = Date.UTC(year, month, day);
540
+ const earliest = Date.UTC(year - 20, month, day);
541
+ const span = Math.round((latest - earliest) / 864e5);
542
+ const born = new Date(latest - rng.int(0, span) * 864e5);
543
+ return born.toISOString().slice(0, 10).replace(/-/g, "");
544
+ }
545
+
451
546
  // src/safe/index.ts
452
547
  var safe = Object.freeze({
453
548
  ssn,
@@ -461,7 +556,11 @@ var safe = Object.freeze({
461
556
  address,
462
557
  dateYmd,
463
558
  npi,
464
- dea
559
+ dea,
560
+ petName,
561
+ animalBreed,
562
+ microchip,
563
+ animalBirthDate
465
564
  });
466
565
 
467
566
  // src/ncpdp/identity.ts
@@ -542,14 +641,44 @@ function defaultHost() {
542
641
  if (typeof undefined !== "function") return { require: __require, resolve: void 0 };
543
642
  return { require: module$1.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href))), resolve: (s) => undefined(s) };
544
643
  }
644
+ function isDenoResolveRefusal(error) {
645
+ return error instanceof TypeError && !("code" in error) && error.message === "Invalid arguments";
646
+ }
545
647
  function isRequireEsmRefusal(error) {
546
648
  return error?.code === "ERR_REQUIRE_ESM";
547
649
  }
650
+ function peerPackage(specifier) {
651
+ const name2 = specifier.split("/").slice(0, 2).join("/");
652
+ return Object.hasOwn(SYNTH_PEER_MESSAGES, name2) ? name2 : void 0;
653
+ }
654
+ function isPeerNotFound(error, specifier, peer) {
655
+ const { code, message } = error ?? {};
656
+ if (typeof message !== "string") return false;
657
+ if (code === "ERR_MODULE_NOT_FOUND") return message.startsWith(`Cannot find package '${peer}'`);
658
+ if (code === "MODULE_NOT_FOUND") return message.startsWith(`Cannot find module '${specifier}'`);
659
+ return false;
660
+ }
548
661
  function loadPeer(specifier, host) {
662
+ try {
663
+ return loadFrom(specifier, host);
664
+ } catch (error) {
665
+ const peer = peerPackage(specifier);
666
+ if (peer === void 0 || !isPeerNotFound(error, specifier, peer)) throw error;
667
+ throw new SynthError(SYNTH_FATAL_CODES.SYNTH_PEER_NOT_INSTALLED, { peer, cause: error });
668
+ }
669
+ }
670
+ function loadFrom(specifier, host) {
549
671
  if (host.resolve === void 0) return host.require(specifier);
550
- const entry = url.fileURLToPath(host.resolve(specifier));
672
+ let url$1;
673
+ try {
674
+ url$1 = host.resolve(specifier);
675
+ } catch (error) {
676
+ if (!isDenoResolveRefusal(error)) throw error;
677
+ return host.require(specifier);
678
+ }
679
+ if (!url$1.startsWith("file:")) return host.require(specifier);
551
680
  try {
552
- return host.require(entry);
681
+ return host.require(url.fileURLToPath(url$1));
553
682
  } catch (error) {
554
683
  if (!isRequireEsmRefusal(error)) throw error;
555
684
  return host.require(specifier);
@@ -808,6 +937,10 @@ function resolveMix(allowed, requested, fallback) {
808
937
  if (requested === void 0) return fallback;
809
938
  return requested.map((entry) => resolveKind(allowed, entry));
810
939
  }
940
+ function requireSelection(selection) {
941
+ if (selection.length === 0) throw new SynthError(SYNTH_FATAL_CODES.SYNTH_EMPTY_POOL);
942
+ return selection;
943
+ }
811
944
 
812
945
  // src/ncpdp/index.ts
813
946
  var ALL_KINDS = Object.freeze([
@@ -835,11 +968,11 @@ function generateKind(kind, seed, verify) {
835
968
  }
836
969
  function ncpdpCorpus(options) {
837
970
  const { seed, verify = true } = options;
838
- const mix = resolveMix(ALL_KINDS, options.mix, DEFAULT_MIX);
971
+ const mix = requireSelection(resolveMix(ALL_KINDS, options.mix, DEFAULT_MIX));
839
972
  const count = options.count ?? mix.length;
840
973
  const seedStream = createRng(seed);
841
974
  const artifacts = Array.from({ length: count }, (_unused, i) => {
842
- const kind = mix[i % mix.length] ?? "NewRx";
975
+ const kind = mix[i % mix.length];
843
976
  const txSeed = seedStream.nextUint32();
844
977
  const rt = generateKind(kind, txSeed, verify);
845
978
  return {