@rethinkhealth/hl7v2-decode-escapes 0.2.4

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,117 @@
1
+ # @rethinkhealth/hl7v2-decode-escapes
2
+
3
+ **[unified](https://unifiedjs.com/)** plugin to decode HL7v2 escape sequences in literal values.
4
+
5
+ ## What is this?
6
+
7
+ `@rethinkhealth/hl7v2-decode-escapes` is a [unified](https://unifiedjs.com/) plugin that traverses an HL7v2 syntax tree (produced by a parser such as [`@rethinkhealth/hl7v2-ast`](https://github.com/rethinkhealth/hl7v2/tree/main/packages/hl7v2-parser)) and decodes HL7v2 escape sequences in all literal values (`subcomponent` nodes).
8
+
9
+ It preserves the original raw value and replaces the `value` property with the decoded text, handling:
10
+
11
+ * HL7 delimiter escapes (`\F\`, `\S\`, `\R\`, `\T\`, `\E\`)
12
+ * Hexadecimal escapes (`\Xdddd\`)
13
+ * Line break directives (`\.br\`)
14
+ * Highlighting markers (`\H\`, `\N\`)
15
+
16
+ ## When should I use this?
17
+
18
+ Use this plugin when you need:
19
+
20
+ * Human-readable HL7v2 values with escape sequences decoded.
21
+ * To process or display HL7v2 message content where `\F\` and similar escapes should be expanded.
22
+
23
+ If you need to parse HL7v2 messages first, use [`@rethinkhealth/hl7v2-ast`](https://github.com/rethinkhealth/hl7v2/tree/main/packages/hl7v2-parser) before applying this plugin.
24
+
25
+ ## Install
26
+
27
+ This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).
28
+
29
+ In Node.js (version 16+), install with [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm):
30
+
31
+ ```sh
32
+ npm install @rethinkhealth/hl7v2-decode-escapes
33
+ ```
34
+
35
+ ## Use
36
+
37
+ Say we have an HL7v2 message with escapes:
38
+
39
+ ```js
40
+ import { unified } from 'unified'
41
+ import { hl7v2Parse } from '@rethinkhealth/hl7v2-ast'
42
+ import { hl7v2DecodeLiterals } from '@rethinkhealth/hl7v2-decode-escapes'
43
+
44
+ const msg = `OBX|1|TX|ID123||Patient allergic to \\F\\Peanuts\\F\\ and \\.br\\Severe reaction`
45
+
46
+ const file = await unified()
47
+ .use(hl7v2Parse)
48
+ .use(hl7v2DecodeLiterals)
49
+ .process(msg)
50
+
51
+ console.log(String(file))
52
+ ```
53
+
54
+ Before decoding, the `subcomponent.value` contains the raw HL7 text:
55
+
56
+ ```json
57
+ {
58
+ "type": "subcomponent",
59
+ "index": 1,
60
+ "value": "Patient allergic to \\F\\Peanuts\\F\\ and \\.br\\Severe reaction"
61
+ }
62
+ ```
63
+
64
+ After this plugin runs:
65
+
66
+ ```json
67
+ {
68
+ "type": "subcomponent",
69
+ "index": 1,
70
+ "value": "Patient allergic to |Peanuts| and \rSevere reaction",
71
+ }
72
+ ```
73
+
74
+ ## API
75
+
76
+ ### `unified().use(hl7v2DecodeLiterals[, options])`
77
+
78
+ Decode HL7v2 escape sequences in literal nodes.
79
+
80
+ ###### Parameters
81
+
82
+ * `options.delimiters` (optional) — Override delimiters. If omitted, the plugin reads them from `Root.data.delimiters` (set by the parser). Defaults to the HL7 standard (`| ^ ~ & \`).
83
+
84
+ ###### Returns
85
+
86
+ Nothing (`undefined`). Mutates the AST in-place.
87
+
88
+ ## Behavior
89
+
90
+ * Preserves original text in `node.data.raw`.
91
+ * Decodes into `node.value`.
92
+ * Uses `Root.data.delimiters` (preferred) or `options.delimiters`.
93
+ * Falls back to defaults if neither is present.
94
+
95
+ ## Security
96
+
97
+ This plugin only transforms AST nodes and does not execute code. Ensure you trust the source of HL7v2 messages before processing.
98
+
99
+ ## Contributing
100
+
101
+ We welcome contributions! Please see our [Contributing Guide](../../CONTRIBUTING.md) for more details.
102
+
103
+ 1. Fork the repository
104
+ 2. Create your feature branch (`git checkout -b feature/amazing-feature`)
105
+ 3. Commit your changes (`git commit -m 'Add some amazing feature'`)
106
+ 4. Push to the branch (`git push origin feature/amazing-feature`)
107
+ 5. Open a Pull Request
108
+
109
+ ## Code of Conduct
110
+
111
+ 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.
112
+
113
+ ## License
114
+
115
+ Copyright 2025 Rethink Health, SUARL. All rights reserved.
116
+
117
+ 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.
@@ -0,0 +1,16 @@
1
+ import type { Root } from '@rethinkhealth/hl7v2-ast';
2
+ import { type HL7v2Delimiters } from '@rethinkhealth/hl7v2-utils';
3
+ import type { Plugin } from 'unified';
4
+ export interface HL7v2DecodeOptions {
5
+ delimiters?: Partial<HL7v2Delimiters>;
6
+ }
7
+ /**
8
+ * Unified plugin to decode HL7v2 escape sequences in subcomponent literals.
9
+ *
10
+ * - Decodes \F\, \S\, \T\, \R\, \E\
11
+ * - Decodes \Xdddd\ hex escapes
12
+ * - Handles \.br\ line breaks
13
+ * - Uses delimiters from Root.data.delimiters if available
14
+ */
15
+ export declare const hl7v2DecodeEscapes: Plugin<[HL7v2DecodeOptions?], 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,IAAI,EAAgB,MAAM,0BAA0B,CAAC;AACnE,OAAO,EAEL,KAAK,eAAe,EACrB,MAAM,4BAA4B,CAAC;AACpC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAGtC,MAAM,WAAW,kBAAkB;IACjC,UAAU,CAAC,EAAE,OAAO,CAAC,eAAe,CAAC,CAAC;CACvC;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,kBAAkB,EAAE,MAAM,CAAC,CAAC,kBAAkB,CAAC,CAAC,EAAE,IAAI,CAgBlE,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,83 @@
1
+ // src/index.ts
2
+ import {
3
+ DEFAULT_DELIMITERS
4
+ } from "@rethinkhealth/hl7v2-utils";
5
+ import { visit } from "unist-util-visit";
6
+ var hl7v2DecodeEscapes = (options) => {
7
+ return (tree) => {
8
+ const delimiters = tree.data?.delimiters || options?.delimiters;
9
+ visit(tree, "subcomponent", (node) => {
10
+ const raw = node.value;
11
+ node.value = decode(raw, {
12
+ ...DEFAULT_DELIMITERS,
13
+ ...delimiters
14
+ });
15
+ });
16
+ };
17
+ };
18
+ function decode(value, d) {
19
+ if (!value?.includes(d.escape)) {
20
+ return value;
21
+ }
22
+ let decoded = "";
23
+ let i = 0;
24
+ while (i < value.length) {
25
+ if (value[i] === d.escape) {
26
+ const end = value.indexOf(d.escape, i + 1);
27
+ if (end === -1) {
28
+ decoded += value.slice(i);
29
+ break;
30
+ }
31
+ const code = value.slice(i + 1, end);
32
+ switch (code) {
33
+ case "F":
34
+ decoded += d.field;
35
+ break;
36
+ case "S":
37
+ decoded += d.component;
38
+ break;
39
+ case "R":
40
+ decoded += d.repetition;
41
+ break;
42
+ case "T":
43
+ decoded += d.subcomponent;
44
+ break;
45
+ case "E":
46
+ decoded += d.escape;
47
+ break;
48
+ case ".br":
49
+ decoded += "\r";
50
+ break;
51
+ case "H":
52
+ case "N":
53
+ break;
54
+ default:
55
+ if (code.startsWith("X") && code.length > 1) {
56
+ decoded += decodeHexSequence(code.slice(1));
57
+ } else {
58
+ decoded += d.escape + code + d.escape;
59
+ }
60
+ break;
61
+ }
62
+ i = end + 1;
63
+ } else {
64
+ decoded += value[i++];
65
+ }
66
+ }
67
+ return decoded;
68
+ }
69
+ function decodeHexSequence(hex) {
70
+ let result = "";
71
+ for (let i = 0; i < hex.length; i += 2) {
72
+ const byte = hex.slice(i, i + 2);
73
+ const codePoint = Number.parseInt(byte, 16);
74
+ if (!Number.isNaN(codePoint)) {
75
+ result += String.fromCharCode(codePoint);
76
+ }
77
+ }
78
+ return result;
79
+ }
80
+ export {
81
+ hl7v2DecodeEscapes
82
+ };
83
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { Root, Subcomponent } from '@rethinkhealth/hl7v2-ast';\nimport {\n DEFAULT_DELIMITERS,\n type HL7v2Delimiters,\n} from '@rethinkhealth/hl7v2-utils';\nimport type { Plugin } from 'unified';\nimport { visit } from 'unist-util-visit';\n\nexport interface HL7v2DecodeOptions {\n delimiters?: Partial<HL7v2Delimiters>;\n}\n\n/**\n * Unified plugin to decode HL7v2 escape sequences in subcomponent literals.\n *\n * - Decodes \\F\\, \\S\\, \\T\\, \\R\\, \\E\\\n * - Decodes \\Xdddd\\ hex escapes\n * - Handles \\.br\\ line breaks\n * - Uses delimiters from Root.data.delimiters if available\n */\nexport const hl7v2DecodeEscapes: Plugin<[HL7v2DecodeOptions?], Root> = (\n options\n) => {\n return (tree: Root) => {\n const delimiters =\n (tree.data as { delimiters?: Partial<HL7v2Delimiters> })?.delimiters ||\n options?.delimiters;\n\n visit(tree, 'subcomponent', (node: Subcomponent) => {\n const raw = node.value;\n node.value = decode(raw, {\n ...DEFAULT_DELIMITERS,\n ...delimiters,\n });\n });\n };\n};\n\n/**\n * Decode HL7v2 escape sequences according to HL7 v2.8 spec.\n *\n * @param value - The value to decode.\n * @param d - The delimiters to use.\n * @returns The decoded value.\n */\n// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: this function must handle multiple HL7v2 escape cases and is as simple as possible given the requirements\nfunction decode(value: string, d: typeof DEFAULT_DELIMITERS): string {\n if (!value?.includes(d.escape)) {\n return value;\n }\n\n let decoded = '';\n let i = 0;\n\n while (i < value.length) {\n if (value[i] === d.escape) {\n const end = value.indexOf(d.escape, i + 1);\n if (end === -1) {\n decoded += value.slice(i); // unterminated escape\n break;\n }\n\n const code = value.slice(i + 1, end);\n\n switch (code) {\n case 'F':\n decoded += d.field;\n break;\n case 'S':\n decoded += d.component;\n break;\n case 'R':\n decoded += d.repetition;\n break;\n case 'T':\n decoded += d.subcomponent;\n break;\n case 'E':\n decoded += d.escape;\n break;\n case '.br':\n decoded += '\\r'; // or '\\n' if you prefer LF\n break;\n case 'H':\n case 'N':\n // Highlight start/end: ignored for now\n break;\n default:\n if (code.startsWith('X') && code.length > 1) {\n decoded += decodeHexSequence(code.slice(1));\n } else {\n // Unknown escape: preserve as-is\n decoded += d.escape + code + d.escape;\n }\n break;\n }\n\n i = end + 1;\n } else {\n decoded += value[i++];\n }\n }\n\n return decoded;\n}\n\n/**\n * Decode HL7 \\Xdddd\\ hexadecimal escape sequences into characters.\n */\nfunction decodeHexSequence(hex: string): string {\n let result = '';\n for (let i = 0; i < hex.length; i += 2) {\n const byte = hex.slice(i, i + 2);\n const codePoint = Number.parseInt(byte, 16);\n if (!Number.isNaN(codePoint)) {\n result += String.fromCharCode(codePoint);\n }\n }\n return result;\n}\n"],"mappings":";AACA;AAAA,EACE;AAAA,OAEK;AAEP,SAAS,aAAa;AAcf,IAAM,qBAA0D,CACrE,YACG;AACH,SAAO,CAAC,SAAe;AACrB,UAAM,aACH,KAAK,MAAoD,cAC1D,SAAS;AAEX,UAAM,MAAM,gBAAgB,CAAC,SAAuB;AAClD,YAAM,MAAM,KAAK;AACjB,WAAK,QAAQ,OAAO,KAAK;AAAA,QACvB,GAAG;AAAA,QACH,GAAG;AAAA,MACL,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AACF;AAUA,SAAS,OAAO,OAAe,GAAsC;AACnE,MAAI,CAAC,OAAO,SAAS,EAAE,MAAM,GAAG;AAC9B,WAAO;AAAA,EACT;AAEA,MAAI,UAAU;AACd,MAAI,IAAI;AAER,SAAO,IAAI,MAAM,QAAQ;AACvB,QAAI,MAAM,CAAC,MAAM,EAAE,QAAQ;AACzB,YAAM,MAAM,MAAM,QAAQ,EAAE,QAAQ,IAAI,CAAC;AACzC,UAAI,QAAQ,IAAI;AACd,mBAAW,MAAM,MAAM,CAAC;AACxB;AAAA,MACF;AAEA,YAAM,OAAO,MAAM,MAAM,IAAI,GAAG,GAAG;AAEnC,cAAQ,MAAM;AAAA,QACZ,KAAK;AACH,qBAAW,EAAE;AACb;AAAA,QACF,KAAK;AACH,qBAAW,EAAE;AACb;AAAA,QACF,KAAK;AACH,qBAAW,EAAE;AACb;AAAA,QACF,KAAK;AACH,qBAAW,EAAE;AACb;AAAA,QACF,KAAK;AACH,qBAAW,EAAE;AACb;AAAA,QACF,KAAK;AACH,qBAAW;AACX;AAAA,QACF,KAAK;AAAA,QACL,KAAK;AAEH;AAAA,QACF;AACE,cAAI,KAAK,WAAW,GAAG,KAAK,KAAK,SAAS,GAAG;AAC3C,uBAAW,kBAAkB,KAAK,MAAM,CAAC,CAAC;AAAA,UAC5C,OAAO;AAEL,uBAAW,EAAE,SAAS,OAAO,EAAE;AAAA,UACjC;AACA;AAAA,MACJ;AAEA,UAAI,MAAM;AAAA,IACZ,OAAO;AACL,iBAAW,MAAM,GAAG;AAAA,IACtB;AAAA,EACF;AAEA,SAAO;AACT;AAKA,SAAS,kBAAkB,KAAqB;AAC9C,MAAI,SAAS;AACb,WAAS,IAAI,GAAG,IAAI,IAAI,QAAQ,KAAK,GAAG;AACtC,UAAM,OAAO,IAAI,MAAM,GAAG,IAAI,CAAC;AAC/B,UAAM,YAAY,OAAO,SAAS,MAAM,EAAE;AAC1C,QAAI,CAAC,OAAO,MAAM,SAAS,GAAG;AAC5B,gBAAU,OAAO,aAAa,SAAS;AAAA,IACzC;AAAA,EACF;AACA,SAAO;AACT;","names":[]}
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@rethinkhealth/hl7v2-decode-escapes",
3
+ "description": "hl7v2 plugin to decode hl7v2 escape sequences",
4
+ "version": "0.2.4",
5
+ "license": "MIT",
6
+ "author": {
7
+ "name": "Melek Somai",
8
+ "email": "melek@rethinkhealth.io"
9
+ },
10
+ "type": "module",
11
+ "types": "./dist/index.d.ts",
12
+ "files": [
13
+ "dist"
14
+ ],
15
+ "exports": {
16
+ ".": "./dist/index.js"
17
+ },
18
+ "dependencies": {
19
+ "unified": "11.0.1",
20
+ "unist-util-visit": "^5.0.0",
21
+ "unist-util-visit-parents": "^6.0.1",
22
+ "@rethinkhealth/hl7v2-utils": "0.2.4"
23
+ },
24
+ "devDependencies": {
25
+ "@types/node": "22.15.31",
26
+ "@types/unist": "^3.0.3",
27
+ "@vitest/coverage-c8": "^0.33.0",
28
+ "@vitest/coverage-v8": "^3.2.4",
29
+ "tsup": "8.5.0",
30
+ "typescript": "^5.8.3",
31
+ "vitest": "^3.2.4",
32
+ "@rethinkhealth/hl7v2-ast": "0.2.4",
33
+ "@rethinkhealth/tsconfig": "0.0.0",
34
+ "@rethinkhealth/testing": "0.0.0"
35
+ },
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
+ "packageManager": "pnpm@10.12.1",
47
+ "publishConfig": {
48
+ "access": "public"
49
+ },
50
+ "scripts": {
51
+ "build": "tsup && tsc --emitDeclarationOnly",
52
+ "check-types": "tsc --noEmit",
53
+ "test": "vitest run",
54
+ "test:coverage": "vitest run --coverage",
55
+ "test:watch": "vitest"
56
+ }
57
+ }