@rethinkhealth/hl7v2-jsonify 0.2.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 +21 -0
- package/README.md +128 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +54 -0
- package/dist/index.js.map +1 -0
- package/package.json +56 -0
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
|
+
# @rethinkhealth/hl7v2-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
|
+
`@rethinkhealth/hl7v2-jsonify` is a [unified](https://unifiedjs.com/) plugin that takes an HL7v2 syntax tree (produced by a parser such as [`@rethinkhealth/hl7v2-ast`][https://github.com/rethinkhealth/hl7v2/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 [`@rethinkhealth/hl7v2-ast`](https://github.com/rethinkhealth/hl7v2/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 @rethinkhealth/hl7v2-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 '@rethinkhealth/hl7v2-ast'
|
|
37
|
+
import { hl7v2Jsonify } from '@rethinkhealth/hl7v2-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()
|
|
42
|
+
.use(hl7v2Parse)
|
|
43
|
+
.use(hl7v2Jsonify)
|
|
44
|
+
.process(msg)
|
|
45
|
+
|
|
46
|
+
console.log(String(file))
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Yields:
|
|
50
|
+
|
|
51
|
+
```json
|
|
52
|
+
{
|
|
53
|
+
"type": "Message",
|
|
54
|
+
"children": [
|
|
55
|
+
{
|
|
56
|
+
"type": "Segment",
|
|
57
|
+
"name": "MSH",
|
|
58
|
+
"children": [
|
|
59
|
+
{ "type": "Field", "name": "MSH-1", "value": "|" },
|
|
60
|
+
{ "type": "Field", "name": "MSH-2", "value": "^~\\&" },
|
|
61
|
+
...
|
|
62
|
+
]
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
"type": "Segment",
|
|
66
|
+
"name": "PID",
|
|
67
|
+
"children": [
|
|
68
|
+
{ "type": "Field", "name": "PID-3", "value": "555-44-4444" },
|
|
69
|
+
{ "type": "Field", "name": "PID-5", "value": "DOE^JOHN" }
|
|
70
|
+
]
|
|
71
|
+
}
|
|
72
|
+
]
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## API
|
|
77
|
+
|
|
78
|
+
### `unified().use(hl7v2Jsonify)`
|
|
79
|
+
|
|
80
|
+
Transform an HL7v2 AST into JSON.
|
|
81
|
+
|
|
82
|
+
###### Parameters
|
|
83
|
+
|
|
84
|
+
There are no options.
|
|
85
|
+
|
|
86
|
+
###### Returns
|
|
87
|
+
|
|
88
|
+
Nothing (`undefined`). The unified compiler returns a stringified JSON document.
|
|
89
|
+
|
|
90
|
+
## JSON Schema
|
|
91
|
+
|
|
92
|
+
The output JSON has the following shape:
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
interface HL7v2JsonNode {
|
|
96
|
+
type: string
|
|
97
|
+
name?: string
|
|
98
|
+
index?: number
|
|
99
|
+
value?: string
|
|
100
|
+
delimiter?: string
|
|
101
|
+
children?: HL7v2JsonNode[]
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Security
|
|
106
|
+
|
|
107
|
+
This plugin only transforms AST nodes and does not execute code. Ensure you trust the source of HL7v2 messages before processing.
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
## Contributing
|
|
111
|
+
|
|
112
|
+
We welcome contributions! Please see our [Contributing Guide](CONTRIBUTING.md) for more details.
|
|
113
|
+
|
|
114
|
+
1. Fork the repository
|
|
115
|
+
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
|
116
|
+
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
|
|
117
|
+
4. Push to the branch (`git push origin feature/amazing-feature`)
|
|
118
|
+
5. Open a Pull Request
|
|
119
|
+
|
|
120
|
+
## Code of Conduct
|
|
121
|
+
|
|
122
|
+
To ensure a welcoming and positive environment, we have a [Code of Conduct](CODE_OF_CONDUCT.md) that all contributors and participants are expected to adhere to.
|
|
123
|
+
|
|
124
|
+
## License
|
|
125
|
+
|
|
126
|
+
Copyright 2025 Rethink Health, SUARL. All rights reserved.
|
|
127
|
+
|
|
128
|
+
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](LICENSE) file for details.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { HL7v2Node } from '@rethinkhealth/hl7v2-ast';
|
|
2
|
+
import type { Plugin } from 'unified';
|
|
3
|
+
type SegmentJSON = {
|
|
4
|
+
segment: string;
|
|
5
|
+
fields: (string | string[])[];
|
|
6
|
+
};
|
|
7
|
+
export declare const hl7v2Jsonify: Plugin<[], HL7v2Node, string>;
|
|
8
|
+
export declare function toJson(root: HL7v2Node): SegmentJSON[];
|
|
9
|
+
export {};
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +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"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { visitParents } from "unist-util-visit-parents";
|
|
3
|
+
var hl7v2Jsonify = function() {
|
|
4
|
+
const self = this;
|
|
5
|
+
function compiler(tree) {
|
|
6
|
+
return JSON.stringify(toJson(tree), null, 2);
|
|
7
|
+
}
|
|
8
|
+
self.compiler = compiler;
|
|
9
|
+
};
|
|
10
|
+
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);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
return segments;
|
|
31
|
+
}
|
|
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;
|
|
38
|
+
}
|
|
39
|
+
if (!current[idx]) {
|
|
40
|
+
current[idx] = [];
|
|
41
|
+
}
|
|
42
|
+
current = current[idx];
|
|
43
|
+
}
|
|
44
|
+
const lastKey = path.at(-1);
|
|
45
|
+
if (lastKey === void 0) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
current[lastKey] = value;
|
|
49
|
+
}
|
|
50
|
+
export {
|
|
51
|
+
hl7v2Jsonify,
|
|
52
|
+
toJson
|
|
53
|
+
};
|
|
54
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +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":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rethinkhealth/hl7v2-jsonify",
|
|
3
|
+
"description": "hl7v2 plugin to transform hl7v2 messages to a simplified JSON representation",
|
|
4
|
+
"version": "0.2.1",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "Melek Somai",
|
|
8
|
+
"email": "melek@rethinkhealth.io"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"files": [
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
15
|
+
"exports": {
|
|
16
|
+
".": "./dist/index.js"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"unified": "11.0.1",
|
|
20
|
+
"unist-util-visit": "^5.0.0",
|
|
21
|
+
"unist-util-visit-parents": "^6.0.1"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@types/node": "22.15.31",
|
|
25
|
+
"@types/unist": "^3.0.3",
|
|
26
|
+
"@vitest/coverage-c8": "^0.33.0",
|
|
27
|
+
"@vitest/coverage-v8": "^3.2.4",
|
|
28
|
+
"tsup": "8.5.0",
|
|
29
|
+
"typescript": "^5.8.3",
|
|
30
|
+
"vitest": "^3.2.4",
|
|
31
|
+
"@rethinkhealth/hl7v2-ast": "0.2.1",
|
|
32
|
+
"@rethinkhealth/testing": "0.0.0",
|
|
33
|
+
"@rethinkhealth/tsconfig": "0.0.0"
|
|
34
|
+
},
|
|
35
|
+
"repository": "rethinkhealth/hl7v2.git",
|
|
36
|
+
"homepage": "https://www.rethinkhealth.io/hl7v2/docs",
|
|
37
|
+
"keywords": [
|
|
38
|
+
"health",
|
|
39
|
+
"healthcare",
|
|
40
|
+
"hl7",
|
|
41
|
+
"hl7v2",
|
|
42
|
+
"nodejs",
|
|
43
|
+
"typescript"
|
|
44
|
+
],
|
|
45
|
+
"packageManager": "pnpm@10.12.1",
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public"
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"build": "tsup && tsc --emitDeclarationOnly",
|
|
51
|
+
"check-types": "tsc --noEmit",
|
|
52
|
+
"test": "vitest run",
|
|
53
|
+
"test:coverage": "vitest run --coverage",
|
|
54
|
+
"test:watch": "vitest"
|
|
55
|
+
}
|
|
56
|
+
}
|