@rethinkhealth/hl7v2-jsonify 0.4.1 → 0.5.0
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/README.md +15 -19
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -4
- package/dist/index.js.map +1 -1
- package/dist/processor.d.ts +1 -0
- package/dist/processor.d.ts.map +1 -0
- package/dist/runtime.d.ts +1 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/types.d.ts +7 -6
- package/dist/types.d.ts.map +1 -0
- package/package.json +21 -21
package/README.md
CHANGED
|
@@ -12,8 +12,8 @@ It strips away [unist](https://github.com/syntax-tree/unist) metadata and preser
|
|
|
12
12
|
|
|
13
13
|
Use this plugin when you need:
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
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
17
|
|
|
18
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
19
|
|
|
@@ -32,18 +32,15 @@ npm install @rethinkhealth/hl7v2-jsonify
|
|
|
32
32
|
Say we have an HL7v2 message and want to convert it to JSON:
|
|
33
33
|
|
|
34
34
|
```js
|
|
35
|
-
import { unified } from
|
|
36
|
-
import { hl7v2Parse } from
|
|
37
|
-
import { hl7v2Jsonify } from
|
|
35
|
+
import { unified } from "unified";
|
|
36
|
+
import { hl7v2Parse } from "@rethinkhealth/hl7v2-ast";
|
|
37
|
+
import { hl7v2Jsonify } from "@rethinkhealth/hl7v2-jsonify";
|
|
38
38
|
|
|
39
|
-
const msg = `MSH|^~\\&|HIS|RIH|EKG|EKG|200202150930||ADT^A01|MSG00001|P|2.4\rPID|||555-44-4444||DOE^JOHN
|
|
39
|
+
const msg = `MSH|^~\\&|HIS|RIH|EKG|EKG|200202150930||ADT^A01|MSG00001|P|2.4\rPID|||555-44-4444||DOE^JOHN`;
|
|
40
40
|
|
|
41
|
-
const file = await unified()
|
|
42
|
-
.use(hl7v2Parse)
|
|
43
|
-
.use(hl7v2Jsonify)
|
|
44
|
-
.process(msg)
|
|
41
|
+
const file = await unified().use(hl7v2Parse).use(hl7v2Jsonify).process(msg);
|
|
45
42
|
|
|
46
|
-
console.log(String(file))
|
|
43
|
+
console.log(String(file));
|
|
47
44
|
```
|
|
48
45
|
|
|
49
46
|
Yields:
|
|
@@ -93,12 +90,12 @@ The output JSON has the following shape:
|
|
|
93
90
|
|
|
94
91
|
```ts
|
|
95
92
|
interface HL7v2JsonNode {
|
|
96
|
-
type: string
|
|
97
|
-
name?: string
|
|
98
|
-
index?: number
|
|
99
|
-
value?: string
|
|
100
|
-
delimiter?: string
|
|
101
|
-
children?: HL7v2JsonNode[]
|
|
93
|
+
type: string;
|
|
94
|
+
name?: string;
|
|
95
|
+
index?: number;
|
|
96
|
+
value?: string;
|
|
97
|
+
delimiter?: string;
|
|
98
|
+
children?: HL7v2JsonNode[];
|
|
102
99
|
}
|
|
103
100
|
```
|
|
104
101
|
|
|
@@ -106,7 +103,6 @@ interface HL7v2JsonNode {
|
|
|
106
103
|
|
|
107
104
|
This plugin only transforms AST nodes and does not execute code. Ensure you trust the source of HL7v2 messages before processing.
|
|
108
105
|
|
|
109
|
-
|
|
110
106
|
## Contributing
|
|
111
107
|
|
|
112
108
|
We welcome contributions! Please see our [Contributing Guide][github-contributing] for more details.
|
|
@@ -129,4 +125,4 @@ This program is licensed to you under the terms of the [MIT License](https://ope
|
|
|
129
125
|
|
|
130
126
|
[github-code-of-conduct]: https://github.com/rethinkhealth/hl7v2/blob/main/CODE_OF_CONDUCT.md
|
|
131
127
|
[github-license]: https://github.com/rethinkhealth/hl7v2/blob/main/LICENSE
|
|
132
|
-
[github-contributing]: https://github.com/rethinkhealth/hl7v2/blob/main/CONTRIBUTING.md
|
|
128
|
+
[github-contributing]: https://github.com/rethinkhealth/hl7v2/blob/main/CONTRIBUTING.md
|
package/dist/index.d.ts
CHANGED
|
@@ -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
CHANGED
|
@@ -12,12 +12,11 @@ function toJsonRuntime(root) {
|
|
|
12
12
|
return out;
|
|
13
13
|
}
|
|
14
14
|
function processSegment(segment) {
|
|
15
|
-
const segmentName = segment.children[0].value;
|
|
16
15
|
const fields = [];
|
|
17
|
-
for (const f of segment.children
|
|
16
|
+
for (const f of segment.children) {
|
|
18
17
|
fields.push(materializeField(f));
|
|
19
18
|
}
|
|
20
|
-
return { segment:
|
|
19
|
+
return { fields, segment: segment.name };
|
|
21
20
|
}
|
|
22
21
|
function processGroup(group) {
|
|
23
22
|
const children = [];
|
|
@@ -28,7 +27,7 @@ function processGroup(group) {
|
|
|
28
27
|
children.push(processGroup(child));
|
|
29
28
|
}
|
|
30
29
|
}
|
|
31
|
-
return { group: group.name ?? ""
|
|
30
|
+
return { children, group: group.name ?? "" };
|
|
32
31
|
}
|
|
33
32
|
function materializeField(field) {
|
|
34
33
|
const toComponent = (c) => {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"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 \"@rethinkhealth/hl7v2-ast\";\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
|
|
1
|
+
{"version":3,"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 \"@rethinkhealth/hl7v2-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 \"@rethinkhealth/hl7v2-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":";AAkBO,SAAS,cAAc,MAA8B;AAC1D,QAAM,IAAI;AACV,QAAM,MAAuB,CAAC;AAE9B,aAAW,SAAS,EAAE,UAAU;AAC9B,QAAI,MAAM,SAAS,WAAW;AAC5B,UAAI,KAAK,eAAe,KAAgB,CAAC;AAAA,IAC3C,WAAW,MAAM,SAAS,SAAS;AACjC,UAAI,KAAK,aAAa,KAAc,CAAC;AAAA,IACvC;AAAA,EACF;AAEA,SAAO;AACT;AAEA,SAAS,eAAe,SAA+B;AACrD,QAAM,SAAsC,CAAC;AAE7C,aAAW,KAAK,QAAQ,UAAU;AAChC,WAAO,KAAK,iBAAiB,CAAC,CAAC;AAAA,EACjC;AAEA,SAAO,EAAE,QAAQ,SAAS,QAAQ,KAAK;AACzC;AAEA,SAAS,aAAa,OAAyB;AAC7C,QAAM,WAAwC,CAAC;AAE/C,aAAW,SAAS,MAA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SAAS,MAA8B;AAC9C,WAAO,cAAc,IAAI;AAAA,EAC3B;AAEA,OAAK,WAAW;AAClB;","names":[]}
|
package/dist/processor.d.ts
CHANGED
|
@@ -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"}
|
package/dist/runtime.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAKV,KAAK,EAIN,MAAM,0BAA0B,CAAC;AAElC,OAAO,KAAK,EAGV,eAAe,EAEhB,MAAM,SAAS,CAAC;AAEjB,wBAAgB,aAAa,CAAC,IAAI,EAAE,KAAK,GAAG,eAAe,CAa1D"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
export type FieldJson = string | string[];
|
|
2
|
-
export
|
|
2
|
+
export interface SegmentJson {
|
|
3
3
|
segment: string;
|
|
4
4
|
fields: (FieldJson | FieldJson[])[];
|
|
5
|
-
}
|
|
6
|
-
export
|
|
5
|
+
}
|
|
6
|
+
export interface GroupJson {
|
|
7
7
|
group: string;
|
|
8
|
-
children:
|
|
9
|
-
}
|
|
10
|
-
export type Hl7v2JsonResult =
|
|
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
CHANGED
|
@@ -1,52 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rethinkhealth/hl7v2-jsonify",
|
|
3
|
+
"version": "0.5.0",
|
|
3
4
|
"description": "hl7v2 plugin to transform hl7v2 messages to a simplified JSON representation",
|
|
4
|
-
"
|
|
5
|
+
"keywords": [
|
|
6
|
+
"health",
|
|
7
|
+
"healthcare",
|
|
8
|
+
"hl7",
|
|
9
|
+
"hl7v2",
|
|
10
|
+
"nodejs",
|
|
11
|
+
"typescript"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://www.rethinkhealth.io/hl7v2/docs",
|
|
5
14
|
"license": "MIT",
|
|
6
15
|
"author": {
|
|
7
16
|
"name": "Melek Somai",
|
|
8
17
|
"email": "melek@rethinkhealth.io"
|
|
9
18
|
},
|
|
10
|
-
"
|
|
11
|
-
"types": "./dist/index.d.ts",
|
|
19
|
+
"repository": "rethinkhealth/hl7v2.git",
|
|
12
20
|
"files": [
|
|
13
21
|
"dist"
|
|
14
22
|
],
|
|
23
|
+
"type": "module",
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
15
25
|
"exports": {
|
|
16
26
|
".": "./dist/index.js"
|
|
17
27
|
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
18
31
|
"dependencies": {
|
|
19
32
|
"unified": "11.0.5",
|
|
20
|
-
"@rethinkhealth/hl7v2-ast": "0.
|
|
33
|
+
"@rethinkhealth/hl7v2-ast": "0.5.0"
|
|
21
34
|
},
|
|
22
35
|
"devDependencies": {
|
|
23
36
|
"@types/node": "24.10.1",
|
|
24
37
|
"@types/unist": "^3.0.3",
|
|
25
|
-
"@vitest/coverage-v8": "4.0.
|
|
38
|
+
"@vitest/coverage-v8": "4.0.18",
|
|
26
39
|
"tsup": "8.5.1",
|
|
27
40
|
"typescript": "^5.9.3",
|
|
28
41
|
"vitest": "4.0.14",
|
|
29
|
-
"@rethinkhealth/
|
|
30
|
-
"@rethinkhealth/
|
|
31
|
-
"@rethinkhealth/
|
|
42
|
+
"@rethinkhealth/testing": "0.0.2",
|
|
43
|
+
"@rethinkhealth/hl7v2-builder": "0.5.0",
|
|
44
|
+
"@rethinkhealth/tsconfig": "0.0.1"
|
|
32
45
|
},
|
|
33
46
|
"engines": {
|
|
34
47
|
"node": ">=18"
|
|
35
48
|
},
|
|
36
|
-
"repository": "rethinkhealth/hl7v2.git",
|
|
37
|
-
"homepage": "https://www.rethinkhealth.io/hl7v2/docs",
|
|
38
|
-
"keywords": [
|
|
39
|
-
"health",
|
|
40
|
-
"healthcare",
|
|
41
|
-
"hl7",
|
|
42
|
-
"hl7v2",
|
|
43
|
-
"nodejs",
|
|
44
|
-
"typescript"
|
|
45
|
-
],
|
|
46
49
|
"packageManager": "pnpm@10.14.0",
|
|
47
|
-
"publishConfig": {
|
|
48
|
-
"access": "public"
|
|
49
|
-
},
|
|
50
50
|
"scripts": {
|
|
51
51
|
"build": "tsup && tsc --emitDeclarationOnly",
|
|
52
52
|
"check-types": "tsc --noEmit",
|