@glion/jsonify 0.14.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Rethink Health
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,128 @@
1
+ # @glion/jsonify
2
+
3
+ **[unified](https://unifiedjs.com/)** plugin to transform HL7v2 message ASTs into a simplified JSON representation.
4
+
5
+ ## What is this?
6
+
7
+ `@glion/jsonify` is a [unified](https://unifiedjs.com/) plugin that takes an HL7v2 syntax tree (produced by a parser such as [`@glion/ast`][https://github.com/rethinkhealth/glion/tree/main/packages/hl7v2-parser]) and compiles it to a clean, structured JSON format.
8
+
9
+ It strips away [unist](https://github.com/syntax-tree/unist) metadata and preserves only meaningful HL7v2 properties such as segment type, field indexes, and values.
10
+
11
+ ## When should I use this?
12
+
13
+ Use this plugin when you need:
14
+
15
+ - A minimal JSON representation of HL7v2 messages for downstream processing.
16
+ - To serialize HL7v2 ASTs into a format suitable for APIs or storage.
17
+
18
+ If you need to parse raw HL7v2 messages first, use [`@glion/ast`](https://github.com/rethinkhealth/glion/tree/main/packages/hl7v2-parser) before applying this plugin.
19
+
20
+ ## Install
21
+
22
+ This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).
23
+
24
+ In Node.js (version 16+), install with [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm):
25
+
26
+ ```sh
27
+ npm install @glion/jsonify
28
+ ```
29
+
30
+ ## Use
31
+
32
+ Say we have an HL7v2 message and want to convert it to JSON:
33
+
34
+ ```js
35
+ import { unified } from "unified";
36
+ import { hl7v2Parse } from "@glion/ast";
37
+ import { hl7v2Jsonify } from "@glion/jsonify";
38
+
39
+ const msg = `MSH|^~\\&|HIS|RIH|EKG|EKG|200202150930||ADT^A01|MSG00001|P|2.4\rPID|||555-44-4444||DOE^JOHN`;
40
+
41
+ const file = await unified().use(hl7v2Parse).use(hl7v2Jsonify).process(msg);
42
+
43
+ console.log(String(file));
44
+ ```
45
+
46
+ Yields:
47
+
48
+ ```json
49
+ {
50
+ "type": "Message",
51
+ "children": [
52
+ {
53
+ "type": "Segment",
54
+ "name": "MSH",
55
+ "children": [
56
+ { "type": "Field", "name": "MSH-1", "value": "|" },
57
+ { "type": "Field", "name": "MSH-2", "value": "^~\\&" },
58
+ ...
59
+ ]
60
+ },
61
+ {
62
+ "type": "Segment",
63
+ "name": "PID",
64
+ "children": [
65
+ { "type": "Field", "name": "PID-3", "value": "555-44-4444" },
66
+ { "type": "Field", "name": "PID-5", "value": "DOE^JOHN" }
67
+ ]
68
+ }
69
+ ]
70
+ }
71
+ ```
72
+
73
+ ## API
74
+
75
+ ### `unified().use(hl7v2Jsonify)`
76
+
77
+ Transform an HL7v2 AST into JSON.
78
+
79
+ ###### Parameters
80
+
81
+ There are no options.
82
+
83
+ ###### Returns
84
+
85
+ Nothing (`undefined`). The unified compiler returns a stringified JSON document.
86
+
87
+ ## JSON Schema
88
+
89
+ The output JSON has the following shape:
90
+
91
+ ```ts
92
+ interface HL7v2JsonNode {
93
+ type: string;
94
+ name?: string;
95
+ index?: number;
96
+ value?: string;
97
+ delimiter?: string;
98
+ children?: HL7v2JsonNode[];
99
+ }
100
+ ```
101
+
102
+ ## Security
103
+
104
+ This plugin only transforms AST nodes and does not execute code. Ensure you trust the source of HL7v2 messages before processing.
105
+
106
+ ## Contributing
107
+
108
+ We welcome contributions! Please see our [Contributing Guide][github-contributing] for more details.
109
+
110
+ 1. Fork the repository
111
+ 2. Create your feature branch (`git checkout -b feature/amazing-feature`)
112
+ 3. Commit your changes (`git commit -m 'Add some amazing feature'`)
113
+ 4. Push to the branch (`git push origin feature/amazing-feature`)
114
+ 5. Open a Pull Request
115
+
116
+ ## Code of Conduct
117
+
118
+ To ensure a welcoming and positive environment, we have a [Code of Conduct][github-code-of-conduct] that all contributors and participants are expected to adhere to.
119
+
120
+ ## License
121
+
122
+ Copyright 2025 Rethink Health, SUARL. All rights reserved.
123
+
124
+ This program is licensed to you under the terms of the [MIT License](https://opensource.org/licenses/MIT). This program is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the [LICENSE][github-license] file for details.
125
+
126
+ [github-code-of-conduct]: https://github.com/rethinkhealth/glion/blob/main/CODE_OF_CONDUCT.md
127
+ [github-license]: https://github.com/rethinkhealth/glion/blob/main/LICENSE
128
+ [github-contributing]: https://github.com/rethinkhealth/glion/blob/main/CONTRIBUTING.md
@@ -0,0 +1,4 @@
1
+ export { hl7v2Jsonify } from "./processor";
2
+ export { toJsonRuntime } from "./runtime";
3
+ export type { FieldJson, GroupJson, Hl7v2JsonResult, SegmentJson, } from "./types";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAC1C,YAAY,EACV,SAAS,EACT,SAAS,EACT,eAAe,EACf,WAAW,GACZ,MAAM,SAAS,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,62 @@
1
+ //#region src/runtime.ts
2
+ function toJsonRuntime(root) {
3
+ const r = root;
4
+ const out = [];
5
+ for (const child of r.children) if (child.type === "segment") out.push(processSegment(child));
6
+ else if (child.type === "group") out.push(processGroup(child));
7
+ return out;
8
+ }
9
+ function processSegment(segment) {
10
+ const fields = [];
11
+ for (const f of segment.children) fields.push(materializeField(f));
12
+ return {
13
+ fields,
14
+ segment: segment.name
15
+ };
16
+ }
17
+ function processGroup(group) {
18
+ const children = [];
19
+ for (const child of group.children) if (child.type === "segment") children.push(processSegment(child));
20
+ else if (child.type === "group") children.push(processGroup(child));
21
+ return {
22
+ children,
23
+ group: group.name ?? ""
24
+ };
25
+ }
26
+ function materializeField(field) {
27
+ const toComponent = (c) => {
28
+ if (c.children?.length === 0) return "";
29
+ if (c.children?.length === 1) return c.children[0].value;
30
+ return c.children ? c.children.map((sc) => sc.value) : [];
31
+ };
32
+ const toRepetitionArray = (r) => r.children.map((c) => toComponent(c));
33
+ const repetitions = field.children;
34
+ if (repetitions.length === 1) {
35
+ const rep = repetitions[0];
36
+ if (!rep) throw new Error("Expected a field repetition");
37
+ if (rep.children.length === 1) {
38
+ const comp = rep.children[0];
39
+ const compVal = toComponent(comp);
40
+ return Array.isArray(compVal) ? [compVal] : compVal;
41
+ }
42
+ return toRepetitionArray(rep);
43
+ }
44
+ return repetitions.map((r) => {
45
+ const rep = r;
46
+ if (rep.children.length === 1) return toComponent(rep.children[0]);
47
+ return toRepetitionArray(rep);
48
+ });
49
+ }
50
+ //#endregion
51
+ //#region src/processor.ts
52
+ function hl7v2Jsonify() {
53
+ const self = this;
54
+ function compiler(tree) {
55
+ return toJsonRuntime(tree);
56
+ }
57
+ self.compiler = compiler;
58
+ }
59
+ //#endregion
60
+ export { hl7v2Jsonify, toJsonRuntime };
61
+
62
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/runtime.ts","../src/processor.ts"],"sourcesContent":["import type {\n Component,\n Field,\n FieldRepetition,\n Group,\n Nodes,\n Root,\n Segment,\n Subcomponent,\n} from \"@glion/ast\";\n\nimport type {\n FieldJson,\n GroupJson,\n Hl7v2JsonResult,\n SegmentJson,\n} from \"./types\";\n\nexport function toJsonRuntime(root: Nodes): Hl7v2JsonResult {\n const r = root as Root;\n const out: Hl7v2JsonResult = [];\n\n for (const child of r.children) {\n if (child.type === \"segment\") {\n out.push(processSegment(child as Segment));\n } else if (child.type === \"group\") {\n out.push(processGroup(child as Group));\n }\n }\n\n return out;\n}\n\nfunction processSegment(segment: Segment): SegmentJson {\n const fields: (FieldJson | FieldJson[])[] = [];\n\n for (const f of segment.children) {\n fields.push(materializeField(f));\n }\n\n return { fields, segment: segment.name };\n}\n\nfunction processGroup(group: Group): GroupJson {\n const children: (SegmentJson | GroupJson)[] = [];\n\n for (const child of group.children) {\n if (child.type === \"segment\") {\n children.push(processSegment(child as Segment));\n } else if (child.type === \"group\") {\n children.push(processGroup(child as Group));\n }\n }\n\n return { children, group: group.name ?? \"\" };\n}\n\n// Convert a Field into JSON-friendly value: string or nested arrays representing reps/components/subcomponents\nfunction materializeField(field: Field): FieldJson | FieldJson[] {\n const toComponent = (c: Component): FieldJson => {\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): FieldJson[] =>\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","import type { Node, Nodes, Root } from \"@glion/ast\";\nimport type { Processor } from \"unified\";\n\nimport { toJsonRuntime } from \"./runtime\";\nimport type { Hl7v2JsonResult } from \"./types\";\n\nexport function hl7v2Jsonify(this: Processor) {\n const self = this as unknown as Processor<\n Node,\n undefined,\n undefined,\n Root,\n undefined\n >;\n\n function compiler(tree: Nodes): Hl7v2JsonResult {\n return toJsonRuntime(tree);\n }\n\n self.compiler = compiler as unknown as typeof self.compiler;\n}\n"],"mappings":";AAkBA,SAAgB,cAAc,MAA8B;CAC1D,MAAM,IAAI;CACV,MAAM,MAAuB,EAAE;AAE/B,MAAK,MAAM,SAAS,EAAE,SACpB,KAAI,MAAM,SAAS,UACjB,KAAI,KAAK,eAAe,MAAiB,CAAC;UACjC,MAAM,SAAS,QACxB,KAAI,KAAK,aAAa,MAAe,CAAC;AAI1C,QAAO;;AAGT,SAAS,eAAe,SAA+B;CACrD,MAAM,SAAsC,EAAE;AAE9C,MAAK,MAAM,KAAK,QAAQ,SACtB,QAAO,KAAK,iBAAiB,EAAE,CAAC;AAGlC,QAAO;EAAE;EAAQ,SAAS,QAAQ;EAAM;;AAG1C,SAAS,aAAa,OAAyB;CAC7C,MAAM,WAAwC,EAAE;AAEhD,MAAK,MAAM,SAAS,MAAM,SACxB,KAAI,MAAM,SAAS,UACjB,UAAS,KAAK,eAAe,MAAiB,CAAC;UACtC,MAAM,SAAS,QACxB,UAAS,KAAK,aAAa,MAAe,CAAC;AAI/C,QAAO;EAAE;EAAU,OAAO,MAAM,QAAQ;EAAI;;AAI9C,SAAS,iBAAiB,OAAuC;CAC/D,MAAM,eAAe,MAA4B;AAC/C,MAAI,EAAE,UAAU,WAAW,EACzB,QAAO;AAET,MAAI,EAAE,UAAU,WAAW,EACzB,QAAQ,EAAE,SAAS,GAAoB;AAEzC,SAAO,EAAE,WAAW,EAAE,SAAS,KAAK,OAAQ,GAAoB,MAAM,GAAG,EAAE;;CAG7E,MAAM,qBAAqB,MACzB,EAAE,SAAS,KAAK,MAAM,YAAY,EAAe,CAAC;CAEpD,MAAM,cAAc,MAAM;AAE1B,KAAI,YAAY,WAAW,GAAG;EAC5B,MAAM,MAAM,YAAY;AAExB,MAAI,CAAC,IACH,OAAM,IAAI,MAAM,8BAA8B;AAGhD,MAAI,IAAI,SAAS,WAAW,GAAG;GAC7B,MAAM,OAAO,IAAI,SAAS;GAC1B,MAAM,UAAU,YAAY,KAAK;AAEjC,UAAO,MAAM,QAAQ,QAAQ,GAAG,CAAC,QAAQ,GAAG;;AAE9C,SAAO,kBAAkB,IAAI;;AAI/B,QAAO,YAAY,KAAK,MAAM;EAC5B,MAAM,MAAM;AACZ,MAAI,IAAI,SAAS,WAAW,EAC1B,QAAO,YAAY,IAAI,SAAS,GAAgB;AAElD,SAAO,kBAAkB,IAAI;GAC7B;;;;AC3FJ,SAAgB,eAA8B;CAC5C,MAAM,OAAO;CAQb,SAAS,SAAS,MAA8B;AAC9C,SAAO,cAAc,KAAK;;AAG5B,MAAK,WAAW"}
@@ -0,0 +1,3 @@
1
+ import type { Processor } from "unified";
2
+ export declare function hl7v2Jsonify(this: Processor): void;
3
+ //# sourceMappingURL=processor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"processor.d.ts","sourceRoot":"","sources":["../src/processor.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAKzC,wBAAgB,YAAY,CAAC,IAAI,EAAE,SAAS,QAc3C"}
@@ -0,0 +1,4 @@
1
+ import type { Nodes } from "@glion/ast";
2
+ import type { Hl7v2JsonResult } from "./types";
3
+ export declare function toJsonRuntime(root: Nodes): Hl7v2JsonResult;
4
+ //# sourceMappingURL=runtime.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAKV,KAAK,EAIN,MAAM,YAAY,CAAC;AAEpB,OAAO,KAAK,EAGV,eAAe,EAEhB,MAAM,SAAS,CAAC;AAEjB,wBAAgB,aAAa,CAAC,IAAI,EAAE,KAAK,GAAG,eAAe,CAa1D"}
@@ -0,0 +1,11 @@
1
+ export type FieldJson = string | string[];
2
+ export interface SegmentJson {
3
+ segment: string;
4
+ fields: (FieldJson | FieldJson[])[];
5
+ }
6
+ export interface GroupJson {
7
+ group: string;
8
+ children: (SegmentJson | GroupJson)[];
9
+ }
10
+ export type Hl7v2JsonResult = (SegmentJson | GroupJson)[];
11
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,MAAM,EAAE,CAAC;AAE1C,MAAM,WAAW,WAAW;IAC1B,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,CAAC,SAAS,GAAG,SAAS,EAAE,CAAC,EAAE,CAAC;CACrC;AAED,MAAM,WAAW,SAAS;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,CAAC,WAAW,GAAG,SAAS,CAAC,EAAE,CAAC;CACvC;AAED,MAAM,MAAM,eAAe,GAAG,CAAC,WAAW,GAAG,SAAS,CAAC,EAAE,CAAC"}
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@glion/jsonify",
3
+ "version": "0.14.1",
4
+ "description": "hl7v2 plugin to transform hl7v2 messages to a simplified JSON representation",
5
+ "keywords": [
6
+ "health",
7
+ "healthcare",
8
+ "hl7",
9
+ "hl7v2",
10
+ "nodejs",
11
+ "typescript"
12
+ ],
13
+ "homepage": "https://glion.dev",
14
+ "license": "MIT",
15
+ "author": {
16
+ "name": "Melek Somai",
17
+ "email": "melek@rethinkhealth.io"
18
+ },
19
+ "repository": "rethinkhealth/glion.git",
20
+ "files": [
21
+ "dist"
22
+ ],
23
+ "type": "module",
24
+ "types": "./dist/index.d.ts",
25
+ "exports": {
26
+ ".": "./dist/index.js"
27
+ },
28
+ "publishConfig": {
29
+ "access": "public"
30
+ },
31
+ "dependencies": {
32
+ "unified": "11.0.5",
33
+ "@glion/ast": "0.14.1"
34
+ },
35
+ "devDependencies": {
36
+ "@types/node": "^25.5.0",
37
+ "@types/unist": "^3.0.3",
38
+ "@vitest/coverage-v8": "4.1.2",
39
+ "tsdown": "0.21.7",
40
+ "typescript": "^5.9.3",
41
+ "vitest": "4.1.2",
42
+ "@glion/builder": "0.14.1",
43
+ "@glion/testing": "0.0.2",
44
+ "@glion/tsconfig": "0.0.1"
45
+ },
46
+ "engines": {
47
+ "node": ">=18"
48
+ },
49
+ "scripts": {
50
+ "build": "tsdown && tsc --emitDeclarationOnly",
51
+ "check-types": "tsc --noEmit",
52
+ "test": "vitest run",
53
+ "test:coverage": "vitest run --coverage",
54
+ "test:watch": "vitest"
55
+ }
56
+ }