@rethinkhealth/hl7v2-jsonify 0.2.2 → 0.2.4

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.d.ts CHANGED
@@ -1,10 +1,11 @@
1
- import type { HL7v2Node } from '@rethinkhealth/hl7v2-ast';
1
+ import type { Nodes } from '@rethinkhealth/hl7v2-ast';
2
2
  import type { Plugin } from 'unified';
3
+ type FieldValue = string | string[];
3
4
  type SegmentJSON = {
4
5
  segment: string;
5
- fields: (string | string[])[];
6
+ fields: (FieldValue | FieldValue[])[];
6
7
  };
7
- export declare const hl7v2Jsonify: Plugin<[], HL7v2Node, string>;
8
- export declare function toJson(root: HL7v2Node): SegmentJSON[];
8
+ export declare const hl7v2Jsonify: Plugin<[], Nodes, string>;
9
+ export declare function toJson(root: Nodes): SegmentJSON[];
9
10
  export {};
10
11
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAC1D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAItC,KAAK,WAAW,GAAG;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,CAAC,MAAM,GAAG,MAAM,EAAE,CAAC,EAAE,CAAC;CAC/B,CAAC;AAEF,eAAO,MAAM,YAAY,EAAE,MAAM,CAAC,EAAE,EAAE,SAAS,EAAE,MAAM,CAStD,CAAC;AAEF,wBAAgB,MAAM,CAAC,IAAI,EAAE,SAAS,GAAG,WAAW,EAAE,CA6BrD"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAIV,KAAK,EAIN,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAGtC,KAAK,UAAU,GAAG,MAAM,GAAG,MAAM,EAAE,CAAC;AAEpC,KAAK,WAAW,GAAG;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,CAAC,UAAU,GAAG,UAAU,EAAE,CAAC,EAAE,CAAC;CACvC,CAAC;AAEF,eAAO,MAAM,YAAY,EAAE,MAAM,CAAC,EAAE,EAAE,KAAK,EAAE,MAAM,CASlD,CAAC;AAEF,wBAAgB,MAAM,CAAC,IAAI,EAAE,KAAK,GAAG,WAAW,EAAE,CAkBjD"}
package/dist/index.js CHANGED
@@ -1,5 +1,4 @@
1
1
  // src/index.ts
2
- import { visitParents } from "unist-util-visit-parents";
3
2
  var hl7v2Jsonify = function() {
4
3
  const self = this;
5
4
  function compiler(tree) {
@@ -8,44 +7,69 @@ var hl7v2Jsonify = function() {
8
7
  self.compiler = compiler;
9
8
  };
10
9
  function toJson(root) {
11
- const segments = [];
12
- let currentSegment = null;
13
- visitParents(root, (node, ancestors) => {
14
- if (node.type === "segment") {
15
- currentSegment = { segment: node.name || "UNKNOWN", fields: [] };
16
- segments.push(currentSegment);
17
- }
18
- if (node.value !== void 0 && currentSegment) {
19
- const path = [...ancestors, node].filter(
20
- (n) => n.index !== void 0 && n.type !== "segment" && n.type !== "root"
21
- ).map((n) => {
22
- if (typeof n.index !== "number") {
23
- throw new Error("HL7 AST node is missing a numeric index");
24
- }
25
- return n.index - 1;
26
- });
27
- setNestedArrayValue(currentSegment.fields, path, node.value);
10
+ const r = root;
11
+ const out = [];
12
+ for (const s of r.children) {
13
+ const segmentName = getSegmentName(s);
14
+ const fields = [];
15
+ for (let i = 1; i < s.children.length; i++) {
16
+ const f = s.children[i];
17
+ fields.push(materializeField(f));
28
18
  }
29
- });
30
- return segments;
19
+ out.push({ segment: segmentName, fields });
20
+ }
21
+ return out;
31
22
  }
32
- function setNestedArrayValue(arr, path, value) {
33
- let current = arr;
34
- for (let i = 0; i < path.length - 1; i++) {
35
- const idx = path[i];
36
- if (idx === void 0) {
37
- return;
23
+ function getSegmentName(segment) {
24
+ try {
25
+ const firstField = segment.children[0];
26
+ if (!firstField) {
27
+ return "UNKNOWN";
28
+ }
29
+ const firstRepetition = firstField.children[0];
30
+ if (!firstRepetition) {
31
+ return "UNKNOWN";
38
32
  }
39
- if (!current[idx]) {
40
- current[idx] = [];
33
+ const firstComponent = firstRepetition.children[0];
34
+ if (!firstComponent) {
35
+ return "UNKNOWN";
41
36
  }
42
- current = current[idx];
37
+ const firstSubcomponent = firstComponent.children[0];
38
+ if (!firstSubcomponent) {
39
+ return "UNKNOWN";
40
+ }
41
+ return firstSubcomponent.value || "UNKNOWN";
42
+ } catch {
43
+ return "UNKNOWN";
43
44
  }
44
- const lastKey = path.at(-1);
45
- if (lastKey === void 0) {
46
- return;
45
+ }
46
+ function materializeField(field) {
47
+ const toComponent = (c) => {
48
+ return c.children.length === 1 ? c.children[0].value : c.children.map((sc) => sc.value);
49
+ };
50
+ const toRepetitionArray = (r) => {
51
+ return r.children.map((c) => toComponent(c));
52
+ };
53
+ const repetitions = field.children;
54
+ if (repetitions.length === 1) {
55
+ const rep = repetitions[0];
56
+ if (!rep) {
57
+ throw new Error("Expected a field repetition");
58
+ }
59
+ if (rep.children.length === 1) {
60
+ const comp = rep.children[0];
61
+ const compVal = toComponent(comp);
62
+ return Array.isArray(compVal) ? [compVal] : compVal;
63
+ }
64
+ return toRepetitionArray(rep);
47
65
  }
48
- current[lastKey] = value;
66
+ return repetitions.map((r) => {
67
+ const rep = r;
68
+ if (rep.children.length === 1) {
69
+ return toComponent(rep.children[0]);
70
+ }
71
+ return toRepetitionArray(rep);
72
+ });
49
73
  }
50
74
  export {
51
75
  hl7v2Jsonify,
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { HL7v2Node } from '@rethinkhealth/hl7v2-ast';\nimport type { Plugin } from 'unified';\nimport type { Node } from 'unist';\nimport { visitParents } from 'unist-util-visit-parents';\n\ntype SegmentJSON = {\n segment: string;\n fields: (string | string[])[]; // Nested arrays for fields/repetitions/components\n};\n\nexport const hl7v2Jsonify: Plugin<[], HL7v2Node, string> = function (): void {\n // biome-ignore lint/complexity/noUselessThisAlias: this is a plugin\n const self = this;\n\n function compiler(tree: Node): string {\n return JSON.stringify(toJson(tree as HL7v2Node), null, 2);\n }\n\n self.compiler = compiler;\n};\n\nexport function toJson(root: HL7v2Node): SegmentJSON[] {\n const segments: SegmentJSON[] = [];\n let currentSegment: SegmentJSON | null = null;\n\n visitParents<HL7v2Node, HL7v2Node>(root, (node, ancestors) => {\n if (node.type === 'segment') {\n currentSegment = { segment: node.name || 'UNKNOWN', fields: [] };\n segments.push(currentSegment);\n }\n\n if (node.value !== undefined && currentSegment) {\n // Build numeric path for all indices except segment/root\n const path = [...ancestors, node]\n .filter(\n (n) =>\n n.index !== undefined && n.type !== 'segment' && n.type !== 'root'\n )\n .map((n) => {\n if (typeof n.index !== 'number') {\n throw new Error('HL7 AST node is missing a numeric index');\n }\n return n.index - 1;\n });\n\n setNestedArrayValue(currentSegment.fields, path, node.value);\n }\n });\n\n return segments;\n}\n\n/**\n * Recursively create nested arrays along the path and set the value.\n */\nfunction setNestedArrayValue(\n arr: (string | string[])[],\n path: number[],\n value: string\n) {\n let current: (string | string[])[] = arr;\n for (let i = 0; i < path.length - 1; i++) {\n const idx = path[i];\n\n if (idx === undefined) {\n return;\n }\n\n if (!current[idx]) {\n current[idx] = [];\n }\n current = current[idx] as (string | string[])[];\n }\n\n const lastKey = path.at(-1);\n if (lastKey === undefined) {\n return;\n }\n\n current[lastKey] = value;\n}\n"],"mappings":";AAGA,SAAS,oBAAoB;AAOtB,IAAM,eAA8C,WAAkB;AAE3E,QAAM,OAAO;AAEb,WAAS,SAAS,MAAoB;AACpC,WAAO,KAAK,UAAU,OAAO,IAAiB,GAAG,MAAM,CAAC;AAAA,EAC1D;AAEA,OAAK,WAAW;AAClB;AAEO,SAAS,OAAO,MAAgC;AACrD,QAAM,WAA0B,CAAC;AACjC,MAAI,iBAAqC;AAEzC,eAAmC,MAAM,CAAC,MAAM,cAAc;AAC5D,QAAI,KAAK,SAAS,WAAW;AAC3B,uBAAiB,EAAE,SAAS,KAAK,QAAQ,WAAW,QAAQ,CAAC,EAAE;AAC/D,eAAS,KAAK,cAAc;AAAA,IAC9B;AAEA,QAAI,KAAK,UAAU,UAAa,gBAAgB;AAE9C,YAAM,OAAO,CAAC,GAAG,WAAW,IAAI,EAC7B;AAAA,QACC,CAAC,MACC,EAAE,UAAU,UAAa,EAAE,SAAS,aAAa,EAAE,SAAS;AAAA,MAChE,EACC,IAAI,CAAC,MAAM;AACV,YAAI,OAAO,EAAE,UAAU,UAAU;AAC/B,gBAAM,IAAI,MAAM,yCAAyC;AAAA,QAC3D;AACA,eAAO,EAAE,QAAQ;AAAA,MACnB,CAAC;AAEH,0BAAoB,eAAe,QAAQ,MAAM,KAAK,KAAK;AAAA,IAC7D;AAAA,EACF,CAAC;AAED,SAAO;AACT;AAKA,SAAS,oBACP,KACA,MACA,OACA;AACA,MAAI,UAAiC;AACrC,WAAS,IAAI,GAAG,IAAI,KAAK,SAAS,GAAG,KAAK;AACxC,UAAM,MAAM,KAAK,CAAC;AAElB,QAAI,QAAQ,QAAW;AACrB;AAAA,IACF;AAEA,QAAI,CAAC,QAAQ,GAAG,GAAG;AACjB,cAAQ,GAAG,IAAI,CAAC;AAAA,IAClB;AACA,cAAU,QAAQ,GAAG;AAAA,EACvB;AAEA,QAAM,UAAU,KAAK,GAAG,EAAE;AAC1B,MAAI,YAAY,QAAW;AACzB;AAAA,EACF;AAEA,UAAQ,OAAO,IAAI;AACrB;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type {\n Component,\n Field,\n FieldRepetition,\n Nodes,\n Root,\n Segment,\n Subcomponent,\n} from '@rethinkhealth/hl7v2-ast';\nimport type { Plugin } from 'unified';\nimport type { Node } from 'unist';\n\ntype FieldValue = string | string[];\n\ntype SegmentJSON = {\n segment: string;\n fields: (FieldValue | FieldValue[])[]; // Nested arrays for fields/repetitions/components\n};\n\nexport const hl7v2Jsonify: Plugin<[], Nodes, string> = function (): void {\n // biome-ignore lint/complexity/noUselessThisAlias: this is a plugin\n const self = this;\n\n function compiler(tree: Node): string {\n return JSON.stringify(toJson(tree as Nodes), null, 2);\n }\n\n self.compiler = compiler;\n};\n\nexport function toJson(root: Nodes): SegmentJSON[] {\n const r = root as Root;\n const out: SegmentJSON[] = [];\n\n for (const s of r.children as Segment[]) {\n const segmentName = getSegmentName(s);\n const fields: (FieldValue | FieldValue[])[] = [];\n\n // Skip the header field (index 0) when projecting to fields\n for (let i = 1; i < s.children.length; i++) {\n const f = s.children[i] as Field;\n fields.push(materializeField(f));\n }\n\n out.push({ segment: segmentName, fields });\n }\n\n return out;\n}\n\n// Extract segment name from the first field's first component's first subcomponent\nfunction getSegmentName(segment: Segment): string {\n try {\n // Navigate: segment -> field[0] -> fieldRepetition[0] -> component[0] -> subcomponent[0]\n const firstField = segment.children[0] as Field | undefined;\n if (!firstField) {\n return 'UNKNOWN';\n }\n\n const firstRepetition = firstField.children[0] as\n | FieldRepetition\n | undefined;\n if (!firstRepetition) {\n return 'UNKNOWN';\n }\n\n const firstComponent = firstRepetition.children[0] as Component | undefined;\n if (!firstComponent) {\n return 'UNKNOWN';\n }\n\n const firstSubcomponent = firstComponent.children[0] as\n | Subcomponent\n | undefined;\n if (!firstSubcomponent) {\n return 'UNKNOWN';\n }\n\n return firstSubcomponent.value || 'UNKNOWN';\n } catch {\n return 'UNKNOWN';\n }\n}\n\n// Convert a Field into JSON-friendly value: string or nested arrays representing reps/components/subcomponents\nfunction materializeField(field: Field): FieldValue | FieldValue[] {\n const toComponent = (c: Component): FieldValue => {\n return c.children.length === 1\n ? (c.children[0] as Subcomponent).value\n : c.children.map((sc) => (sc as Subcomponent).value);\n };\n\n const toRepetitionArray = (r: FieldRepetition): FieldValue[] => {\n return r.children.map((c) => toComponent(c as Component));\n };\n\n const repetitions = field.children as FieldRepetition[];\n\n if (repetitions.length === 1) {\n const rep = repetitions[0];\n\n if (!rep) {\n throw new Error('Expected a field repetition');\n }\n\n if (rep.children.length === 1) {\n const comp = rep.children[0] as Component;\n const compVal = toComponent(comp);\n\n return Array.isArray(compVal) ? [compVal] : compVal;\n }\n return toRepetitionArray(rep);\n }\n\n // Multiple repetitions: project each repetition to a value (string or array)\n return repetitions.map((r) => {\n const rep = r as FieldRepetition;\n if (rep.children.length === 1) {\n return toComponent(rep.children[0] as Component);\n }\n return toRepetitionArray(rep);\n }) as string[];\n}\n"],"mappings":";AAmBO,IAAM,eAA0C,WAAkB;AAEvE,QAAM,OAAO;AAEb,WAAS,SAAS,MAAoB;AACpC,WAAO,KAAK,UAAU,OAAO,IAAa,GAAG,MAAM,CAAC;AAAA,EACtD;AAEA,OAAK,WAAW;AAClB;AAEO,SAAS,OAAO,MAA4B;AACjD,QAAM,IAAI;AACV,QAAM,MAAqB,CAAC;AAE5B,aAAW,KAAK,EAAE,UAAuB;AACvC,UAAM,cAAc,eAAe,CAAC;AACpC,UAAM,SAAwC,CAAC;AAG/C,aAAS,IAAI,GAAG,IAAI,EAAE,SAAS,QAAQ,KAAK;AAC1C,YAAM,IAAI,EAAE,SAAS,CAAC;AACtB,aAAO,KAAK,iBAAiB,CAAC,CAAC;AAAA,IACjC;AAEA,QAAI,KAAK,EAAE,SAAS,aAAa,OAAO,CAAC;AAAA,EAC3C;AAEA,SAAO;AACT;AAGA,SAAS,eAAe,SAA0B;AAChD,MAAI;AAEF,UAAM,aAAa,QAAQ,SAAS,CAAC;AACrC,QAAI,CAAC,YAAY;AACf,aAAO;AAAA,IACT;AAEA,UAAM,kBAAkB,WAAW,SAAS,CAAC;AAG7C,QAAI,CAAC,iBAAiB;AACpB,aAAO;AAAA,IACT;AAEA,UAAM,iBAAiB,gBAAgB,SAAS,CAAC;AACjD,QAAI,CAAC,gBAAgB;AACnB,aAAO;AAAA,IACT;AAEA,UAAM,oBAAoB,eAAe,SAAS,CAAC;AAGnD,QAAI,CAAC,mBAAmB;AACtB,aAAO;AAAA,IACT;AAEA,WAAO,kBAAkB,SAAS;AAAA,EACpC,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAGA,SAAS,iBAAiB,OAAyC;AACjE,QAAM,cAAc,CAAC,MAA6B;AAChD,WAAO,EAAE,SAAS,WAAW,IACxB,EAAE,SAAS,CAAC,EAAmB,QAChC,EAAE,SAAS,IAAI,CAAC,OAAQ,GAAoB,KAAK;AAAA,EACvD;AAEA,QAAM,oBAAoB,CAAC,MAAqC;AAC9D,WAAO,EAAE,SAAS,IAAI,CAAC,MAAM,YAAY,CAAc,CAAC;AAAA,EAC1D;AAEA,QAAM,cAAc,MAAM;AAE1B,MAAI,YAAY,WAAW,GAAG;AAC5B,UAAM,MAAM,YAAY,CAAC;AAEzB,QAAI,CAAC,KAAK;AACR,YAAM,IAAI,MAAM,6BAA6B;AAAA,IAC/C;AAEA,QAAI,IAAI,SAAS,WAAW,GAAG;AAC7B,YAAM,OAAO,IAAI,SAAS,CAAC;AAC3B,YAAM,UAAU,YAAY,IAAI;AAEhC,aAAO,MAAM,QAAQ,OAAO,IAAI,CAAC,OAAO,IAAI;AAAA,IAC9C;AACA,WAAO,kBAAkB,GAAG;AAAA,EAC9B;AAGA,SAAO,YAAY,IAAI,CAAC,MAAM;AAC5B,UAAM,MAAM;AACZ,QAAI,IAAI,SAAS,WAAW,GAAG;AAC7B,aAAO,YAAY,IAAI,SAAS,CAAC,CAAc;AAAA,IACjD;AACA,WAAO,kBAAkB,GAAG;AAAA,EAC9B,CAAC;AACH;","names":[]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rethinkhealth/hl7v2-jsonify",
3
3
  "description": "hl7v2 plugin to transform hl7v2 messages to a simplified JSON representation",
4
- "version": "0.2.2",
4
+ "version": "0.2.4",
5
5
  "license": "MIT",
6
6
  "author": {
7
7
  "name": "Melek Somai",
@@ -29,8 +29,8 @@
29
29
  "typescript": "^5.8.3",
30
30
  "vitest": "^3.2.4",
31
31
  "@rethinkhealth/testing": "0.0.0",
32
- "@rethinkhealth/hl7v2-ast": "0.2.2",
33
- "@rethinkhealth/tsconfig": "0.0.0"
32
+ "@rethinkhealth/tsconfig": "0.0.0",
33
+ "@rethinkhealth/hl7v2-ast": "0.2.4"
34
34
  },
35
35
  "repository": "rethinkhealth/hl7v2.git",
36
36
  "homepage": "https://www.rethinkhealth.io/hl7v2/docs",