-
Notifications
You must be signed in to change notification settings - Fork 44
/
sense.js
168 lines (139 loc) · 4.41 KB
/
sense.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//sense.js - Determining file content type e.g. CCDA or C32 or BB.json/JSON or text/other formats.
"use strict";
var xml = require("blue-button-xml").xmlUtil;
//Sense document type based on XML object
var senseXml = function (doc) {
//data must be an object
if (!doc || typeof (doc) !== "object") {
//TODO: throw a proper error here
return null;
}
var c32Result = xml.xpath(doc, 'h:templateId[@root=\"2.16.840.1.113883.3.88.11.32.1\"]');
if (c32Result && c32Result.length > 0) {
return {
type: "c32"
};
}
var cdaResult = xml.xpath(doc, 'h:templateId[@root=\"2.16.840.1.113883.10.20.1\"]');
var cdaTemplateResult = xml.xpath(doc, 'h:code[@code=\"34133-9\"][@codeSystem=\"2.16.840.1.113883.6.1\"]');
if ((cdaResult && cdaResult.length > 0) && (cdaTemplateResult && cdaTemplateResult.length > 0)) {
return {
type: "cda"
};
}
var ccdResult = xml.xpath(doc, 'h:templateId[@root=\"2.16.840.1.113883.10.20.22.1.1\"] | h:templateId[@root=\"2.16.840.1.113883.10.20.22.1.2\"]');
if (ccdResult && ccdResult.length > 0) {
return {
type: "ccda"
};
}
var ncpdpResult = xml.xpath(doc, '//Message/Body/*');
if (ncpdpResult && ncpdpResult.length > 0) {
try {
require.resolve("blue-button-ncpdp"); // check if the module is present
return {
type: "ncpdp"
};
} catch (ex) {}
}
return {
type: "xml"
};
};
//Sense document type based on String
var senseString = function (data) {
//data must be a string
if (!data || typeof (data) !== "string") {
//TODO: throw a proper error here
return null;
}
//console.log(data);
var doc;
var result;
var version = "";
var reg_exp_v;
//TODO: better xml detection needed
if (data.indexOf("<?xml") !== -1 || data.indexOf("<ClinicalDocument") !== -1) {
//parse xml object...
try {
doc = xml.parse(data);
} catch (ex) {
return {
type: "unknown",
error: ex
};
}
result = senseXml(doc);
result.xml = doc;
return result;
} else if (data.trim().indexOf("<") === 0) {
//sensing xml with no xml declaration
//TODO: there should be a better way (like comparing first and last tags and see if they match)
doc;
try {
doc = xml.parse(data);
} catch (ex) {
return {
type: "unknown",
error: ex
};
}
result = senseXml(doc);
result.xml = doc;
return result;
} else {
//parse json or determine if text object...
try {
var json = JSON.parse(data); // {}
if (json.data && json.meta) {
return {
type: "blue-button.js",
json: json
};
} else {
return {
type: "json",
json: json
};
}
} catch (e) {
//console.error("Parsing error:", e);
if (data.indexOf("MYMEDICARE.GOV PERSONAL HEALTH INFORMATION") > 0 &&
data.indexOf("Produced by the Blue Button") > 0) {
version = "";
reg_exp_v = /Produced by the Blue Button \(v(\d+\.\d+)\)/g;
version = reg_exp_v.exec(data)[1];
return {
type: "cms",
version: version
};
} else if (data.indexOf("MY HEALTHEVET PERSONAL INFORMATION REPORT") > 0) {
version = "";
reg_exp_v = /utton \(v(\d+.\d+|\d+)\)/g;
version = reg_exp_v.exec(data)[1];
return {
type: "va",
version: version
};
} else if (data.indexOf("%PDF") === 0) {
return {
type: "pdf"
};
} else if (data.indexOf("+\n Disclaimer:") > 0) {
return {
type: "format-x"
};
}
return {
type: "unknown"
};
}
}
return {
type: "unknown"
};
};
module.exports = {
senseXml: senseXml,
senseString: senseString
};