@rethinkhealth/hl7v2-ast 0.1.0 → 0.2.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.
Files changed (3) hide show
  1. package/README.md +214 -0
  2. package/index.d.ts +9 -7
  3. package/package.json +1 -1
package/README.md ADDED
@@ -0,0 +1,214 @@
1
+ # HL7v2-AST
2
+
3
+ **H**ealth **L**evel **7** Version 2 **A**bstract **S**yntax **T**ree.
4
+
5
+ ***
6
+
7
+ **hl7v2-ast** is a specification for representing HL7v2 messages as an [abstract syntax tree](https://en.wikipedia.org/wiki/Abstract_syntax_tree). It implements **[unist](https://github.com/syntax-tree/unist)** and provides a structured representation of HL7v2 segments, fields, components, and subcomponents.
8
+
9
+ ## Introduction
10
+
11
+ This document defines a format for representing HL7v2 messages as an abstract syntax tree.
12
+
13
+ **hl7v2-ast** was created to support parsing, validation, and transformation of HL7v2 messages in a structured way.
14
+
15
+ The specification follows the [Unist](https://github.com/syntax-tree/unist) model to benefit from the ecosystem of utilities and the [Unified](https://unifiedjs.com) processing pipeline.
16
+
17
+ ### Where this specification fits
18
+
19
+ - **hl7v2-ast** extends [unist](https://github.com/syntax-tree/unist) with HL7-specific node types.
20
+ - Integrates with editor tooling, validators, and transformers.
21
+
22
+
23
+ ## Types
24
+
25
+ TypeScript types are published with the package:
26
+
27
+ ```sh
28
+ npm install @rethinkhealth/hl7v2-ast
29
+ ```
30
+
31
+ ## Nodes (abstract)
32
+
33
+ ### `Literal`
34
+
35
+ ```idl
36
+ interface Literal <: UnistLiteral {
37
+ value: string
38
+ }
39
+ ```
40
+
41
+ **Literal** represents a leaf HL7v2 node containing a `value`, such as a field,
42
+ component, or subcomponent.
43
+
44
+ ### `Parent`
45
+
46
+ ```idl
47
+ interface Parent <: UnistParent {
48
+ children: [HL7v2Node]
49
+ }
50
+ ```
51
+
52
+ **Parent** represents a container node in HL7v2, such as `message` or `segment`.
53
+
54
+ ## Nodes
55
+
56
+ ### `Message`
57
+
58
+ ```idl
59
+ interface Message <: Parent {
60
+ type: 'message'
61
+ children: [Segment]
62
+ }
63
+ ```
64
+
65
+ **Message** is the root of an HL7v2 document. It contains one or more `Segment` nodes.
66
+
67
+ ### `Segment`
68
+
69
+ ```idl
70
+ interface Segment <: Parent {
71
+ type: 'segment'
72
+ name: string
73
+ index: number
74
+ delimiter: string
75
+ children: [Field]
76
+ }
77
+ ```
78
+
79
+ **Segment** represents an HL7v2 segment such as `MSH`, `PID`, or `OBX`.
80
+
81
+ * `name` is the 3-letter segment code.
82
+ * `index` is the segment's position in the message.
83
+ * `delimiter` is the field separator used.
84
+
85
+ ### `Field`
86
+
87
+ ```idl
88
+ interface Field <: Parent {
89
+ type: 'field'
90
+ index: number
91
+ value?: string
92
+ delimiter?: string
93
+ children?: [Component]
94
+ }
95
+ ```
96
+
97
+ **Field** represents a field within a segment.
98
+
99
+ * If the field contains components, it is a `Parent`.
100
+ * If not, it is a `Literal` with a `value`.
101
+
102
+ ### `Component`
103
+
104
+ ```idl
105
+ interface Component <: Parent {
106
+ type: 'component'
107
+ index: number
108
+ value?: string
109
+ delimiter?: string
110
+ children?: [Subcomponent]
111
+ }
112
+ ```
113
+
114
+ **Component** represents a component within a field.
115
+
116
+ * Contains `Subcomponent` nodes if further split.
117
+
118
+ ### `Subcomponent`
119
+
120
+ ```idl
121
+ interface Subcomponent <: Literal {
122
+ type: 'subcomponent'
123
+ index: number
124
+ value: string
125
+ }
126
+ ```
127
+
128
+ **Subcomponent** represents the smallest value unit in an HL7v2 message.
129
+
130
+ ## Position
131
+
132
+ All nodes may include a `position` property following [unist]((https://github.com/syntax-tree/unist)):
133
+
134
+ ```idl
135
+ interface Position {
136
+ start: Point
137
+ end: Point
138
+ }
139
+
140
+ interface Point {
141
+ line: number // 1-based segment line
142
+ column: number // 1-based character column within the segment
143
+ offset: number // 0-based character index in the entire message
144
+ }
145
+ ```
146
+
147
+ Example:
148
+
149
+ ```json
150
+ {
151
+ "type": "field",
152
+ "index": 1,
153
+ "value": "DOE^JOHN",
154
+ "position": {
155
+ "start": { "line": 2, "column": 5, "offset": 48 },
156
+ "end": { "line": 2, "column": 14, "offset": 57 }
157
+ }
158
+ }
159
+ ```
160
+
161
+ ## Delimiters
162
+
163
+ HL7v2 messages use configurable delimiters. **hl7v2-ast** tracks delimiters per node for round-tripping.
164
+
165
+ Default:
166
+
167
+ ```json
168
+ {
169
+ "field": "|",
170
+ "component": "^",
171
+ "subcomponent": "&",
172
+ "repetition": "~",
173
+ "escape": "\\",
174
+ "segment": "\r"
175
+ }
176
+ ```
177
+
178
+ MSH-1 and MSH-2 are auto-detected unless overridden.
179
+
180
+ ## Content model
181
+
182
+ ```idl
183
+ type HL7v2Content =
184
+ Message | Segment | Field | Component | Subcomponent
185
+ ```
186
+
187
+ ## Extensions
188
+
189
+ The AST is designed for:
190
+
191
+ * **Validation plugins** (segment rules, field presence)
192
+ * **Annotation plugins** (map to FHIR, metadata)
193
+ * **Transformers** (to JSON, FHIR, XML)
194
+
195
+
196
+ ## Contributing
197
+
198
+ We welcome contributions! Please see our [Contributing Guide](../../CONTRIBUTING.md) for more details.
199
+
200
+ 1. Fork the repository
201
+ 2. Create your feature branch (`git checkout -b feature/amazing-feature`)
202
+ 3. Commit your changes (`git commit -m 'Add some amazing feature'`)
203
+ 4. Push to the branch (`git push origin feature/amazing-feature`)
204
+ 5. Open a Pull Request
205
+
206
+ ## Code of Conduct
207
+
208
+ 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.
209
+
210
+ ## License
211
+
212
+ Copyright 2025 Rethink Health, SUARL. All rights reserved.
213
+
214
+ 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/index.d.ts CHANGED
@@ -8,14 +8,16 @@ import type { Node, Position } from 'unist';
8
8
  export interface HL7v2Node extends Node {
9
9
  /**
10
10
  * The type of the node.
11
+ *
12
+ * There are 6 types of nodes:
13
+ * - **root**: The root node of the AST. This is the top-level node of the AST.
14
+ * - **segment**: A segment node (e.g. PID, MSH, etc.)
15
+ * - **header**: A header node (e.g. MSH, EVN, etc.)
16
+ * - **field**: A field node (e.g. PID.1, MSH.1, etc.)
17
+ * - **component**: A component node (e.g. PID.1.1, MSH.1.1, etc.)
18
+ * - **subcomponent**: A subcomponent node (e.g. PID.1.1.1, MSH.1.1.1, etc.)
11
19
  */
12
- type:
13
- | 'message'
14
- | 'segment'
15
- | 'header'
16
- | 'field'
17
- | 'component'
18
- | 'subcomponent';
20
+ type: 'root' | 'segment' | 'header' | 'field' | 'component' | 'subcomponent';
19
21
 
20
22
  /**
21
23
  * The name of the node (e.g. "PID", "MSH", "EVN", etc.)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rethinkhealth/hl7v2-ast",
3
3
  "description": "HL7v2 is a specification for representing HL7v2 messages as an abstract syntax tree. It implements the unist spec.",
4
- "version": "0.1.0",
4
+ "version": "0.2.2",
5
5
  "license": "MIT",
6
6
  "author": {
7
7
  "name": "Melek Somai",