@rethinkhealth/hl7v2-decode-escapes 0.12.0 → 0.13.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.
package/dist/index.d.ts CHANGED
@@ -10,7 +10,11 @@ export interface HL7v2DecodeOptions {
10
10
  * - Decodes \Xdddd\ hex escapes
11
11
  * - Handles \.br\ line breaks
12
12
  * - Strips highlighting markers (\H\, \N\)
13
- * - Uses delimiters from Root.data.delimiters if available
13
+ *
14
+ * Delimiter resolution order:
15
+ * 1. `options.delimiters` (explicit override)
16
+ * 2. `file.data.delimiters` (set by hl7v2-annotate-delimiters)
17
+ * 3. `DEFAULT_DELIMITERS`
14
18
  */
15
19
  export declare const hl7v2DecodeEscapes: Plugin<[HL7v2DecodeOptions?], Root, Root>;
16
20
  //# sourceMappingURL=index.d.ts.map
@@ -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,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,CActE,CAAC"}
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;AAItC,MAAM,WAAW,kBAAkB;IACjC,UAAU,CAAC,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC;CAClC;AAED;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,kBAAkB,EAAE,MAAM,CAAC,CAAC,kBAAkB,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,CActE,CAAC"}
package/dist/index.js CHANGED
@@ -1,85 +1,90 @@
1
- // src/index.ts
2
1
  import { DEFAULT_DELIMITERS } from "@rethinkhealth/hl7v2-utils";
3
2
  import { visit } from "unist-util-visit";
4
- var hl7v2DecodeEscapes = (options) => (tree) => {
5
- const d = {
6
- ...DEFAULT_DELIMITERS,
7
- ...tree.data?.delimiters,
8
- ...options?.delimiters
9
- };
10
- visit(tree, "subcomponent", (node) => {
11
- const raw = node.value;
12
- node.value = decode(raw, d);
13
- });
14
- return tree;
3
+ //#region src/index.ts
4
+ /**
5
+ * Unified plugin to decode HL7v2 escape sequences in subcomponent literals.
6
+ *
7
+ * - Decodes \F\, \S\, \T\, \R\, \E\
8
+ * - Decodes \Xdddd\ hex escapes
9
+ * - Handles \.br\ line breaks
10
+ * - Strips highlighting markers (\H\, \N\)
11
+ *
12
+ * Delimiter resolution order:
13
+ * 1. `options.delimiters` (explicit override)
14
+ * 2. `file.data.delimiters` (set by hl7v2-annotate-delimiters)
15
+ * 3. `DEFAULT_DELIMITERS`
16
+ */
17
+ const hl7v2DecodeEscapes = (options) => (tree, file) => {
18
+ const d = {
19
+ ...file.data.delimiters ?? DEFAULT_DELIMITERS,
20
+ ...options?.delimiters
21
+ };
22
+ visit(tree, "subcomponent", (node) => {
23
+ const raw = node.value;
24
+ node.value = decode(raw, d);
25
+ });
26
+ return tree;
15
27
  };
28
+ /**
29
+ * Decode HL7v2 escape sequences according to HL7 v2.8 spec.
30
+ *
31
+ * @param value - The value to decode.
32
+ * @param d - The delimiters to use.
33
+ * @returns The decoded value.
34
+ */
16
35
  function decode(value, d) {
17
- if (!value?.includes(d.escape)) {
18
- return value;
19
- }
20
- let decoded = "";
21
- let i = 0;
22
- while (i < value.length) {
23
- if (value[i] === d.escape) {
24
- const end = value.indexOf(d.escape, i + 1);
25
- if (end === -1) {
26
- decoded += value.slice(i);
27
- break;
28
- }
29
- const code = value.slice(i + 1, end);
30
- switch (code) {
31
- case "F": {
32
- decoded += d.field;
33
- break;
34
- }
35
- case "S": {
36
- decoded += d.component;
37
- break;
38
- }
39
- case "R": {
40
- decoded += d.repetition;
41
- break;
42
- }
43
- case "T": {
44
- decoded += d.subcomponent;
45
- break;
46
- }
47
- case "E": {
48
- decoded += d.escape;
49
- break;
50
- }
51
- case ".br": {
52
- decoded += d.segment;
53
- break;
54
- }
55
- case "H":
56
- case "N": {
57
- break;
58
- }
59
- default: {
60
- decoded += code.startsWith("X") && code.length > 1 ? decodeHexSequence(code.slice(1)) : d.escape + code + d.escape;
61
- break;
62
- }
63
- }
64
- i = end + 1;
65
- } else {
66
- decoded += value[i++];
67
- }
68
- }
69
- return decoded;
36
+ if (!value?.includes(d.escape)) return value;
37
+ let decoded = "";
38
+ let i = 0;
39
+ while (i < value.length) if (value[i] === d.escape) {
40
+ const end = value.indexOf(d.escape, i + 1);
41
+ if (end === -1) {
42
+ decoded += value.slice(i);
43
+ break;
44
+ }
45
+ const code = value.slice(i + 1, end);
46
+ switch (code) {
47
+ case "F":
48
+ decoded += d.field;
49
+ break;
50
+ case "S":
51
+ decoded += d.component;
52
+ break;
53
+ case "R":
54
+ decoded += d.repetition;
55
+ break;
56
+ case "T":
57
+ decoded += d.subcomponent;
58
+ break;
59
+ case "E":
60
+ decoded += d.escape;
61
+ break;
62
+ case ".br":
63
+ decoded += d.segment;
64
+ break;
65
+ case "H":
66
+ case "N": break;
67
+ default:
68
+ decoded += code.startsWith("X") && code.length > 1 ? decodeHexSequence(code.slice(1)) : d.escape + code + d.escape;
69
+ break;
70
+ }
71
+ i = end + 1;
72
+ } else decoded += value[i++];
73
+ return decoded;
70
74
  }
75
+ /**
76
+ * Decode HL7 \Xdddd\ hexadecimal escape sequences into characters.
77
+ */
71
78
  function decodeHexSequence(hex) {
72
- let result = "";
73
- for (let i = 0; i < hex.length; i += 2) {
74
- const byte = hex.slice(i, i + 2);
75
- const codePoint = Number.parseInt(byte, 16);
76
- if (!Number.isNaN(codePoint)) {
77
- result += String.fromCodePoint(codePoint);
78
- }
79
- }
80
- return result;
79
+ let result = "";
80
+ for (let i = 0; i < hex.length; i += 2) {
81
+ const byte = hex.slice(i, i + 2);
82
+ const codePoint = Number.parseInt(byte, 16);
83
+ if (!Number.isNaN(codePoint)) result += String.fromCodePoint(codePoint);
84
+ }
85
+ return result;
81
86
  }
82
- export {
83
- hl7v2DecodeEscapes
84
- };
87
+ //#endregion
88
+ export { hl7v2DecodeEscapes };
89
+
85
90
  //# sourceMappingURL=index.js.map
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 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 * - Strips highlighting markers (\\H\\, \\N\\)\n * - Uses delimiters from Root.data.delimiters if available\n */\nexport const hl7v2DecodeEscapes: Plugin<[HL7v2DecodeOptions?], 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 visit(tree, \"subcomponent\", (node: Subcomponent) => {\n const raw = node.value;\n node.value = decode(raw, d);\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 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;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,MAAM,gBAAgB,CAAC,SAAuB;AAClD,UAAM,MAAM,KAAK;AACjB,SAAK,QAAQ,OAAO,KAAK,CAAC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AM,GAAG,IAAI,CAAC;AAC/B,UAAM,YAAY,OAAO,SAAS,MAAM,EAAE;AAC1C,QAAI,CAAC,OAAO,MAAM,SAAS,GAAG;AAC5B,gBAAU,OAAO,cAAc,SAAS;AAAA,IAC1C;AAAA,EACF;AACA,SAAO;AACT;","names":[]}
1
+ {"version":3,"file":"index.js","names":[],"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\";\nimport type { VFile } from \"vfile\";\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 * - Strips highlighting markers (\\H\\, \\N\\)\n *\n * Delimiter resolution order:\n * 1. `options.delimiters` (explicit override)\n * 2. `file.data.delimiters` (set by hl7v2-annotate-delimiters)\n * 3. `DEFAULT_DELIMITERS`\n */\nexport const hl7v2DecodeEscapes: Plugin<[HL7v2DecodeOptions?], Root, Root> =\n (options) => (tree: Root, file: VFile) => {\n const d = {\n ...((file.data.delimiters as Delimiters | undefined) ??\n DEFAULT_DELIMITERS),\n ...options?.delimiters,\n };\n\n visit(tree, \"subcomponent\", (node: Subcomponent) => {\n const raw = node.value;\n node.value = decode(raw, d);\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: 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 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":";;;;;;;;;;;;;;;;AAuBA,MAAa,sBACV,aAAa,MAAY,SAAgB;CACxC,MAAM,IAAI;EACR,GAAK,KAAK,KAAK,cACb;EACF,GAAG,SAAS;EACb;AAED,OAAM,MAAM,iBAAiB,SAAuB;EAClD,MAAM,MAAM,KAAK;AACjB,OAAK,QAAQ,OAAO,KAAK,EAAE;GAC3B;AAEF,QAAO;;;;;;;;;AAWX,SAAS,OAAO,OAAe,GAAuB;AACpD,KAAI,CAAC,OAAO,SAAS,EAAE,OAAO,CAC5B,QAAO;CAGT,IAAI,UAAU;CACd,IAAI,IAAI;AAER,QAAO,IAAI,MAAM,OACf,KAAI,MAAM,OAAO,EAAE,QAAQ;EACzB,MAAM,MAAM,MAAM,QAAQ,EAAE,QAAQ,IAAI,EAAE;AAC1C,MAAI,QAAQ,IAAI;AACd,cAAW,MAAM,MAAM,EAAE;AACzB;;EAGF,MAAM,OAAO,MAAM,MAAM,IAAI,GAAG,IAAI;AAEpC,UAAQ,MAAR;GACE,KAAK;AACH,eAAW,EAAE;AACb;GAEF,KAAK;AACH,eAAW,EAAE;AACb;GAEF,KAAK;AACH,eAAW,EAAE;AACb;GAEF,KAAK;AACH,eAAW,EAAE;AACb;GAEF,KAAK;AACH,eAAW,EAAE;AACb;GAEF,KAAK;AACH,eAAW,EAAE;AACb;GAEF,KAAK;GACL,KAAK,IACH;GAEF;AACE,eACE,KAAK,WAAW,IAAI,IAAI,KAAK,SAAS,IAClC,kBAAkB,KAAK,MAAM,EAAE,CAAC,GAChC,EAAE,SAAS,OAAO,EAAE;AAC1B;;AAIJ,MAAI,MAAM;OAGV,YAAW,MAAM;AAIrB,QAAO;;;;;AAMT,SAAS,kBAAkB,KAAqB;CAC9C,IAAI,SAAS;AACb,MAAK,IAAI,IAAI,GAAG,IAAI,IAAI,QAAQ,KAAK,GAAG;EACtC,MAAM,OAAO,IAAI,MAAM,GAAG,IAAI,EAAE;EAChC,MAAM,YAAY,OAAO,SAAS,MAAM,GAAG;AAC3C,MAAI,CAAC,OAAO,MAAM,UAAU,CAC1B,WAAU,OAAO,cAAc,UAAU;;AAG7C,QAAO"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rethinkhealth/hl7v2-decode-escapes",
3
- "version": "0.12.0",
3
+ "version": "0.13.2",
4
4
  "description": "hl7v2 plugin to decode hl7v2 escape sequences",
5
5
  "keywords": [
6
6
  "health",
@@ -32,17 +32,18 @@
32
32
  "unified": "11.0.5",
33
33
  "unist-util-visit": "5.1.0",
34
34
  "unist-util-visit-parents": "6.0.2",
35
- "@rethinkhealth/hl7v2-utils": "0.12.0"
35
+ "@rethinkhealth/hl7v2-utils": "0.13.2"
36
36
  },
37
37
  "devDependencies": {
38
38
  "@types/node": "^25.5.0",
39
39
  "@types/unist": "^3.0.3",
40
40
  "@vitest/coverage-v8": "4.1.0",
41
- "tsup": "^8.5.1",
41
+ "tsdown": "0.21.7",
42
42
  "typescript": "^5.9.3",
43
+ "vfile": "^6.0.3",
43
44
  "vitest": "4.1.0",
44
- "@rethinkhealth/hl7v2-ast": "0.12.0",
45
- "@rethinkhealth/hl7v2-builder": "0.12.0",
45
+ "@rethinkhealth/hl7v2-ast": "0.13.2",
46
+ "@rethinkhealth/hl7v2-builder": "0.13.2",
46
47
  "@rethinkhealth/testing": "0.0.2",
47
48
  "@rethinkhealth/tsconfig": "0.0.1"
48
49
  },
@@ -52,7 +53,7 @@
52
53
  "packageManager": "pnpm@10.14.0",
53
54
  "scripts": {
54
55
  "bench": "vitest bench --run",
55
- "build": "tsup && tsc --emitDeclarationOnly",
56
+ "build": "tsdown && tsc --emitDeclarationOnly",
56
57
  "check-types": "tsc --noEmit",
57
58
  "test": "vitest run",
58
59
  "test:coverage": "vitest run --coverage",