@rethinkhealth/hl7v2-jsonify 0.2.19 → 0.2.21
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.js +2 -8
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -10,21 +10,15 @@ function toJson(root) {
|
|
|
10
10
|
const r = root;
|
|
11
11
|
const out = [];
|
|
12
12
|
for (const s of r.children) {
|
|
13
|
-
const segmentName =
|
|
13
|
+
const segmentName = s.children[0].value;
|
|
14
14
|
const fields = [];
|
|
15
|
-
for (const f of s.children) {
|
|
15
|
+
for (const f of s.children.slice(1)) {
|
|
16
16
|
fields.push(materializeField(f));
|
|
17
17
|
}
|
|
18
18
|
out.push({ segment: segmentName, fields });
|
|
19
19
|
}
|
|
20
20
|
return out;
|
|
21
21
|
}
|
|
22
|
-
function getSegmentName(segment) {
|
|
23
|
-
if (segment.name) {
|
|
24
|
-
return segment.name;
|
|
25
|
-
}
|
|
26
|
-
return "UNKNOWN";
|
|
27
|
-
}
|
|
28
22
|
function materializeField(field) {
|
|
29
23
|
const toComponent = (c) => {
|
|
30
24
|
if (c.children?.length === 0) {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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<[], Root, 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 =
|
|
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<[], Root, 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 = s.children[0].value;\n const fields: (FieldValue | FieldValue[])[] = [];\n\n // Skip the header field (index 0) when projecting to fields\n for (const f of s.children.slice(1) as Field[]) {\n fields.push(materializeField(f));\n }\n\n out.push({ segment: segmentName, fields });\n }\n\n return out;\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 if (c.children?.length === 0) {\n return \"\";\n }\n if (c.children?.length === 1) {\n return (c.children[0] as Subcomponent).value;\n }\n return c.children ? c.children.map((sc) => (sc as Subcomponent).value) : [];\n };\n\n const toRepetitionArray = (r: FieldRepetition): FieldValue[] =>\n r.children.map((c) => toComponent(c as Component));\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,eAAyC,WAAkB;AAEtE,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,SAAS,CAAC,EAAE;AAClC,UAAM,SAAwC,CAAC;AAG/C,eAAW,KAAK,EAAE,SAAS,MAAM,CAAC,GAAc;AAC9C,aAAO,KAAK,iBAAiB,CAAC,CAAC;AAAA,IACjC;AAEA,QAAI,KAAK,EAAE,SAAS,aAAa,OAAO,CAAC;AAAA,EAC3C;AAEA,SAAO;AACT;AAGA,SAAS,iBAAiB,OAAyC;AACjE,QAAM,cAAc,CAAC,MAA6B;AAChD,QAAI,EAAE,UAAU,WAAW,GAAG;AAC5B,aAAO;AAAA,IACT;AACA,QAAI,EAAE,UAAU,WAAW,GAAG;AAC5B,aAAQ,EAAE,SAAS,CAAC,EAAmB;AAAA,IACzC;AACA,WAAO,EAAE,WAAW,EAAE,SAAS,IAAI,CAAC,OAAQ,GAAoB,KAAK,IAAI,CAAC;AAAA,EAC5E;AAEA,QAAM,oBAAoB,CAAC,MACzB,EAAE,SAAS,IAAI,CAAC,MAAM,YAAY,CAAc,CAAC;AAEnD,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.
|
|
4
|
+
"version": "0.2.21",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "Melek Somai",
|
|
@@ -27,10 +27,10 @@
|
|
|
27
27
|
"tsup": "8.5.0",
|
|
28
28
|
"typescript": "^5.8.3",
|
|
29
29
|
"vitest": "^3.2.4",
|
|
30
|
-
"@rethinkhealth/hl7v2-ast": "0.2.
|
|
31
|
-
"@rethinkhealth/hl7v2-builder": "0.2.
|
|
32
|
-
"@rethinkhealth/
|
|
33
|
-
"@rethinkhealth/
|
|
30
|
+
"@rethinkhealth/hl7v2-ast": "0.2.21",
|
|
31
|
+
"@rethinkhealth/hl7v2-builder": "0.2.21",
|
|
32
|
+
"@rethinkhealth/tsconfig": "0.0.1",
|
|
33
|
+
"@rethinkhealth/testing": "0.0.1"
|
|
34
34
|
},
|
|
35
35
|
"repository": "rethinkhealth/hl7v2.git",
|
|
36
36
|
"homepage": "https://www.rethinkhealth.io/hl7v2/docs",
|