-
Notifications
You must be signed in to change notification settings - Fork 1
/
highlight.js
851 lines (805 loc) · 28.5 KB
/
highlight.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
const parser = require('xml2json');
var escape = require('escape-html');
const fs = require('fs');
const moment = require('moment');
const { init } = require('express/lib/application');
const { v4: uuidv4 } = require('uuid');
// let xmlFile = fs.readFileSync('./examples/allergy/example.xml', 'utf-8');
// let jsonFile = fs.readFileSync('./examples/allergy/example.json', 'utf-8');
let template = fs.readFileSync('./template.html', 'utf-8');
let options = {
object: true,
reversible: false,
coerce: false,
sanitize: true,
trim: true,
arrayNotation: true,
alternateTextNode: false
};
// Translation of elements
const translation = {
'2.16.840.1.113883.6.96' : 'https://www.snomed.org/|http://www.snomed.org/|https://snomed.info/sct|http://snomed.info/sct',
'2.16.840.1.113883.6.88' : 'https://www.nlm.nih.gov/research/umls/rxnorm|http://www.nlm.nih.gov/research/umls/rxnorm',
'2.16.840.1.113883.6.1' : 'https://loinc.org|http://loinc.org',
'2.16.840.1.113883.6.90' : 'http://hl7.org/fhir/sid/icd-10-cm',
'2.16.840.1.113883.6.69': 'https://hl7.org/fhir/sid/ndc|http://hl7.org/fhir/sid/ndc',
'2.16.840.1.113883.6.101': 'https://terminology.hl7.org/CodeSystem/v3-nuccProviderCodes|http://terminology.hl7.org/CodeSystem/v3-nuccProviderCodes|https://nucc.org/provider-taxonomy|http://nucc.org/provider-taxonomy',
'2.16.840.1.113883.5.4': 'https://hl7.org/fhir/v3/ActCode|http://hl7.org/fhir/v3/ActCode',
'2.16.840.1.113883.6.12': 'https://www.ama-assn.org/go/cpt|http://www.ama-assn.org/go/cpt',
'2.16.840.1.113883.12.292': 'https://hl7.org/fhir/sid/cvx|http://hl7.org/fhir/sid/cvx',
'2.16.840.1.113883.6.103': 'https://hl7.org/fhir/sid/icd-9-cm|http://hl7.org/fhir/sid/icd-9-cm',
'2.16.840.1.113883.6.104': 'https://hl7.org/fhir/sid/icd-9-cm|http://hl7.org/fhir/sid/icd-9-cm',
'2.16.840.1.113883.6.4': 'https://www.icd10data.com/icd10pcs|http://www.icd10data.com/icd10pcs',
'2.16.840.1.113883.3.26.1.1': 'https://ncicb.nci.nih.gov/xml/owl/EVS/Thesaurus.owl|http://ncicb.nci.nih.gov/xml/owl/EVS/Thesaurus.owl',
'2.16.840.1.113883.5.83': 'https://hl7.org/fhir/v3/ObservationInterpretation|http://hl7.org/fhir/v3/ObservationInterpretation'
};
/*
These were not mapped above but available in VSAC
ActMood http://hl7.org/fhir/v3/ActMood 2.16.840.1.113883.5.1001
ActPriority http://hl7.org/fhir/v3/ActPriority 2.16.840.1.113883.5.7
ActReason http://hl7.org/fhir/v3/ActReason 2.16.840.1.113883.5.8
ActRelationshipType http://hl7.org/fhir/v3/ActRelationshipType 2.16.840.1.113883.5.1002
ActStatus http://hl7.org/fhir/v3/ActStatus 2.16.840.1.113883.5.14
AddressUse http://hl7.org/fhir/v3/AddressUse 2.16.840.1.113883.5.1119
AdministrativeGender http://hl7.org/fhir/v3/AdministrativeGender 2.16.840.1.113883.5.1
AdministrativeSex http://hl7.org/fhir/v2/0001 2.16.840.1.113883.18.2
CDT http://www.ada.org/cdt 2.16.840.1.113883.6.13
Confidentiality http://hl7.org/fhir/v3/Confidentiality 2.16.840.1.113883.5.25
DischargeDisposition http://hl7.org/fhir/v2/0112 2.16.840.1.113883.12.112
EntityNamePartQualifier http://hl7.org/fhir/v3/EntityNamePartQualifier 2.16.840.1.113883.5.43
EntityNameUse http://hl7.org/fhir/v3/EntityNameUse 2.16.840.1.113883.5.45
LanguageAbilityMode http://hl7.org/fhir/v3/LanguageAbilityMode 2.16.840.1.113883.5.60
LanguageAbilityProficiency http://hl7.org/fhir/v3/LanguageAbilityProficiency 2.16.840.1.113883.5.61
LivingArrangement http://hl7.org/fhir/v3/LivingArrangement 2.16.840.1.113883.5.63
MaritalStatus http://hl7.org/fhir/v3/MaritalStatus 2.16.840.1.113883.5.2
MED-RT http://www.nlm.nih.gov/research/umls/MED-RT 2.16.840.1.113883.6.345
NDFRT http://hl7.org/fhir/ndfrt 2.16.840.1.113883.3.26.1.5
NUCCPT http://nucc.org/provider-taxonomy 2.16.840.1.113883.6.101
NullFlavor http://hl7.org/fhir/v3/NullFlavor 2.16.840.1.113883.5.1008
ObservationInterpretation http://hl7.org/fhir/v3/ObservationInterpretation 2.16.840.1.113883.5.83
ObservationValue http://hl7.org/fhir/v3/ObservationValue 2.16.840.1.113883.5.1063
ParticipationFunction http://hl7.org/fhir/v3/ParticipationFunction 2.16.840.1.113883.5.88
ParticipationMode http://hl7.org/fhir/v3/ParticipationMode 2.16.840.1.113883.5.1064
ParticipationType http://hl7.org/fhir/v3/ParticipationType 2.16.840.1.113883.5.90
PresentOnAdmission https://www.cms.gov/Medicare/Medicare-Fee-for-Service-Payment/HospitalAcqCond/Coding 2.16.840.1.113883.6.301.11
ReligiousAffiliation http://hl7.org/fhir/v3/ReligiousAffiliation 2.16.840.1.113883.5.1076
RoleClass http://hl7.org/fhir/v3/RoleClass 2.16.840.1.113883.5.110
RoleCode http://hl7.org/fhir/v3/RoleCode 2.16.840.1.113883.5.111
RoleStatus http://hl7.org/fhir/v3/RoleStatus 2.16.840.1.113883.5.1068
SOP https://nahdo.org/sopt 2.16.840.1.113883.3.221.5
UCUM (Common UCUM Units) http://unitsofmeasure.org 2.16.840.1.113883.6.8
UMLS http://www.nlm.nih.gov/research/umls 2.16.840.1.113883.6.86
UNII http://fdasis.nlm.nih.gov 2.16.840.1.113883.4.9
mediaType http://hl7.org/fhir/v3/MediaType 2.16.840.1.113883.5.79
*/
// LOWER LEVEL CDA FUNTIONS
const addr = function (thing, data) {
if (thing[0].use) data.push(thing[0].use);
if (thing[0].streetAddressLine && thing[0].streetAddressLine.length) {
for (let i = 0; i < thing[0].streetAddressLine.length; i++) {
if (thing[0].streetAddressLine[i][0]) data.push(thing[0].streetAddressLine[i][0]);
}
}
if (thing[0].city && thing[0].city[0]) data.push(thing[0].city[0]);
if (thing[0].state && thing[0].state[0]) data.push(thing[0].state[0]);
if (thing[0].postalCode && thing[0].postalCode[0]) data.push(thing[0].postalCode[0]);
if (thing[0].country && thing[0].country[0]) data.push(thing[0].country[0]);
}
const code = function (thing, data) {
for (let i = 0; i < thing.length; i++) {
if (thing && thing[i]) {
if (thing[i].code) data.push(thing[i].code);
if (thing[i].codeSystem) data.push(thing[i].codeSystem);
if (thing[i].displayName) data.push(thing[i].displayName);
if (thing[i].originalText) originalText(thing[i].originalText, data);
if (thing[i].translation) code(thing[i].translation, data);
}
}
};
const consumable = function (thing, data) {
if (thing[0] && thing[0].manufacturedProduct && thing[0].manufacturedProduct[0] && thing[0].manufacturedProduct[0].manufacturedMaterial && thing[0].manufacturedProduct[0].manufacturedMaterial[0]) {
// console.log('In consumable');
console.log(thing[0].manufacturedProduct[0].manufacturedMaterial[0])
if (thing[0].manufacturedProduct[0].manufacturedMaterial[0].code) {
code(thing[0].manufacturedProduct[0].manufacturedMaterial[0].code, data);
}
if (thing[0].manufacturedProduct[0].manufacturedMaterial[0].lotNumberText && thing[0].manufacturedProduct[0].manufacturedMaterial[0].lotNumberText[0]) {
// console.log('In lotNumber');
data.push(thing[0].manufacturedProduct[0].manufacturedMaterial[0].lotNumberText[0]);
}
}
}
const effectiveTime = function (thing, data) {
for (let i = 0; i < thing.length; i++) {
if (thing[i].value) {
data.push(thing[i].value);
if (thing[i].unit) {
data.push(thing[i].unit);
}
}
else if (thing[i].period && thing[i].period[0]) {
if (thing[i].period[0].value){
data.push(thing[i].period[0].value);
}
if (thing[i].period[0].unit) {
data.push(thing[i].period[0].unit);
}
}
else {
if (thing[i].low && thing[i].low[0] && thing[i].low[0].value) {
data.push(thing[i].low[0].value);
if(thing[i].low[0].unit) data.push(thing[i].high[0].unit);
}
if (thing[i].high && thing[i].high[0] && thing[i].high[0].value) {
data.push(thing[i].high[0].value);
if(thing[i].high[0].unit) data.push(thing[i].high[0].unit);
}
}
}
}
const id = function (thing, data) {
for (let i = 0; i < thing.length; i++) {
if (thing[i].root) data.push(thing[i].root);
if (thing[i].extension) data.push(thing[i].extension);
}
}
const originalText = function (thing, data) {
if (thing[0].reference) {
if (thing[0].reference[0].value) data.push(thing[0].reference[0].value);
}
else if (typeof(thing[0]) === 'string' || thing[0] instanceof String) data.push(...thing);
}
const name = function (thing, data) {
if (thing[0].given || thing[0].family) {
if (thing[0].given && thing[0].given.length) {
for (let i = 0; i < thing[0].given.length; i++) {
if (thing[0].given[i]) data.push(thing[0].given[i]);
}
}
if (thing[0].family && thing[0].family[0]) data.push(thing[0].family[0]);
}
else if (typeof(thing[0]) === 'string' || thing[0] instanceof String) {
data.push(thing[0]);
}
}
const participant = function (thing, data) {
if (thing[0].participantRole && thing[0].participantRole[0] && thing[0].participantRole[0].playingEntity && thing[0].participantRole[0].playingEntity[0] && thing[0].participantRole[0].playingEntity[0].code) {
code(thing[0].participantRole[0].playingEntity[0].code, data);
}
}
const telecom = function (thing, data) {
if (thing[0].use) data.push(thing[0].use);
if (thing[0].value) data.push(thing[0].value);
}
const value = function (thing, data) {
for (let i = 0; i < thing.length; i++) {
if (thing[i].value) data.push(thing[i].value);
if (thing[i].unit) data.push(thing[i].unit);
if (thing[i].code) data.push(thing[i].code);
if (thing[i].codeSystem) data.push(thing[i].codeSystem);
if (thing[i].displayName) data.push(thing[i].displayName);
if (thing[i].originalText) originalText(thing[i].originalText, data);
if (thing[i].translation) code(thing[i].translation, data);
}
}
// HIGHER LEVEL CDA FUNCTIONs
const act = function (thing, data) {
if (thing[0].id) {
id(thing[0].id, data);
}
if (thing[0].code) {
code(thing[0].code, data);
}
if (thing[0].text) {
originalText(thing[0].text, data);
}
if (thing[0].statusCode) {
code(thing[0].statusCode, data);
}
if (thing[0].effectiveTime) {
effectiveTime(thing[0].effectiveTime, data);
}
if (thing[0].entryRelationship) {
entryRelationship(thing[0].entryRelationship, data)
}
if (thing[0].author){
author(thing[0].author, data);
}
if (thing[0].performer){
performer(thing[0].performer, data);
}
}
const author = function (thing, data) {
if (thing & thing[0]) {
if (thing[0].functionCode) {
code(thing[0].functionCode, data);
}
if (thing[0].code) {
code(thing[0].code, data);
}
if (thing[0].time) {
effectiveTime(thing[0].time, data);
}
if (thing[0].assignedAuthor && thing[0].assignedAuthor[0]) {
if (thing[0].assignedAuthor[0].id) {
id(thing[0].assignedAuthor[0].id, data);
}
if (thing[0].assignedAuthor[0].code) {
code(thing[0].assignedAuthor[0].code, data);
}
if (thing[0].assignedAuthor[0].addr) {
addr(thing[0].assignedAuthor[0].addr, data);
}
if (thing[0].assignedAuthor[0].telecom) {
telecom(thing[0].assignedAuthor[0].telecom, data);
}
if (thing[0].assignedAuthor[0].assignedPerson && thing[0].assignedAuthor[0].assignedPerson[0]) {
if (thing[0].assignedAuthor[0].assignedPerson[0].id) {
id(thing[0].assignedAuthor[0].assignedPerson[0].id, data)
}
if (thing[0].assignedAuthor[0].assignedPerson[0].name) {
name(thing[0].assignedAuthor[0].assignedPerson[0].name, data)
}
if (thing[0].assignedAuthor[0].assignedPerson[0].addr) {
addr(thing[0].assignedAuthor[0].assignedPerson[0].addr, data)
}
if (thing[0].assignedAuthor[0].assignedPerson[0].telecom) {
telecom(thing[0].assignedAuthor[0].assignedPerson[0].telecom, data)
}
}
}
}
}
const encounter = function (thing, data) {
if (thing[0].id) {
id(thing[0].id, data);
}
if (thing[0].code) {
code(thing[0].code, data);
}
if (thing[0].text) {
originalText(thing[0].text, data);
}
if (thing[0].statusCode) {
code(thing[0].statusCode, data);
}
if (thing[0].effectiveTime) {
effectiveTime(thing[0].effectiveTime, data);
}
if (thing[0].entryRelationship) {
entryRelationship(thing[0].entryRelationship, data)
}
if (thing[0].author){
author(thing[0].author, data);
}
if (thing[0].performer){
performer(thing[0].performer, data);
}
}
const entryRelationship = function (thing, data) {
for (let i = 0; i < thing.length; i++) {
if (thing[i].act) {
act(thing[i].act, data);
}
else if (thing[i].encounter) {
encounter(thing[i].encounter, data);
}
else if (thing[i].observation) {
observation(thing[i].observation, data);
}
else if (thing[i].organizer) {
organizer(thing[i].organizer, data);
}
else if (thing[i].procedure) {
procedure(thing[i].procedure, data);
}
else if (thing[i].substanceAdministration) {
substanceAdministration(thing[i].substanceAdministration, data);
}
else if (thing[i].supply) {
supply(thing[i].supply, data);
}
}
}
const observation = function (thing, data) {
if (thing[0].id) {
id(thing[0].id, data);
}
if (thing[0].code) {
code(thing[0].code, data);
}
if (thing[0].text) {
originalText(thing[0].text, data);
}
if (thing[0].statusCode) {
code(thing[0].statusCode, data);
}
if (thing[0].effectiveTime) {
effectiveTime(thing[0].effectiveTime, data);
}
if (thing[0].value) {
value(thing[0].value, data)
}
if (thing[0].interpretationCode) {
code(thing[0].interpretationCode, data)
}
if (thing[0].methodCode) {
code(thing[0].methodCode, data)
}
if (thing[0].targetSiteCode) {
code(thing[0].targetSiteCode, data)
}
if (thing[0].referenceRange) {
for (let j = 0; j < thing[0].referenceRange.length; j++) {
if (thing[0].referenceRange[j].text) {
}
if (thing[0].referenceRange[j].value) {
value(thing[0].referenceRange[j].value, data)
}
}
}
if (thing[0].participant){
participant(thing[0].participant, data)
}
if (thing[0].entryRelationship) {
entryRelationship(thing[0].entryRelationship, data)
}
if (thing[0].author){
author(thing[0].author, data);
}
if (thing[0].performer){
performer(thing[0].performer, data);
}
}
const organizer = function (thing, data) {
if (thing[0].id) {
id(thing[0].id, data);
}
if (thing[0].code) {
code(thing[0].code, data);
}
if (thing[0].effectiveTime) {
effectiveTime(thing[0].effectiveTime, data);
}
if (thing[0].component) {
for (let i = 0; i < thing[0].component.length; i++) {
if (thing[0].component[i].observation) {
observation(thing[0].component[i].observation, data);
}
}
}
if (thing[0].author){
author(thing[0].author, data);
}
if (thing[0].performer){
performer(thing[0].performer, data);
}
}
const performer = function (thing, data) {
if (thing && thing[0] && thing[0].assignedEntity) {
thing = thing[0].assignedEntity;
if (thing[0].id) {
id(thing[0].id, data);
}
if (thing[0].functionCode) {
code(thing[0].functionCode, data);
}
if (thing[0].code) {
code(thing[0].code, data);
}
if (thing[0].time) {
effectiveTime(thing[0].time, data);
}
if (thing[0].assignedPerson && thing[0].assignedPerson[0]) {
if (thing[0].assignedPerson[0].id) {
id(thing[0].assignedPerson[0].id, data)
}
if (thing[0].assignedPerson[0].name) {
name(thing[0].assignedPerson[0].name, data)
}
if (thing[0].assignedPerson[0].addr) {
addr(thing[0].assignedPerson[0].addr, data)
}
if (thing[0].assignedPerson[0].telecom) {
telecom(thing[0].assignedPerson[0].telecom, data)
}
}
if (thing[0].representedOrganization && thing[0].representedOrganization[0]) {
if (thing[0].representedOrganization[0].id) {
id(thing[0].representedOrganization[0].id, data)
}
if (thing[0].representedOrganization[0].name) {
name(thing[0].representedOrganization[0].name, data)
}
if (thing[0].representedOrganization[0].addr) {
addr(thing[0].representedOrganization[0].addr, data)
}
if (thing[0].representedOrganization[0].telecom) {
telecom(thing[0].representedOrganization[0].telecom, data)
}
}
}
}
const procedure = function (thing, data) {
if (thing[0].id) {
id(thing[0].id, data);
}
if (thing[0].code) {
code(thing[0].code, data);
}
if (thing[0].text) {
originalText(thing[0].text, data);
}
if (thing[0].statusCode) {
code(thing[0].statusCode, data);
}
if (thing[0].effectiveTime) {
effectiveTime(thing[0].effectiveTime, data);
}
if (thing[0].value) {
value(thing[0].value, data)
}
if (thing[0].approachSiteCode) {
code(thing[0].approachSiteCode, data)
}
if (thing[0].methodCode) {
code(thing[0].methodCode, data)
}
if (thing[0].targetSiteCode) {
code(thing[0].targetSiteCode, data)
}
if (thing[0].participant){
participant(thing[0].participant, data)
}
if (thing[0].entryRelationship) {
entryRelationship(thing[0].entryRelationship, data)
}
if (thing[0].author){
author(thing[0].author, data);
}
if (thing[0].performer){
performer(thing[0].performer, data);
}
}
const substanceAdministration = function (thing, data) {
if (thing[0].id) {
id(thing[0].id, data);
}
if (thing[0].code) {
code(thing[0].code, data);
}
if (thing[0].text) {
originalText(thing[0].text, data);
}
if (thing[0].statusCode) {
code(thing[0].statusCode, data);
}
if (thing[0].effectiveTime) {
effectiveTime(thing[0].effectiveTime, data);
}
if (thing[0].routeCode) {
code(thing[0].value, data)
}
if (thing[0].approachSiteCode) {
code(thing[0].approachSiteCode, data)
}
if (thing[0].doseQuantity) {
value(thing[0].doseQuantity, data)
}
if (thing[0].rateQuantity) {
value(thing[0].rateQuantity, data)
}
if (thing[0].maxDoseQuantity) {
value(thing[0].maxDoseQuantity, data)
}
if (thing[0].methodCode) {
code(thing[0].methodCode, data)
}
if (thing[0].targetSiteCode) {
code(thing[0].targetSiteCode, data)
}
if (thing[0].participant){
participant(thing[0].participant, data)
}
if (thing[0].consumable){
consumable(thing[0].consumable, data)
}
if (thing[0].entryRelationship) {
entryRelationship(thing[0].entryRelationship, data)
}
if (thing[0].author){
author(thing[0].author, data);
}
if (thing[0].performer){
performer(thing[0].performer, data);
}
}
const supply = function (thing, data) {
if (thing[0].id) {
id(thing[0].id, data);
}
if (thing[0].code) {
code(thing[0].code, data);
}
if (thing[0].text) {
originalText(thing[0].text, data);
}
if (thing[0].statusCode) {
code(thing[0].statusCode, data);
}
if (thing[0].effectiveTime) {
effectiveTime(thing[0].effectiveTime, data);
}
if (thing[0].quantity) {
value(thing[0].quantity, data);
}
if (thing[0].entryRelationship) {
entryRelationship(thing[0].entryRelationship, data)
}
if (thing[0].author){
author(thing[0].author, data);
}
if (thing[0].performer){
performer(thing[0].performer, data);
}
}
// Operations for Mapping
const dateTranslate = function (thing) {
let output = null
try {
thing = thing.replace('.000', '');
let newTime = moment(thing, 'YYYYMMDDHHmmssZ');
output = newTime.toISOString();
}
catch (e) {
console.log(e);
}
finally {
return output;
}
}
const match = function (fhirStuff, data) {
let matches = {
cda: [],
fhir: []
};
let prior = {};
let colorIndex = 10;
fhirStuff = fhirStuff.replace(/urn:oid:/gm, '');
for (let i = 0; i < data.length; i++) {
if (!prior[data[i]]) {
let initialLength = matches.cda.length;
if (fhirStuff.includes( '"' + data[i] + '"', 'gm') || fhirStuff.includes("'" + data[i] + "'", 'gm') ) {
matches.cda.push({string: data[i], color: colorIndex});
matches.fhir.push({string: data[i], color: colorIndex});
prior[data[i]] = true;
}
else if (fhirStuff.includes(data[i] + '"', 'gm') || fhirStuff.includes(data[i] + "'", 'gm') ) {
matches.cda.push({string: data[i], color: colorIndex});
matches.fhir.push({string: data[i], color: colorIndex});
prior[data[i]] = true;
}
else if (fhirStuff.includes(' ' + data[i] + ' ', 'gm')) {
matches.cda.push({string: data[i], color: colorIndex});
matches.fhir.push({string: data[i], color: colorIndex});
prior[data[i]] = true;
}
else if (fhirStuff.includes(' ' + data[i] + ',', 'gm')) {
matches.cda.push({string: data[i], color: colorIndex});
matches.fhir.push({string: data[i], color: colorIndex, number: true});
prior[data[i]] = true;
}
else if (translation[data[i]]) {
let pieces = translation[data[i]].split('|')
for (let j = 0; j < pieces.length; j++) {
if (fhirStuff.includes(pieces[j])) {
matches.cda.push({string: data[i], color: colorIndex});
matches.fhir.push({string: pieces[j], color: colorIndex});
prior[data[i]] = true;
}
}
}
else if (data[i].slice(0,2) === '19' || data[i].slice(0,2) === '20') {
if (data[i].length < 9) {
let re = data[i];
if (data[i].length === 8) re = data[i].slice(0,4) + '-' + data[i].slice(4,6) + '-' + data[i].slice(6,8);
else if (data[i].length === 6) re = data[i].slice(0,4) + '-' + data[i].slice(4,6);
let results = fhirStuff.match(re);
if (results && results.length) {
matches.cda.push({string: data[i], color: colorIndex});
matches.fhir.push({string: results[0], color: colorIndex});
prior[data[i]] = true;
}
}
else {
let iso = dateTranslate(data[i]);
if (iso) {
let start = iso.slice(0,11);
let end = iso.slice(13,16);
let re = new RegExp(start + '..' + end, 'gm');
let results = fhirStuff.match(re)
if (results && results.length) {
matches.cda.push({string: data[i], color: colorIndex});
matches.fhir.push({string: results[0], color: colorIndex});
prior[data[i]] = true;
}
}
}
}
if (matches.cda.length !== initialLength) colorIndex++;
}
if (colorIndex === 43) colorIndex = 10;
}
console.log(matches);
return matches;
}
const mark = function (cda, fhir, matches) {
let cdaOutput = escape(cda);
let fhirOutput = escape(fhir);
for (let i = 0; i < matches.cda.length; i++) {
let stringreplace = matches.cda[i].string;
if (stringreplace.length < 4) {
stringreplace = `"${stringreplace}"`;
}
let match = new RegExp(stringreplace, 'g');
if (!cdaOutput.match(match)) {
stringreplace = matches.cda[i].string;
stringreplace = `>${stringreplace}<`;
match = new RegExp(stringreplace, 'g');
}
// console.log(match);
// console.log(cdaOutput.match(match));
cdaOutput = cdaOutput.replace(match, `<mark class="color${matches.cda[i].color}" >${stringreplace}</mark>`)
}
for (let i = 0; i < matches.cda.length; i++) {
let stringreplace2 = matches.fhir[i].string;
if (stringreplace2.length < 4 && !matches.fhir[i].number) {
stringreplace2 = `"${stringreplace2}"`;
}
// how to handle integers, will highlight comma
else if (stringreplace2.length < 4) {
stringreplace2 = `${stringreplace2},`;
}
let match = new RegExp(stringreplace2, 'g');
fhirOutput = fhirOutput.replace(match, `<mark class="color${matches.fhir[i].color}" >${stringreplace2}</mark>`)
}
// console.log(cdaOutput);
// console.log(fhirOutput);
// console.log(template);
let newHtml = template.replace('<div id="cda" class="border codeArea">', `<div id="cda" class="border codeArea">${cdaOutput}`);
newHtml = newHtml.replace('<div id="fhir" class="border codeArea">', `<div id="fhir" class="border codeArea">${fhirOutput}`);
return newHtml;
}
// Main function
const run = function (cdaStuff, fhirStuff) {
let cda = parser.toJson(cdaStuff, options);
let fhir = null;
if (!cda.entry && !cda.recordTarget) {
return 'ERROR: Not a CDA entry or recordTarget';
}
if (typeof(fhirStuff) === 'string') {
try {
fhir = JSON.parse(fhirStuff);
}
catch (e) {
console.log(e);
}
}
else {
fhir = fhirStuff;
fhirStuff = JSON.stringify(fhir);
}
if (!fhir || !fhir.resource) {
// console.log(fhir);
return 'ERROR: Not a FHIR resource';
}
let data = [];
if (cda.recordTarget && cda.recordTarget[0] && cda.recordTarget[0].patientRole && cda.recordTarget[0].patientRole[0]) {
if (cda.recordTarget[0].patientRole[0].id) {
id(cda.recordTarget[0].patientRole[0].id, data);
}
if (cda.recordTarget[0].patientRole[0].addr) {
addr(cda.recordTarget[0].patientRole[0].addr, data);
}
if (cda.recordTarget[0].patientRole[0].telecom){
telecom(cda.recordTarget[0].patientRole[0].telecom, data);
}
if (cda.recordTarget[0].patientRole[0].patient && cda.recordTarget[0].patientRole[0].patient[0]) {
if (cda.recordTarget[0].patientRole[0].patient[0].name) {
name(cda.recordTarget[0].patientRole[0].patient[0].name, data);
}
if (cda.recordTarget[0].patientRole[0].patient[0].administrativeGenderCode) {
code(cda.recordTarget[0].patientRole[0].patient[0].administrativeGenderCode, data);
}
if (cda.recordTarget[0].patientRole[0].patient[0].birthTime) {
effectiveTime(cda.recordTarget[0].patientRole[0].patient[0].birthTime, data);
}
if (cda.recordTarget[0].patientRole[0].patient[0].raceCode) {
code(cda.recordTarget[0].patientRole[0].patient[0].raceCode, data);
}
if (cda.recordTarget[0].patientRole[0].patient[0].ethnicGroupCode) {
code(cda.recordTarget[0].patientRole[0].patient[0].ethnicGroupCode, data);
}
if (cda.recordTarget[0].patientRole[0].patient[0].languageCommunication && cda.recordTarget[0].patientRole[0].patient[0].languageCommunication[0]) {
if(cda.recordTarget[0].patientRole[0].patient[0].languageCommunication[0].languageCode) {
code(cda.recordTarget[0].patientRole[0].patient[0].languageCommunication[0].languageCode, data);
}
if(cda.recordTarget[0].patientRole[0].patient[0].languageCommunication[0].preferenceInd) {
value(cda.recordTarget[0].patientRole[0].patient[0].languageCommunication[0].preferenceInd, data);
}
}
}
}
else {
for (let i = 0; i < cda.entry.length; i++) {
if (cda.entry[i].act) {
act(cda.entry[i].act, data);
}
else if (cda.entry[i].observation) {
observation(cda.entry[i].observation, data);
}
else if (cda.entry[i].substanceAdministration) {
substanceAdministration(cda.entry[i].substanceAdministration, data);
}
else if (cda.entry[i].procedure) {
procedure(cda.entry[i].procedure, data);
}
else if (cda.entry[i].supply) {
supply(cda.entry[i].supply, data);
}
else if (cda.entry[i].organizer) {
organizer(cda.entry[i].organizer, data);
}
else if (cda.entry[i].encounter) {
encounter(cda.entry[i].encounter, data);
}
else {
console.log(`skipping ${cda} from main entry`);
}
}
}
// Process XML comments of resolves to
let r = new RegExp(/<!--[\s\S\n]*?-->/gm);
let comments = cdaStuff.match(r);
if (comments && comments.length) {
for (let i = 0; i < comments.length; i++) {
let pieces = comments[i].split(': ')
if (pieces[1]) {
let output = pieces[1].replace(' -->', '');
data.push(output);
}
}
}
console.log(data);
let matches = match(fhirStuff, data)
// console.log(matches);
let html = mark(cdaStuff, fhirStuff, matches)
// console.log(JSON.stringify(cda));
// make a record for debugging time being
let identifier = uuidv4();
if(!fs.existsSync('debug')) {
fs.mkdirSync('debug');
setTimeout(function() {
fs.writeFileSync(`./debug/${identifier}.xml`, cdaStuff);
//fs.writeFileSync(`./debug/${identifier}.html`, html);
fs.writeFileSync(`./debug/${identifier}.json`, fhirStuff);
return html;
}, 1000);
}
else {
fs.writeFileSync(`./debug/${identifier}.xml`, cdaStuff);
// fs.writeFileSync(`./debug/${identifier}.html`, html);
fs.writeFileSync(`./debug/${identifier}.json`, fhirStuff);
return html;
}
}
module.exports = run;
//run(xmlFile, jsonFile)
/*
let iso = '1980-05-01T04:00:00.000Z'
let start = iso.slice(0,11);
let end = iso.slice(13);
let re = new RegExp(start + '.*' + end, 'gm');
let z = '1980-05-01T00:00:00.000Z'
z.match(re)
*/