@rethinkhealth/hl7v2-jsonify 0.12.0 → 0.13.2
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 +51 -66
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
package/dist/index.js
CHANGED
|
@@ -1,77 +1,62 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/runtime.ts
|
|
2
2
|
function toJsonRuntime(root) {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
} else if (child.type === "group") {
|
|
9
|
-
out.push(processGroup(child));
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
return out;
|
|
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;
|
|
13
8
|
}
|
|
14
9
|
function processSegment(segment) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
10
|
+
const fields = [];
|
|
11
|
+
for (const f of segment.children) fields.push(materializeField(f));
|
|
12
|
+
return {
|
|
13
|
+
fields,
|
|
14
|
+
segment: segment.name
|
|
15
|
+
};
|
|
20
16
|
}
|
|
21
17
|
function processGroup(group) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
return { children, group: group.name ?? "" };
|
|
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
|
+
};
|
|
31
25
|
}
|
|
32
26
|
function materializeField(field) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
56
|
-
return repetitions.map((r) => {
|
|
57
|
-
const rep = r;
|
|
58
|
-
if (rep.children.length === 1) {
|
|
59
|
-
return toComponent(rep.children[0]);
|
|
60
|
-
}
|
|
61
|
-
return toRepetitionArray(rep);
|
|
62
|
-
});
|
|
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
|
+
});
|
|
63
49
|
}
|
|
64
|
-
|
|
65
|
-
|
|
50
|
+
//#endregion
|
|
51
|
+
//#region src/processor.ts
|
|
66
52
|
function hl7v2Jsonify() {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
53
|
+
const self = this;
|
|
54
|
+
function compiler(tree) {
|
|
55
|
+
return toJsonRuntime(tree);
|
|
56
|
+
}
|
|
57
|
+
self.compiler = compiler;
|
|
72
58
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
};
|
|
59
|
+
//#endregion
|
|
60
|
+
export { hl7v2Jsonify, toJsonRuntime };
|
|
61
|
+
|
|
77
62
|
//# sourceMappingURL=index.js.map
|
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\";\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":";
|
|
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 \"@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 }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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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rethinkhealth/hl7v2-jsonify",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.2",
|
|
4
4
|
"description": "hl7v2 plugin to transform hl7v2 messages to a simplified JSON representation",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"health",
|
|
@@ -30,25 +30,25 @@
|
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"unified": "11.0.5",
|
|
33
|
-
"@rethinkhealth/hl7v2-ast": "0.
|
|
33
|
+
"@rethinkhealth/hl7v2-ast": "0.13.2"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@types/node": "^25.5.0",
|
|
37
37
|
"@types/unist": "^3.0.3",
|
|
38
38
|
"@vitest/coverage-v8": "4.1.0",
|
|
39
|
-
"
|
|
39
|
+
"tsdown": "0.21.7",
|
|
40
40
|
"typescript": "^5.9.3",
|
|
41
41
|
"vitest": "4.1.0",
|
|
42
|
-
"@rethinkhealth/hl7v2-builder": "0.
|
|
43
|
-
"@rethinkhealth/
|
|
44
|
-
"@rethinkhealth/
|
|
42
|
+
"@rethinkhealth/hl7v2-builder": "0.13.2",
|
|
43
|
+
"@rethinkhealth/testing": "0.0.2",
|
|
44
|
+
"@rethinkhealth/tsconfig": "0.0.1"
|
|
45
45
|
},
|
|
46
46
|
"engines": {
|
|
47
47
|
"node": ">=18"
|
|
48
48
|
},
|
|
49
49
|
"packageManager": "pnpm@10.14.0",
|
|
50
50
|
"scripts": {
|
|
51
|
-
"build": "
|
|
51
|
+
"build": "tsdown && tsc --emitDeclarationOnly",
|
|
52
52
|
"check-types": "tsc --noEmit",
|
|
53
53
|
"test": "vitest run",
|
|
54
54
|
"test:coverage": "vitest run --coverage",
|