@rethinkhealth/hl7v2-encode-escapes 0.7.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/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,118 @@
1
+ # @rethinkhealth/hl7v2-encode-escapes
2
+
3
+ **[unified](https://unifiedjs.com/)** plugin to encode special characters as HL7v2 escape sequences in literal values.
4
+
5
+ ## What is this?
6
+
7
+ `@rethinkhealth/hl7v2-encode-escapes` is a [unified](https://unifiedjs.com/) plugin that traverses an HL7v2 syntax tree and encodes delimiter characters in all literal values (`subcomponent` nodes) as HL7v2 escape sequences.
8
+
9
+ This is the inverse of [`@rethinkhealth/hl7v2-decode-escapes`](https://github.com/rethinkhealth/hl7v2/tree/main/packages/hl7v2-decode-escapes), handling:
10
+
11
+ - Delimiter encoding (`|` → `\F\`, `^` → `\S\`, `~` → `\R\`, `&` → `\T\`)
12
+ - Escape character encoding (`\` → `\E\`)
13
+ - Line break encoding (`\r` → `\.br\`)
14
+
15
+ ## When should I use this?
16
+
17
+ Use this plugin when you need to:
18
+
19
+ - Prepare an HL7v2 AST for serialization after modifying subcomponent values that may contain delimiter characters.
20
+ - Ensure values with special characters are safely encoded before converting the AST back to HL7v2 text with [`@rethinkhealth/hl7v2-to-hl7v2`](https://github.com/rethinkhealth/hl7v2/tree/main/packages/hl7v2-to-hl7v2).
21
+
22
+ ## Install
23
+
24
+ This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).
25
+
26
+ In Node.js (version 18+), install with [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm):
27
+
28
+ ```sh
29
+ npm install @rethinkhealth/hl7v2-encode-escapes
30
+ ```
31
+
32
+ ## Use
33
+
34
+ ```js
35
+ import { unified } from "unified";
36
+ import { hl7v2EncodeEscapes } from "@rethinkhealth/hl7v2-encode-escapes";
37
+ import { hl7v2ToHl7v2 } from "@rethinkhealth/hl7v2-to-hl7v2";
38
+
39
+ // After modifying AST values that may contain delimiters:
40
+ const file = await unified()
41
+ .use(hl7v2EncodeEscapes) // encode special chars as escape sequences
42
+ .use(hl7v2ToHl7v2) // serialize AST to HL7v2 text
43
+ .stringify(tree);
44
+ ```
45
+
46
+ Before encoding, a `subcomponent.value` might contain literal delimiters:
47
+
48
+ ```json
49
+ {
50
+ "type": "subcomponent",
51
+ "value": "Patient allergic to |Peanuts| and \rSevere reaction"
52
+ }
53
+ ```
54
+
55
+ After this plugin runs:
56
+
57
+ ```json
58
+ {
59
+ "type": "subcomponent",
60
+ "value": "Patient allergic to \\F\\Peanuts\\F\\ and \\.br\\Severe reaction"
61
+ }
62
+ ```
63
+
64
+ ## API
65
+
66
+ ### `unified().use(hl7v2EncodeEscapes[, options])`
67
+
68
+ Encode special characters as HL7v2 escape sequences in literal nodes.
69
+
70
+ ###### Parameters
71
+
72
+ - `options.delimiters` (optional) — Override delimiters. If omitted, the plugin reads them from `Root.data.delimiters` (set by the parser). Defaults to the HL7 standard (`| ^ ~ & \`).
73
+
74
+ ###### Returns
75
+
76
+ Nothing (`undefined`). Mutates the AST in-place.
77
+
78
+ ## Round-trip compatibility
79
+
80
+ This plugin is the inverse of `@rethinkhealth/hl7v2-decode-escapes`. Encoding then decoding restores the original value:
81
+
82
+ ```js
83
+ import { hl7v2DecodeEscapes } from "@rethinkhealth/hl7v2-decode-escapes";
84
+ import { hl7v2EncodeEscapes } from "@rethinkhealth/hl7v2-encode-escapes";
85
+
86
+ // encode → decode round-trip
87
+ const encoded = await unified().use(hl7v2EncodeEscapes).run(tree);
88
+ const decoded = await unified().use(hl7v2DecodeEscapes).run(encoded);
89
+ // decoded values === original values
90
+ ```
91
+
92
+ ## Security
93
+
94
+ This plugin only transforms AST nodes and does not execute code. Ensure you trust the source of HL7v2 messages before processing.
95
+
96
+ ## Contributing
97
+
98
+ We welcome contributions! Please see our [Contributing Guide][github-contributing] for more details.
99
+
100
+ 1. Fork the repository
101
+ 2. Create your feature branch (`git checkout -b feature/amazing-feature`)
102
+ 3. Commit your changes (`git commit -m 'Add some amazing feature'`)
103
+ 4. Push to the branch (`git push origin feature/amazing-feature`)
104
+ 5. Open a Pull Request
105
+
106
+ ## Code of Conduct
107
+
108
+ 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.
109
+
110
+ ## License
111
+
112
+ Copyright 2025 Rethink Health, SUARL. All rights reserved.
113
+
114
+ 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.
115
+
116
+ [github-code-of-conduct]: https://github.com/rethinkhealth/hl7v2/blob/main/CODE_OF_CONDUCT.md
117
+ [github-license]: https://github.com/rethinkhealth/hl7v2/blob/main/LICENSE
118
+ [github-contributing]: https://github.com/rethinkhealth/hl7v2/blob/main/CONTRIBUTING.md
@@ -0,0 +1,16 @@
1
+ import type { Delimiters, Root } from "@rethinkhealth/hl7v2-ast";
2
+ import type { Plugin } from "unified";
3
+ export interface HL7v2EncodeOptions {
4
+ delimiters?: Partial<Delimiters>;
5
+ }
6
+ /**
7
+ * Unified plugin to encode special characters as HL7v2 escape sequences
8
+ * in subcomponent literals.
9
+ *
10
+ * - Encodes delimiter characters: | → \F\, ^ → \S\, ~ → \R\, & → \T\
11
+ * - Encodes escape character: \ → \E\
12
+ * - Encodes segment delimiter: \r → \.br\
13
+ * - Uses delimiters from Root.data.delimiters if available
14
+ */
15
+ export declare const hl7v2EncodeEscapes: Plugin<[HL7v2EncodeOptions?], Root, Root>;
16
+ //# 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,UAAU,EAAE,IAAI,EAAgB,MAAM,0BAA0B,CAAC;AAE/E,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAGtC,MAAM,WAAW,kBAAkB;IACjC,UAAU,CAAC,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC;CAClC;AAED;;;;;;;;GAQG;AACH,eAAO,MAAM,kBAAkB,EAAE,MAAM,CAAC,CAAC,kBAAkB,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,CAetE,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,40 @@
1
+ // src/index.ts
2
+ import { DEFAULT_DELIMITERS } from "@rethinkhealth/hl7v2-utils";
3
+ import { visit } from "unist-util-visit";
4
+ var hl7v2EncodeEscapes = (options) => (tree) => {
5
+ const d = {
6
+ ...DEFAULT_DELIMITERS,
7
+ ...tree.data?.delimiters,
8
+ ...options?.delimiters
9
+ };
10
+ const charToEscape = buildEscapeMap(d);
11
+ visit(tree, "subcomponent", (node) => {
12
+ node.value = encode(node.value, charToEscape);
13
+ });
14
+ return tree;
15
+ };
16
+ function buildEscapeMap(d) {
17
+ return /* @__PURE__ */ new Map([
18
+ [d.escape, `${d.escape}E${d.escape}`],
19
+ [d.field, `${d.escape}F${d.escape}`],
20
+ [d.component, `${d.escape}S${d.escape}`],
21
+ [d.repetition, `${d.escape}R${d.escape}`],
22
+ [d.subcomponent, `${d.escape}T${d.escape}`],
23
+ [d.segment, `${d.escape}.br${d.escape}`]
24
+ ]);
25
+ }
26
+ function encode(value, charToEscape) {
27
+ if (!value) {
28
+ return value;
29
+ }
30
+ let encoded = "";
31
+ for (const ch of value) {
32
+ const escape = charToEscape.get(ch);
33
+ encoded += escape ?? ch;
34
+ }
35
+ return encoded;
36
+ }
37
+ export {
38
+ hl7v2EncodeEscapes
39
+ };
40
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { Delimiters, Root, Subcomponent } from \"@rethinkhealth/hl7v2-ast\";\nimport { DEFAULT_DELIMITERS } from \"@rethinkhealth/hl7v2-utils\";\nimport type { Plugin } from \"unified\";\nimport { visit } from \"unist-util-visit\";\n\nexport interface HL7v2EncodeOptions {\n delimiters?: Partial<Delimiters>;\n}\n\n/**\n * Unified plugin to encode special characters as HL7v2 escape sequences\n * in subcomponent literals.\n *\n * - Encodes delimiter characters: | → \\F\\, ^ → \\S\\, ~ → \\R\\, & → \\T\\\n * - Encodes escape character: \\ → \\E\\\n * - Encodes segment delimiter: \\r → \\.br\\\n * - Uses delimiters from Root.data.delimiters if available\n */\nexport const hl7v2EncodeEscapes: Plugin<[HL7v2EncodeOptions?], Root, Root> =\n (options) => (tree: Root) => {\n const d = {\n ...DEFAULT_DELIMITERS,\n ...(tree.data as { delimiters?: Partial<Delimiters> })?.delimiters,\n ...options?.delimiters,\n };\n\n const charToEscape = buildEscapeMap(d);\n\n visit(tree, \"subcomponent\", (node: Subcomponent) => {\n node.value = encode(node.value, charToEscape);\n });\n\n return tree;\n };\n\n/**\n * Build a map from literal characters to their HL7v2 escape sequences.\n * The escape character must be mapped first to avoid double-encoding.\n */\nfunction buildEscapeMap(\n d: typeof DEFAULT_DELIMITERS\n): ReadonlyMap<string, string> {\n return new Map<string, string>([\n [d.escape, `${d.escape}E${d.escape}`],\n [d.field, `${d.escape}F${d.escape}`],\n [d.component, `${d.escape}S${d.escape}`],\n [d.repetition, `${d.escape}R${d.escape}`],\n [d.subcomponent, `${d.escape}T${d.escape}`],\n [d.segment, `${d.escape}.br${d.escape}`],\n ]);\n}\n\n/**\n * Encode special characters in a value as HL7v2 escape sequences.\n *\n * @param value - The value to encode.\n * @param charToEscape - Map of characters to their escape sequences.\n * @returns The encoded value.\n */\nfunction encode(\n value: string,\n charToEscape: ReadonlyMap<string, string>\n): string {\n if (!value) {\n return value;\n }\n\n let encoded = \"\";\n\n for (const ch of value) {\n const escape = charToEscape.get(ch);\n encoded += escape ?? ch;\n }\n\n return encoded;\n}\n"],"mappings":";AACA,SAAS,0BAA0B;AAEnC,SAAS,aAAa;AAef,IAAM,qBACX,CAAC,YAAY,CAAC,SAAe;AAC3B,QAAM,IAAI;AAAA,IACR,GAAG;AAAA,IACH,GAAI,KAAK,MAA+C;AAAA,IACxD,GAAG,SAAS;AAAA,EACd;AAEA,QAAM,eAAe,eAAe,CAAC;AAErC,QAAM,MAAM,gBAAgB,CAAC,SAAuB;AAClD,SAAK,QAAQ,OAAO,KAAK,OAAO,YAAY;AAAA,EAC9C,CAAC;AAED,SAAO;AACT;AAMF,SAAS,eACP,GAC6B;AAC7B,SAAO,oBAAI,IAAoB;AAAA,IAC7B,CAAC,EAAE,QAAQ,GAAG,EAAE,MAAM,IAAI,EAAE,MAAM,EAAE;AAAA,IACpC,CAAC,EAAE,OAAO,GAAG,EAAE,MAAM,IAAI,EAAE,MAAM,EAAE;AAAA,IACnC,CAAC,EAAE,WAAW,GAAG,EAAE,MAAM,IAAI,EAAE,MAAM,EAAE;AAAA,IACvC,CAAC,EAAE,YAAY,GAAG,EAAE,MAAM,IAAI,EAAE,MAAM,EAAE;AAAA,IACxC,CAAC,EAAE,cAAc,GAAG,EAAE,MAAM,IAAI,EAAE,MAAM,EAAE;AAAA,IAC1C,CAAC,EAAE,SAAS,GAAG,EAAE,MAAM,MAAM,EAAE,MAAM,EAAE;AAAA,EACzC,CAAC;AACH;AASA,SAAS,OACP,OACA,cACQ;AACR,MAAI,CAAC,OAAO;AACV,WAAO;AAAA,EACT;AAEA,MAAI,UAAU;AAEd,aAAW,MAAM,OAAO;AACtB,UAAM,SAAS,aAAa,IAAI,EAAE;AAClC,eAAW,UAAU;AAAA,EACvB;AAEA,SAAO;AACT;","names":[]}
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@rethinkhealth/hl7v2-encode-escapes",
3
+ "version": "0.7.0",
4
+ "description": "hl7v2 plugin to encode special characters as hl7v2 escape sequences",
5
+ "keywords": [
6
+ "health",
7
+ "healthcare",
8
+ "hl7",
9
+ "hl7v2",
10
+ "nodejs",
11
+ "typescript"
12
+ ],
13
+ "homepage": "https://www.rethinkhealth.io/hl7v2/docs",
14
+ "license": "MIT",
15
+ "author": {
16
+ "name": "Melek Somai",
17
+ "email": "melek@rethinkhealth.io"
18
+ },
19
+ "repository": "rethinkhealth/hl7v2.git",
20
+ "files": [
21
+ "dist"
22
+ ],
23
+ "type": "module",
24
+ "types": "./dist/index.d.ts",
25
+ "exports": {
26
+ ".": "./dist/index.js"
27
+ },
28
+ "publishConfig": {
29
+ "access": "public"
30
+ },
31
+ "dependencies": {
32
+ "unified": "11.0.5",
33
+ "unist-util-visit": "5.0.0",
34
+ "@rethinkhealth/hl7v2-utils": "0.7.0"
35
+ },
36
+ "devDependencies": {
37
+ "@types/node": "^24.10.1",
38
+ "@types/unist": "^3.0.3",
39
+ "@vitest/coverage-v8": "4.0.18",
40
+ "tsup": "^8.5.1",
41
+ "typescript": "^5.9.3",
42
+ "vitest": "4.1.0",
43
+ "@rethinkhealth/hl7v2-ast": "0.7.0",
44
+ "@rethinkhealth/hl7v2-builder": "0.7.0",
45
+ "@rethinkhealth/hl7v2-decode-escapes": "0.7.0",
46
+ "@rethinkhealth/testing": "0.0.2",
47
+ "@rethinkhealth/tsconfig": "0.0.1"
48
+ },
49
+ "engines": {
50
+ "node": ">=18"
51
+ },
52
+ "packageManager": "pnpm@10.14.0",
53
+ "scripts": {
54
+ "bench": "vitest bench --run",
55
+ "build": "tsup && tsc --emitDeclarationOnly",
56
+ "check-types": "tsc --noEmit",
57
+ "test": "vitest run",
58
+ "test:coverage": "vitest run --coverage",
59
+ "test:watch": "vitest"
60
+ }
61
+ }