@haste-health/hl7v2-parsing 0.1.1
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 +1 -0
- package/lib/index.d.ts +8 -0
- package/lib/index.js +79 -0
- package/lib/index.js.map +1 -0
- package/package.json +24 -0
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# hl7v2-parsing
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type Hl7V2Component = string | string[];
|
|
2
|
+
export type Hl7V2Field = Hl7V2Component | Hl7V2Component[];
|
|
3
|
+
export type Hl7V2Segment = {
|
|
4
|
+
name: string;
|
|
5
|
+
fields: (Hl7V2Field | Hl7V2Field[])[];
|
|
6
|
+
};
|
|
7
|
+
export type Hl7V2Message = Record<Hl7V2Segment["name"], Hl7V2Segment["fields"]>;
|
|
8
|
+
export default function parseHl7V2Message(hl7v2Message: string): Hl7V2Message;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { OperationError, outcomeFatal } from "@haste-health/operation-outcomes";
|
|
2
|
+
function parseComponent(specialCharacters, component) {
|
|
3
|
+
const subCompoenents = component.split(specialCharacters.subComponentSeperatorCharacter);
|
|
4
|
+
return subCompoenents.length === 1 ? subCompoenents[0] : subCompoenents;
|
|
5
|
+
}
|
|
6
|
+
function parseField(specialCharacters, field) {
|
|
7
|
+
const repititions = field
|
|
8
|
+
.split(specialCharacters.repetitionCharacter)
|
|
9
|
+
.map((repitition) => {
|
|
10
|
+
const components = repitition.split(specialCharacters.seperatorCharacter);
|
|
11
|
+
return components.length === 1
|
|
12
|
+
? parseComponent(specialCharacters, components[0])
|
|
13
|
+
: components.map((c) => parseComponent(specialCharacters, c));
|
|
14
|
+
});
|
|
15
|
+
return repititions.length === 1 ? repititions[0] : repititions;
|
|
16
|
+
}
|
|
17
|
+
function parseSegment(specialCharacters, segment) {
|
|
18
|
+
const [name, ...fields] = segment.split(specialCharacters.fieldSeperator);
|
|
19
|
+
return { name, fields: fields.map((f) => parseField(specialCharacters, f)) };
|
|
20
|
+
}
|
|
21
|
+
const SegmentEnd = /\r?\n|\r|\n/;
|
|
22
|
+
/**
|
|
23
|
+
*
|
|
24
|
+
*/
|
|
25
|
+
function parseSingleMessage(hl7v2Message) {
|
|
26
|
+
const mshEnd = hl7v2Message.search(SegmentEnd);
|
|
27
|
+
const mshSegment = hl7v2Message.slice(0, mshEnd);
|
|
28
|
+
if (!mshSegment.startsWith("MSH")) {
|
|
29
|
+
throw new OperationError(outcomeFatal("invalid", "Invalid MSH Segment"));
|
|
30
|
+
}
|
|
31
|
+
const fieldSeperator = mshSegment[3];
|
|
32
|
+
// component separator, repetition separator, escape character, subcomponent separator, and truncation character.
|
|
33
|
+
// Recommneded Characters: ^~\& #
|
|
34
|
+
const specialCharEndIndex = mshSegment.indexOf(fieldSeperator, 4);
|
|
35
|
+
const specialCharString = mshSegment.slice(4, specialCharEndIndex);
|
|
36
|
+
const [seperatorCharacter, repetitionCharacter, escapeCharacter, subComponentSeperatorCharacter, truncationCharacter,] = specialCharString.split("");
|
|
37
|
+
const specialCharacters = {
|
|
38
|
+
fieldSeperator,
|
|
39
|
+
seperatorCharacter,
|
|
40
|
+
repetitionCharacter,
|
|
41
|
+
escapeCharacter,
|
|
42
|
+
subComponentSeperatorCharacter,
|
|
43
|
+
truncationCharacter,
|
|
44
|
+
};
|
|
45
|
+
// Special handling of MSH segment
|
|
46
|
+
const hl7v2MessageParsed = {
|
|
47
|
+
MSH: [
|
|
48
|
+
fieldSeperator,
|
|
49
|
+
specialCharString,
|
|
50
|
+
...mshSegment
|
|
51
|
+
.slice(specialCharEndIndex + 1)
|
|
52
|
+
.split(fieldSeperator)
|
|
53
|
+
.map((f) => parseField(specialCharacters, f)),
|
|
54
|
+
],
|
|
55
|
+
};
|
|
56
|
+
const segments = hl7v2Message
|
|
57
|
+
.slice(mshEnd + 1)
|
|
58
|
+
.split(SegmentEnd)
|
|
59
|
+
.map((segment) => parseSegment(specialCharacters, segment));
|
|
60
|
+
return segments.reduce((acc, segment) => {
|
|
61
|
+
acc[segment.name] = segment.fields;
|
|
62
|
+
return acc;
|
|
63
|
+
}, hl7v2MessageParsed);
|
|
64
|
+
}
|
|
65
|
+
export default function parseHl7V2Message(hl7v2Message) {
|
|
66
|
+
switch (true) {
|
|
67
|
+
case hl7v2Message.startsWith("MSH"): {
|
|
68
|
+
return parseSingleMessage(hl7v2Message);
|
|
69
|
+
}
|
|
70
|
+
// Batch messages
|
|
71
|
+
case hl7v2Message.startsWith("FHS"): {
|
|
72
|
+
throw new OperationError(outcomeFatal("invalid", "FHS not supported"));
|
|
73
|
+
}
|
|
74
|
+
default: {
|
|
75
|
+
throw new OperationError(outcomeFatal("invalid", "Invalid Message"));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,kCAAkC,CAAC;AAsBhF,SAAS,cAAc,CACrB,iBAAoC,EACpC,SAAiB;IAEjB,MAAM,cAAc,GAAG,SAAS,CAAC,KAAK,CACpC,iBAAiB,CAAC,8BAA8B,CACjD,CAAC;IAEF,OAAO,cAAc,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC;AAC1E,CAAC;AAED,SAAS,UAAU,CACjB,iBAAoC,EACpC,KAAa;IAEb,MAAM,WAAW,GAAG,KAAK;SACtB,KAAK,CAAC,iBAAiB,CAAC,mBAAmB,CAAC;SAC5C,GAAG,CAAC,CAAC,UAAU,EAAE,EAAE;QAClB,MAAM,UAAU,GAAG,UAAU,CAAC,KAAK,CAAC,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UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YACpC,OAAO,kBAAkB,CAAC,YAAY,CAAC,CAAC;QAC1C,CAAC;QACD,iBAAiB;QACjB,KAAK,YAAY,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YACpC,MAAM,IAAI,cAAc,CAAC,YAAY,CAAC,SAAS,EAAE,mBAAmB,CAAC,CAAC,CAAC;QACzE,CAAC;QACD,OAAO,CAAC,CAAC,CAAC;YACR,MAAM,IAAI,cAAc,CAAC,YAAY,CAAC,SAAS,EAAE,iBAAiB,CAAC,CAAC,CAAC;QACvE,CAAC;IACH,CAAC;AACH,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@haste-health/hl7v2-parsing",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "tsc",
|
|
8
|
+
"test": "pnpm node --experimental-vm-modules $(pnpm bin jest)",
|
|
9
|
+
"publish": "pnpm build && pnpm npm publish --access public --tolerate-republish"
|
|
10
|
+
},
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"@jest/globals": "^29.7.0",
|
|
13
|
+
"jest": "^29.7.0",
|
|
14
|
+
"ts-jest": "^29.3.2",
|
|
15
|
+
"typescript": "5.9.2"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"README.md",
|
|
19
|
+
"lib/**"
|
|
20
|
+
],
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@haste-health/operation-outcomes": "workspace:^"
|
|
23
|
+
}
|
|
24
|
+
}
|