@glion/annotate-profile-fields 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,150 @@
1
+ # @glion/annotate-profile-fields
2
+
3
+ **[unified](https://github.com/unifiedjs/unified)** plugin to annotate HL7v2 field nodes with profile metadata.
4
+
5
+ ## Contents
6
+
7
+ - [What is this?](#what-is-this)
8
+ - [When should I use this?](#when-should-i-use-this)
9
+ - [Install](#install)
10
+ - [Use](#use)
11
+ - [API](#api)
12
+ - [Examples](#examples)
13
+ - [Compatibility](#compatibility)
14
+ - [Related](#related)
15
+
16
+ ## What is this?
17
+
18
+ This package is a [unified](https://github.com/unifiedjs/unified) plugin that enriches Field AST nodes with metadata from HL7v2 profile definitions. After running this plugin, each field carries its profile information directly on `field.data` — making the AST self-describing.
19
+
20
+ The plugin reads the message version from MSH-12, loads the corresponding field definitions from `@glion/profiles`, and spreads the profile properties onto each field node. Unknown segments (Z-segments) and unsupported versions are silently skipped.
21
+
22
+ ## When should I use this?
23
+
24
+ Use this plugin when:
25
+
26
+ - You want to inspect a parsed AST and see field names, datatypes, and constraints without loading profiles yourself
27
+ - Building tools (serializers, debuggers, IDE integrations) that need field-level metadata
28
+ - Creating custom processors that need to know which fields are required, repeatable, or coded
29
+
30
+ ## Install
31
+
32
+ ```bash
33
+ npm install @glion/annotate-profile-fields
34
+ ```
35
+
36
+ ## Use
37
+
38
+ ```typescript
39
+ import { unified } from "unified";
40
+ import { hl7v2Parser } from "@glion/parser";
41
+ import { hl7v2AnnotateProfileFields } from "@glion/annotate-profile-fields";
42
+
43
+ const processor = unified().use(hl7v2Parser).use(hl7v2AnnotateProfileFields);
44
+
45
+ const message =
46
+ "MSH|^~\\&|SENDER||RECEIVER||20241201||ADT^A01^ADT_A01|MSG123|P|2.5\rPID|1||12345||Doe^John";
47
+
48
+ const tree = processor.parse(message);
49
+ await processor.run(tree);
50
+
51
+ // Field nodes now carry profile metadata on field.data
52
+ // e.g., PID-3: { id: "PID-3", name: "Patient Identifier List", required: true, repeatable: true, datatype: "CX", ... }
53
+ ```
54
+
55
+ ## API
56
+
57
+ This package exports the identifier `hl7v2AnnotateProfileFields`. The default export is `hl7v2AnnotateProfileFields`.
58
+
59
+ ### `unified().use(hl7v2AnnotateProfileFields)`
60
+
61
+ Annotate Field nodes with profile metadata.
62
+
63
+ This plugin:
64
+
65
+ 1. Reads the HL7v2 version from MSH-12
66
+ 2. Loads field definitions for all segments in the message
67
+ 3. Visits each Field node and spreads the matching profile properties onto `field.data`
68
+
69
+ ###### Returns
70
+
71
+ Async transformer (`async function (Root) => Root`)
72
+
73
+ ### Augmented `FieldData`
74
+
75
+ Importing this package augments the `FieldData` interface from `@glion/ast`:
76
+
77
+ | Property | Type | Description |
78
+ | ------------ | --------- | ---------------------------------------------------- |
79
+ | `id` | `string` | Field identifier (e.g., `"MSH-9"`, `"PID-3"`) |
80
+ | `name` | `string` | Human-readable field name (e.g., `"Patient Name"`) |
81
+ | `required` | `boolean` | Whether this field is required in the segment |
82
+ | `repeatable` | `boolean` | Whether this field supports repetition |
83
+ | `datatype` | `string` | Datatype ID (e.g., `"XPN"`, `"ST"`, `"CWE"`) |
84
+ | `maxLength` | `number` | Maximum allowed length (when defined in the profile) |
85
+ | `table` | `string` | Table reference for coded fields (e.g., `"HL70001"`) |
86
+ | `item` | `string` | HL7 specification item number |
87
+
88
+ All properties are optional (`undefined` when not available in the profile).
89
+
90
+ ## Examples
91
+
92
+ ### Accessing field metadata
93
+
94
+ ```typescript
95
+ import { visit } from "@glion/util-visit";
96
+
97
+ // After running the annotator...
98
+ visit(tree, "field", (node, ancestors, info) => {
99
+ if (node.data?.required && node.data?.name) {
100
+ console.log(`Required field: ${node.data.id} (${node.data.name})`);
101
+ }
102
+ });
103
+ ```
104
+
105
+ ### Finding coded fields
106
+
107
+ ```typescript
108
+ visit(tree, "field", (node) => {
109
+ if (node.data?.table) {
110
+ console.log(`${node.data.id} uses table ${node.data.table}`);
111
+ }
112
+ });
113
+ ```
114
+
115
+ ## Compatibility
116
+
117
+ - **Node.js**: 18+
118
+ - **TypeScript**: 5.0+
119
+ - **unified**: 11.0+
120
+
121
+ ## Related
122
+
123
+ - [`@glion/annotate-profile-datatypes`](../hl7v2-annotate-profile-datatypes) — Annotate components with datatype metadata
124
+ - [`@glion/annotate-profile-code-systems`](../hl7v2-annotate-profile-code-systems) — Annotate coded values with UTG display names
125
+ - [`@glion/preset-annotate-profile-recommended`](../hl7v2-preset-annotate-profile-recommended) — Preset bundling all profile annotators
126
+ - [`@glion/profiles`](../hl7v2-profiles) — Profile data source
127
+
128
+ ## Contributing
129
+
130
+ We welcome contributions! Please see our [Contributing Guide][github-contributing] for more details.
131
+
132
+ 1. Fork the repository
133
+ 2. Create your feature branch (`git checkout -b feature/amazing-feature`)
134
+ 3. Commit your changes (`git commit -m 'Add some amazing feature'`)
135
+ 4. Push to the branch (`git push origin feature/amazing-feature`)
136
+ 5. Open a Pull Request
137
+
138
+ ## Code of Conduct
139
+
140
+ 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.
141
+
142
+ ## License
143
+
144
+ Copyright 2025 Rethink Health, SUARL. All rights reserved.
145
+
146
+ 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.
147
+
148
+ [github-code-of-conduct]: https://github.com/rethinkhealth/glion/blob/main/CODE_OF_CONDUCT.md
149
+ [github-license]: https://github.com/rethinkhealth/glion/blob/main/LICENSE
150
+ [github-contributing]: https://github.com/rethinkhealth/glion/blob/main/CONTRIBUTING.md
@@ -0,0 +1,34 @@
1
+ import type { Root } from "@glion/ast";
2
+ import type { Plugin } from "unified";
3
+ declare module "@glion/ast" {
4
+ interface FieldData {
5
+ /** Field identifier (e.g., "MSH-9", "PID-3"). */
6
+ id?: string | undefined;
7
+ /** Human-readable field name from the profile (e.g., "Patient Name"). */
8
+ name?: string | undefined;
9
+ /** Whether this field is required in the segment. */
10
+ required?: boolean | undefined;
11
+ /** Whether this field supports repetition. */
12
+ repeatable?: boolean | undefined;
13
+ /** Datatype ID for this field (e.g., "XPN", "ST", "CWE"). */
14
+ datatype?: string | undefined;
15
+ /** Maximum allowed length for this field. */
16
+ maxLength?: number | undefined;
17
+ /** Table reference for coded fields (e.g., "HL70001"). */
18
+ table?: string | undefined;
19
+ /** HL7 specification item number. */
20
+ item?: string | undefined;
21
+ }
22
+ }
23
+ /**
24
+ * Unified plugin that annotates Field nodes with profile metadata.
25
+ *
26
+ * Reads field definitions from `file.data.fields` (populated by
27
+ * `@glion/annotate-profile-context`) and spreads the profile
28
+ * properties directly onto each `field.data`.
29
+ *
30
+ * Unknown segments (Z-segments) and unsupported versions are silently skipped.
31
+ */
32
+ export declare const hl7v2AnnotateProfileFields: Plugin<[], Root, Root>;
33
+ export default hl7v2AnnotateProfileFields;
34
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAa,IAAI,EAAW,MAAM,YAAY,CAAC;AAG3D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAGtC,OAAO,QAAQ,YAAY,CAAC;IAC1B,UAAU,SAAS;QACjB,iDAAiD;QACjD,EAAE,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;QACxB,yEAAyE;QACzE,IAAI,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;QAC1B,qDAAqD;QACrD,QAAQ,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;QAC/B,8CAA8C;QAC9C,UAAU,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;QACjC,6DAA6D;QAC7D,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;QAC9B,6CAA6C;QAC7C,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;QAC/B,0DAA0D;QAC1D,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;QAC3B,qCAAqC;QACrC,IAAI,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;KAC3B;CACF;AAED;;;;;;;;GAQG;AACH,eAAO,MAAM,0BAA0B,EAAE,MAAM,CAAC,EAAE,EAAE,IAAI,EAAE,IAAI,CA8B3D,CAAC;AAmBJ,eAAe,0BAA0B,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,39 @@
1
+ import { SKIP, visit } from "@glion/util-visit";
2
+ //#region src/index.ts
3
+ /**
4
+ * Unified plugin that annotates Field nodes with profile metadata.
5
+ *
6
+ * Reads field definitions from `file.data.fields` (populated by
7
+ * `@glion/annotate-profile-context`) and spreads the profile
8
+ * properties directly onto each `field.data`.
9
+ *
10
+ * Unknown segments (Z-segments) and unsupported versions are silently skipped.
11
+ */
12
+ const hl7v2AnnotateProfileFields = () => (tree, file) => {
13
+ const ctx = file.data.profile;
14
+ if (!ctx) return tree;
15
+ visit(tree, "field", (node, ancestors, info) => {
16
+ const segment = ancestors.at(-1);
17
+ if (segment?.type !== "segment") return SKIP;
18
+ const profile = ctx.fields.get(segment.name)?.bySequence.get(info.sequence);
19
+ if (!profile) return SKIP;
20
+ if (!node.data) node.data = {};
21
+ spreadFieldProfile(node.data, profile);
22
+ return SKIP;
23
+ });
24
+ return tree;
25
+ };
26
+ function spreadFieldProfile(data, profile) {
27
+ data.id = profile.id;
28
+ data.name = profile.name;
29
+ data.required = profile.required;
30
+ data.repeatable = profile.repeatable;
31
+ data.datatype = profile.datatype;
32
+ if (profile.maxLength !== void 0) data.maxLength = profile.maxLength;
33
+ if (profile.table !== void 0) data.table = profile.table;
34
+ if (profile.item !== void 0) data.item = profile.item;
35
+ }
36
+ //#endregion
37
+ export { hl7v2AnnotateProfileFields as default, hl7v2AnnotateProfileFields };
38
+
39
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["// oxlint-disable-next-line no-unused-vars -- triggers VFile DataMap augmentation\nimport type { ProfileContext } from \"@glion/annotate-profile-context\";\nimport type { FieldData, Root, Segment } from \"@glion/ast\";\nimport type { FieldProfile } from \"@glion/profiles\";\nimport { SKIP, visit } from \"@glion/util-visit\";\nimport type { Plugin } from \"unified\";\nimport type { VFile } from \"vfile\";\n\ndeclare module \"@glion/ast\" {\n interface FieldData {\n /** Field identifier (e.g., \"MSH-9\", \"PID-3\"). */\n id?: string | undefined;\n /** Human-readable field name from the profile (e.g., \"Patient Name\"). */\n name?: string | undefined;\n /** Whether this field is required in the segment. */\n required?: boolean | undefined;\n /** Whether this field supports repetition. */\n repeatable?: boolean | undefined;\n /** Datatype ID for this field (e.g., \"XPN\", \"ST\", \"CWE\"). */\n datatype?: string | undefined;\n /** Maximum allowed length for this field. */\n maxLength?: number | undefined;\n /** Table reference for coded fields (e.g., \"HL70001\"). */\n table?: string | undefined;\n /** HL7 specification item number. */\n item?: string | undefined;\n }\n}\n\n/**\n * Unified plugin that annotates Field nodes with profile metadata.\n *\n * Reads field definitions from `file.data.fields` (populated by\n * `@glion/annotate-profile-context`) and spreads the profile\n * properties directly onto each `field.data`.\n *\n * Unknown segments (Z-segments) and unsupported versions are silently skipped.\n */\nexport const hl7v2AnnotateProfileFields: Plugin<[], Root, Root> =\n () => (tree: Root, file: VFile) => {\n const ctx = file.data.profile;\n if (!ctx) {\n return tree;\n }\n\n // Annotate each field with its profile\n visit(tree, \"field\", (node, ancestors, info) => {\n const segment = ancestors.at(-1) as Segment | undefined;\n if (segment?.type !== \"segment\") {\n return SKIP;\n }\n\n const profile = ctx.fields\n .get(segment.name)\n ?.bySequence.get(info.sequence);\n if (!profile) {\n return SKIP;\n }\n\n if (!node.data) {\n node.data = {};\n }\n\n spreadFieldProfile(node.data, profile);\n return SKIP;\n });\n\n return tree;\n };\n\nfunction spreadFieldProfile(data: FieldData, profile: FieldProfile): void {\n data.id = profile.id;\n data.name = profile.name;\n data.required = profile.required;\n data.repeatable = profile.repeatable;\n data.datatype = profile.datatype;\n if (profile.maxLength !== undefined) {\n data.maxLength = profile.maxLength;\n }\n if (profile.table !== undefined) {\n data.table = profile.table;\n }\n if (profile.item !== undefined) {\n data.item = profile.item;\n }\n}\n\nexport default hl7v2AnnotateProfileFields;\n"],"mappings":";;;;;;;;;;;AAsCA,MAAa,oCACJ,MAAY,SAAgB;CACjC,MAAM,MAAM,KAAK,KAAK;AACtB,KAAI,CAAC,IACH,QAAO;AAIT,OAAM,MAAM,UAAU,MAAM,WAAW,SAAS;EAC9C,MAAM,UAAU,UAAU,GAAG,GAAG;AAChC,MAAI,SAAS,SAAS,UACpB,QAAO;EAGT,MAAM,UAAU,IAAI,OACjB,IAAI,QAAQ,KAAK,EAChB,WAAW,IAAI,KAAK,SAAS;AACjC,MAAI,CAAC,QACH,QAAO;AAGT,MAAI,CAAC,KAAK,KACR,MAAK,OAAO,EAAE;AAGhB,qBAAmB,KAAK,MAAM,QAAQ;AACtC,SAAO;GACP;AAEF,QAAO;;AAGX,SAAS,mBAAmB,MAAiB,SAA6B;AACxE,MAAK,KAAK,QAAQ;AAClB,MAAK,OAAO,QAAQ;AACpB,MAAK,WAAW,QAAQ;AACxB,MAAK,aAAa,QAAQ;AAC1B,MAAK,WAAW,QAAQ;AACxB,KAAI,QAAQ,cAAc,KAAA,EACxB,MAAK,YAAY,QAAQ;AAE3B,KAAI,QAAQ,UAAU,KAAA,EACpB,MAAK,QAAQ,QAAQ;AAEvB,KAAI,QAAQ,SAAS,KAAA,EACnB,MAAK,OAAO,QAAQ"}
package/package.json ADDED
@@ -0,0 +1,64 @@
1
+ {
2
+ "name": "@glion/annotate-profile-fields",
3
+ "version": "0.14.1",
4
+ "description": "Unified plugin to annotate HL7v2 field nodes with profile metadata",
5
+ "keywords": [
6
+ "annotate",
7
+ "health",
8
+ "healthcare",
9
+ "hl7",
10
+ "hl7v2",
11
+ "nodejs",
12
+ "plugin",
13
+ "profile",
14
+ "typescript",
15
+ "unified"
16
+ ],
17
+ "homepage": "https://glion.dev",
18
+ "license": "MIT",
19
+ "author": {
20
+ "name": "Melek Somai",
21
+ "email": "melek@rethinkhealth.io"
22
+ },
23
+ "repository": "rethinkhealth/glion.git",
24
+ "files": [
25
+ "dist"
26
+ ],
27
+ "type": "module",
28
+ "types": "./dist/index.d.ts",
29
+ "exports": {
30
+ ".": "./dist/index.js"
31
+ },
32
+ "publishConfig": {
33
+ "access": "public"
34
+ },
35
+ "dependencies": {
36
+ "unified": "11.0.5",
37
+ "vfile": "6.0.3",
38
+ "@glion/util-visit": "0.14.1",
39
+ "@glion/ast": "0.14.1"
40
+ },
41
+ "devDependencies": {
42
+ "@types/node": "^25.5.0",
43
+ "@vitest/coverage-v8": "4.1.2",
44
+ "tsdown": "0.21.7",
45
+ "typescript": "^5.9.3",
46
+ "vitest": "4.1.2",
47
+ "@glion/profiles": "0.14.1",
48
+ "@glion/testing": "0.0.2",
49
+ "@glion/annotate-profile-context": "0.14.1",
50
+ "@glion/builder": "0.14.1",
51
+ "@glion/tsconfig": "0.0.1"
52
+ },
53
+ "engines": {
54
+ "node": ">=18"
55
+ },
56
+ "scripts": {
57
+ "build": "tsdown && tsc --emitDeclarationOnly",
58
+ "check-types": "tsc --noEmit",
59
+ "bench": "vitest bench --run",
60
+ "test": "vitest run",
61
+ "test:coverage": "vitest run --coverage",
62
+ "test:watch": "vitest"
63
+ }
64
+ }