@rethinkhealth/hl7v2-decode-escapes 0.4.2 → 0.5.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/README.md +19 -19
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +18 -14
- package/dist/index.js.map +1 -1
- package/package.json +22 -22
package/README.md
CHANGED
|
@@ -8,17 +8,17 @@
|
|
|
8
8
|
|
|
9
9
|
It preserves the original raw value and replaces the `value` property with the decoded text, handling:
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
- HL7 delimiter escapes (`\F\`, `\S\`, `\R\`, `\T\`, `\E\`)
|
|
12
|
+
- Hexadecimal escapes (`\Xdddd\`)
|
|
13
|
+
- Line break directives (`\.br\`)
|
|
14
|
+
- Highlighting markers (`\H\`, `\N\`)
|
|
15
15
|
|
|
16
16
|
## When should I use this?
|
|
17
17
|
|
|
18
18
|
Use this plugin when you need:
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
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
22
|
|
|
23
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
24
|
|
|
@@ -37,18 +37,18 @@ npm install @rethinkhealth/hl7v2-decode-escapes
|
|
|
37
37
|
Say we have an HL7v2 message with escapes:
|
|
38
38
|
|
|
39
39
|
```js
|
|
40
|
-
import { unified } from
|
|
41
|
-
import { hl7v2Parse } from
|
|
42
|
-
import { hl7v2DecodeLiterals } from
|
|
40
|
+
import { unified } from "unified";
|
|
41
|
+
import { hl7v2Parse } from "@rethinkhealth/hl7v2-ast";
|
|
42
|
+
import { hl7v2DecodeLiterals } from "@rethinkhealth/hl7v2-decode-escapes";
|
|
43
43
|
|
|
44
|
-
const msg = `OBX|1|TX|ID123||Patient allergic to \\F\\Peanuts\\F\\ and \\.br\\Severe reaction
|
|
44
|
+
const msg = `OBX|1|TX|ID123||Patient allergic to \\F\\Peanuts\\F\\ and \\.br\\Severe reaction`;
|
|
45
45
|
|
|
46
46
|
const file = await unified()
|
|
47
47
|
.use(hl7v2Parse)
|
|
48
48
|
.use(hl7v2DecodeLiterals)
|
|
49
|
-
.process(msg)
|
|
49
|
+
.process(msg);
|
|
50
50
|
|
|
51
|
-
console.log(String(file))
|
|
51
|
+
console.log(String(file));
|
|
52
52
|
```
|
|
53
53
|
|
|
54
54
|
Before decoding, the `subcomponent.value` contains the raw HL7 text:
|
|
@@ -67,7 +67,7 @@ After this plugin runs:
|
|
|
67
67
|
{
|
|
68
68
|
"type": "subcomponent",
|
|
69
69
|
"index": 1,
|
|
70
|
-
"value": "Patient allergic to |Peanuts| and \rSevere reaction"
|
|
70
|
+
"value": "Patient allergic to |Peanuts| and \rSevere reaction"
|
|
71
71
|
}
|
|
72
72
|
```
|
|
73
73
|
|
|
@@ -79,7 +79,7 @@ Decode HL7v2 escape sequences in literal nodes.
|
|
|
79
79
|
|
|
80
80
|
###### Parameters
|
|
81
81
|
|
|
82
|
-
|
|
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
83
|
|
|
84
84
|
###### Returns
|
|
85
85
|
|
|
@@ -87,10 +87,10 @@ Nothing (`undefined`). Mutates the AST in-place.
|
|
|
87
87
|
|
|
88
88
|
## Behavior
|
|
89
89
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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
94
|
|
|
95
95
|
## Security
|
|
96
96
|
|
|
@@ -118,4 +118,4 @@ This program is licensed to you under the terms of the [MIT License](https://ope
|
|
|
118
118
|
|
|
119
119
|
[github-code-of-conduct]: https://github.com/rethinkhealth/hl7v2/blob/main/CODE_OF_CONDUCT.md
|
|
120
120
|
[github-license]: https://github.com/rethinkhealth/hl7v2/blob/main/LICENSE
|
|
121
|
-
[github-contributing]: https://github.com/rethinkhealth/hl7v2/blob/main/CONTRIBUTING.md
|
|
121
|
+
[github-contributing]: https://github.com/rethinkhealth/hl7v2/blob/main/CONTRIBUTING.md
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { Delimiters, Root } from "@rethinkhealth/hl7v2-ast";
|
|
2
2
|
import type { Plugin } from "unified";
|
|
3
|
-
export
|
|
3
|
+
export interface HL7v2DecodeOptions {
|
|
4
4
|
delimiters?: Partial<Delimiters>;
|
|
5
|
-
}
|
|
5
|
+
}
|
|
6
6
|
/**
|
|
7
7
|
* Unified plugin to decode HL7v2 escape sequences in subcomponent literals.
|
|
8
8
|
*
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +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,
|
|
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;;;;;;;GAOG;AACH,eAAO,MAAM,kBAAkB,EAAE,MAAM,CAAC,CAAC,kBAAkB,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,CAetE,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -27,34 +27,38 @@ function decode(value, d) {
|
|
|
27
27
|
}
|
|
28
28
|
const code = value.slice(i + 1, end);
|
|
29
29
|
switch (code) {
|
|
30
|
-
case "F":
|
|
30
|
+
case "F": {
|
|
31
31
|
decoded += d.field;
|
|
32
32
|
break;
|
|
33
|
-
|
|
33
|
+
}
|
|
34
|
+
case "S": {
|
|
34
35
|
decoded += d.component;
|
|
35
36
|
break;
|
|
36
|
-
|
|
37
|
+
}
|
|
38
|
+
case "R": {
|
|
37
39
|
decoded += d.repetition;
|
|
38
40
|
break;
|
|
39
|
-
|
|
41
|
+
}
|
|
42
|
+
case "T": {
|
|
40
43
|
decoded += d.subcomponent;
|
|
41
44
|
break;
|
|
42
|
-
|
|
45
|
+
}
|
|
46
|
+
case "E": {
|
|
43
47
|
decoded += d.escape;
|
|
44
48
|
break;
|
|
45
|
-
|
|
49
|
+
}
|
|
50
|
+
case ".br": {
|
|
46
51
|
decoded += d.segment;
|
|
47
52
|
break;
|
|
53
|
+
}
|
|
48
54
|
case "H":
|
|
49
|
-
case "N":
|
|
55
|
+
case "N": {
|
|
50
56
|
break;
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
} else {
|
|
55
|
-
decoded += d.escape + code + d.escape;
|
|
56
|
-
}
|
|
57
|
+
}
|
|
58
|
+
default: {
|
|
59
|
+
decoded += code.startsWith("X") && code.length > 1 ? decodeHexSequence(code.slice(1)) : d.escape + code + d.escape;
|
|
57
60
|
break;
|
|
61
|
+
}
|
|
58
62
|
}
|
|
59
63
|
i = end + 1;
|
|
60
64
|
} else {
|
|
@@ -69,7 +73,7 @@ function decodeHexSequence(hex) {
|
|
|
69
73
|
const byte = hex.slice(i, i + 2);
|
|
70
74
|
const codePoint = Number.parseInt(byte, 16);
|
|
71
75
|
if (!Number.isNaN(codePoint)) {
|
|
72
|
-
result += String.
|
|
76
|
+
result += String.fromCodePoint(codePoint);
|
|
73
77
|
}
|
|
74
78
|
}
|
|
75
79
|
return result;
|
package/dist/index.js.map
CHANGED
|
@@ -1 +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
|
|
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 HL7v2DecodeOptions {\n delimiters?: Partial<Delimiters>;\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, Root> =\n (options) => (tree: Root) => {\n const delimiters =\n (tree.data as { delimiters?: Partial<Delimiters> })?.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 return tree;\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 }\n case \"S\": {\n decoded += d.component;\n break;\n }\n case \"R\": {\n decoded += d.repetition;\n break;\n }\n case \"T\": {\n decoded += d.subcomponent;\n break;\n }\n case \"E\": {\n decoded += d.escape;\n break;\n }\n case \".br\": {\n decoded += d.segment;\n break;\n }\n case \"H\":\n case \"N\": {\n // Highlight start/end: ignored for now\n break;\n }\n default: {\n decoded +=\n code.startsWith(\"X\") && code.length > 1\n ? decodeHexSequence(code.slice(1))\n : d.escape + code + d.escape;\n break;\n }\n }\n\n i = end + 1;\n } else {\n // oxlint-disable-next-line no-plusplus\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.fromCodePoint(codePoint);\n }\n }\n return result;\n}\n"],"mappings":";AACA,SAAS,0BAA0B;AAEnC,SAAS,aAAa;AAcf,IAAM,qBACX,CAAC,YAAY,CAAC,SAAe;AAC3B,QAAM,aACH,KAAK,MAA+C,cACrD,SAAS;AAEX,QAAM,MAAM,gBAAgB,CAAC,SAAuB;AAClD,UAAM,MAAM,KAAK;AACjB,SAAK,QAAQ,OAAO,KAAK;AAAA,MACvB,GAAG;AAAA,MACH,GAAG;AAAA,IACL,CAAC;AAAA,EACH,CAAC;AAED,SAAO;AACT;AAUF,SAAS,OAAO,OAAe,GA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E;AAC1C,QAAI,CAAC,OAAO,MAAM,SAAS,GAAG;AAC5B,gBAAU,OAAO,cAAc,SAAS;AAAA,IAC1C;AAAA,EACF;AACA,SAAO;AACT;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,55 +1,55 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rethinkhealth/hl7v2-decode-escapes",
|
|
3
|
+
"version": "0.5.0",
|
|
3
4
|
"description": "hl7v2 plugin to decode hl7v2 escape sequences",
|
|
4
|
-
"
|
|
5
|
+
"keywords": [
|
|
6
|
+
"health",
|
|
7
|
+
"healthcare",
|
|
8
|
+
"hl7",
|
|
9
|
+
"hl7v2",
|
|
10
|
+
"nodejs",
|
|
11
|
+
"typescript"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://www.rethinkhealth.io/hl7v2/docs",
|
|
5
14
|
"license": "MIT",
|
|
6
15
|
"author": {
|
|
7
16
|
"name": "Melek Somai",
|
|
8
17
|
"email": "melek@rethinkhealth.io"
|
|
9
18
|
},
|
|
10
|
-
"
|
|
11
|
-
"types": "./dist/index.d.ts",
|
|
19
|
+
"repository": "rethinkhealth/hl7v2.git",
|
|
12
20
|
"files": [
|
|
13
21
|
"dist"
|
|
14
22
|
],
|
|
23
|
+
"type": "module",
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
15
25
|
"exports": {
|
|
16
26
|
".": "./dist/index.js"
|
|
17
27
|
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
18
31
|
"dependencies": {
|
|
19
32
|
"unified": "11.0.5",
|
|
20
33
|
"unist-util-visit": "5.0.0",
|
|
21
34
|
"unist-util-visit-parents": "6.0.2",
|
|
22
|
-
"@rethinkhealth/hl7v2-utils": "0.
|
|
35
|
+
"@rethinkhealth/hl7v2-utils": "0.5.0"
|
|
23
36
|
},
|
|
24
37
|
"devDependencies": {
|
|
25
38
|
"@types/node": "24.10.1",
|
|
26
39
|
"@types/unist": "^3.0.3",
|
|
27
|
-
"@vitest/coverage-v8": "4.0.
|
|
40
|
+
"@vitest/coverage-v8": "4.0.18",
|
|
28
41
|
"tsup": "8.5.1",
|
|
29
42
|
"typescript": "^5.9.3",
|
|
30
43
|
"vitest": "4.0.14",
|
|
31
|
-
"@rethinkhealth/hl7v2-
|
|
32
|
-
"@rethinkhealth/hl7v2-
|
|
33
|
-
"@rethinkhealth/
|
|
34
|
-
"@rethinkhealth/
|
|
44
|
+
"@rethinkhealth/hl7v2-ast": "0.5.0",
|
|
45
|
+
"@rethinkhealth/hl7v2-builder": "0.5.0",
|
|
46
|
+
"@rethinkhealth/tsconfig": "0.0.1",
|
|
47
|
+
"@rethinkhealth/testing": "0.0.2"
|
|
35
48
|
},
|
|
36
49
|
"engines": {
|
|
37
50
|
"node": ">=18"
|
|
38
51
|
},
|
|
39
|
-
"repository": "rethinkhealth/hl7v2.git",
|
|
40
|
-
"homepage": "https://www.rethinkhealth.io/hl7v2/docs",
|
|
41
|
-
"keywords": [
|
|
42
|
-
"health",
|
|
43
|
-
"healthcare",
|
|
44
|
-
"hl7",
|
|
45
|
-
"hl7v2",
|
|
46
|
-
"nodejs",
|
|
47
|
-
"typescript"
|
|
48
|
-
],
|
|
49
52
|
"packageManager": "pnpm@10.14.0",
|
|
50
|
-
"publishConfig": {
|
|
51
|
-
"access": "public"
|
|
52
|
-
},
|
|
53
53
|
"scripts": {
|
|
54
54
|
"build": "tsup && tsc --emitDeclarationOnly",
|
|
55
55
|
"check-types": "tsc --noEmit",
|