-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
102 lines (92 loc) · 2.38 KB
/
utils.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
"use strict";
var moment = require("moment");
var splitZoneMilliseconds = function (hl7DateTime) {
var result = {};
var index = hl7DateTime.indexOf('-');
if (index < 0) {
index = hl7DateTime.indexOf('+');
}
if (index >= 0) {
result.zone = hl7DateTime.slice(index);
hl7DateTime = hl7DateTime.slice(0, index);
}
index = hl7DateTime.indexOf('.');
if (index >= 0) {
result.milliseconds = hl7DateTime.slice(index + 1);
hl7DateTime = hl7DateTime.slice(0, index);
}
result.dateToSecond = hl7DateTime;
return result;
};
var splitYYYYMMDDHHMMSS = function (hl7DateTime) {
var result = [];
var start = 0;
var stop = 4;
var n = hl7DateTime.length;
while (stop <= n) {
var piece = hl7DateTime.slice(start, stop);
var value = piece;
result.push(value);
start = stop;
stop += 2;
}
return result;
};
var parseHl7 = function (hl7DateTime) {
var r = splitZoneMilliseconds(hl7DateTime);
var dateArray = splitYYYYMMDDHHMMSS(r.dateToSecond);
if (r.milliseconds) {
dateArray.push(r.milliseconds);
}
return {
dateArray: dateArray,
zone: r.zone
};
};
var dateArrayZoneToMoment = (function () {
var prefix = ['', '-', '-', 'T', ':', ':', '.'];
return function (input) {
var fd = input.dateArray.reduce(function (r, piece, index) {
r = r + prefix[index] + piece;
return r;
}, "");
fd += input.zone;
var m = moment.parseZone(fd);
return m;
};
})();
var dateArrayToMoment = function (dateArray) {
var dateArrayTimeIgnored = dateArray.slice(0, 3);
var dateArrayAsNumbers = dateArrayTimeIgnored.map(function (e) {
return parseInt(e, 10);
});
if (dateArrayAsNumbers.length > 1) { // 0 based months
dateArrayAsNumbers[1] = dateArrayAsNumbers[1] - 1;
}
return moment.utc(dateArrayAsNumbers);
};
exports.hl7ToISO = function (hl7DateTime) {
var d = parseHl7(hl7DateTime);
var m = d.zone ? dateArrayZoneToMoment(d) : dateArrayToMoment(d.dateArray);
return m.toISOString();
};
exports.hl7ToPrecision = (function () {
var precisions = [
null,
'year',
'month',
'day',
'hour',
'minute',
'second',
'subsecond'
];
return function (hl7DateTime) {
var d = parseHl7(hl7DateTime);
var n = d.dateArray.length;
if ((!d.zone) && (n > 3)) {
n = 3; // ignore time when no zone
}
return (n < precisions.length) ? precisions[n] : null;
};
})();