2022-09-22 07:10:08 -04:00
function isEqualArray ( a , b ) {
if ( a . length == b . length ) {
for ( var i = a . length - 1 ; i >= 0 ; -- i ) if ( a [ i ] != b [ i ] ) return false ;
return true ;
} else return false ;
}
export function lab _parse ( data ) {
data = data . join ( '\n' ) ;
if ( data == '\nNo Data Found' ) return [ ] ;
return data . split ( '\n===============================================================================\n \n' ) . map ( lab _parse1 ) . filter ( x => x ) ;
}
export function lab _reparse _results ( reports ) {
var res = [ ] , report , result ;
for ( var i = 0 ; i < reports . length ; ++ i ) {
if ( ( report = reports [ i ] ) . hasOwnProperty ( 'results' ) ) {
report = Object . assign ( { } , report ) ;
var results = report . results ;
delete report . results ;
if ( report . hasOwnProperty ( 'comment' ) ) delete report . comment ;
for ( var j = 0 ; j < results . length ; ++ j ) res . push ( result = Object . assign ( { } , report , results [ j ] ) ) ;
}
}
return res ;
}
function lab _parse1 ( data ) {
if ( data . startsWith ( '\n' ) ) return lab _parse1default ( data ) ;
if ( data . startsWith ( ' ---- MICROBIOLOGY ----\n' ) ) return lab _parse1microbiology ( data ) ;
if ( data . startsWith ( 'Performing Lab Sites\n' ) ) return null ;
}
function lab _parse1default ( data ) {
var res = { } , m , x , line ;
if ( m = data . match ( /^Report Released Date\/Time: (.*)/m ) ) res . time _released = new Date ( m [ 1 ] ) ; // 'Aug 24, 2022@07:15'
if ( m = data . match ( /^Provider: (.*)/m ) ) res . practitioner = m [ 1 ] ; // 'BARGNES,VINCENT HARRY III'
if ( m = data . match ( /^ Specimen: (.*?)\.\s*(.*)/m ) ) {
res . specimen = m [ 1 ] ; // 'SERUM'
res . accession = m [ 2 ] ; // 'CH 0800 6706'
}
if ( m = data . match ( /^ Specimen Collection Date: (.*)/m ) ) res . time _collected = new Date ( m [ 1 ] ) ; // 'Aug 24, 2022'
data = data . split ( '\n Test name Result units Ref. range Site Code\n' ) [ 1 ] . split ( '\n' ) ;
var results = res . results = [ ] ;
for ( var i = 0 ; i < data . length ; ++ i ) {
if ( ( line = data [ i ] ) . startsWith ( 'Comment: ' ) ) {
res . comment = data . slice ( i ) . join ( '\n' ) . substring ( 9 ) ;
break ;
} else if ( line . startsWith ( ' Eval: ' ) ) {
if ( results . length > 0 ) {
x = results [ results . length - 1 ] ;
if ( x . comment ) x . comment . push ( line . substring ( 12 ) ) ;
else x . comment = [ line . substring ( 12 ) ] ;
} else console . log ( 'DANGLING:' , line ) ;
2022-09-26 18:24:12 -04:00
} else if ( m = line . match ( /^\b(?<name>.*?)\s{2,}(?<value>.*?)(?: (?<flag>L\*|L|H\*|H))?\s+(?:(?<unit>.{10}) (?<range>.*?)(?: \[(?<site>\d+)\])?)?$/ ) ) {
2022-09-22 07:10:08 -04:00
if ( ( m . groups . range ) && ( m . groups . range . startsWith ( 'Ref: ' ) ) ) m . groups . range = m . groups . range . substring ( 5 ) ;
results . push ( x = m . groups ) ;
2022-09-26 18:24:12 -04:00
if ( ( x . value === '' ) && ( m = x . name . match ( /^(?<name>.*?)(?<value>(?:[\d\.]+|positive|negative|reactive|not detected|collected - specimen in lab|test not performed))$/i ) ) ) {
2022-09-26 18:05:16 -04:00
x . name = m . groups . name ;
x . value = m . groups . value ;
}
2022-09-22 07:10:08 -04:00
for ( var k in x ) if ( x [ k ] ) x [ k ] = x [ k ] ? x [ k ] . replace ( /^\s+|\s+$/g , '' ) : undefined ;
2022-09-26 18:24:12 -04:00
} else if ( m = line . match ( /^\b(?<name>.*?) (?<value>(?:[\d\.]+|positive|negative|reactive|not detected|collected - specimen in lab|test not performed))\s*$/i ) ) {
results . push ( x = m . groups ) ;
for ( var k in x ) if ( x [ k ] ) x [ k ] = x [ k ] ? x [ k ] . replace ( /^\s+|\s+$/g , '' ) : undefined ;
2022-09-22 07:10:08 -04:00
} else if ( line . startsWith ( ' [' ) ) {
if ( results . length > 0 ) results [ results . length - 1 ] . site = line . split ( '[' ) [ 1 ] . split ( ']' ) [ 0 ]
else console . log ( 'DANGLING:' , line ) ;
} else if ( line . startsWith ( ' ' ) ) {
if ( results . length > 0 ) {
x = results [ results . length - 1 ] ;
if ( line . endsWith ( ']' ) ) {
x . range = line . split ( '[' ) [ 0 ] . replace ( /^\s+|\s+$/g , '' ) ;
x . site = line . split ( '[' ) [ 1 ] . split ( ']' ) [ 0 ] ;
} else x . range = line . replace ( /^\s+|\s+$/g , '' ) ;
} else console . log ( 'DANGLING:' , line ) ;
2022-09-26 18:24:12 -04:00
} else if ( m = line . match ( /^\s{40}\b(?:(?<unit>.{10}) (?<range>.*?)(?: \[(?<site>\d+)\])?)?$/ ) ) {
if ( results . length > 0 ) {
x = results [ results . length - 1 ] ;
if ( m . groups . unit ) x . unit = m . groups . unit . replace ( /^\s+|\s+$/g , '' ) ;
if ( m . groups . range ) x . range = m . groups . range . replace ( /^\s+|\s+$/g , '' ) ;
if ( m . groups . site ) x . site = m . groups . site . replace ( /^\s+|\s+$/g , '' ) ;
} else console . log ( 'DANGLING:' , line , m . groups ) ;
2022-09-22 07:10:08 -04:00
} else console . log ( 'INVALID:' , line ) ;
}
for ( var i = results . length - 1 ; i >= 0 ; -- i ) {
results [ ( x = results [ i ] ) . name ] = x ;
if ( x . comment ) x . comment = x . comment . join ( '\n' ) ;
}
if ( ( res . accession . startsWith ( 'HE ' ) ) && ( ( results . hasOwnProperty ( 'SEGS' ) ) || ( results . hasOwnProperty ( 'BANDS' ) ) ) ) {
results . push ( results [ 'NEUTROPHIL%' ] = {
name : 'NEUTROPHIL%' , unit : '%' , range : '42.2 - 75.2' ,
value : x = ( results . hasOwnProperty ( 'SEGS' ) ? + results . SEGS . value : 0 ) + ( results . hasOwnProperty ( 'BANDS' ) ? + results . BANDS . value : 0 ) ,
flag : ( x < 42.2 ? 'L' : x > 75.2 ? 'H' : undefined )
} ) ;
results . push ( results [ 'NEUTROPHIL#' ] = {
name : 'NEUTROPHIL#' , unit : 'K/cmm' , range : '1.4 - 6.5' ,
value : + ( x = 0.01 * x * results . WBC . value ) . toFixed ( 3 ) ,
flag : ( x < 1.4 ? 'L' : x > 6.5 ? 'H' : undefined )
} ) ;
}
return res ;
}
function lab _parse1microbiology ( data ) {
var res = { } , lines = data . split ( '\n' ) , line , m ;
var idx _body = lines . indexOf ( ' ' ) ;
for ( var i = 0 ; i < lines . length ; ++ i ) {
line = lines [ i ] ;
if ( line . startsWith ( 'Accession [UID]: ' ) ) {
if ( m = line . match ( /^Accession \[UID\]: (?<accession>.*?) \[(?<accession_uid>\d+)\]/ ) ) { // 'BCUL 22 819 [3922000819]'
res . accession = m . groups . accession ;
res . accession _uid = m . groups . accession _uid ;
}
if ( m = line . match ( /Received: (.*)$/ ) ) res . time _received = new Date ( m [ 1 ] ) ; // 'Aug 01, 2022@11:57'
} else if ( line . startsWith ( 'Collection sample: ' ) ) {
res . sample = line . substring ( 0 , 39 ) . substring ( 19 ) . replace ( /^\s+|\s+$/g , '' ) ;
res . time _collected = new Date ( line . substring ( 39 ) . split ( 'Collection date: ' ) [ 1 ] . replace ( /^\s+|\s+$/g , '' ) ) ;
} else if ( line . startsWith ( 'Site/Specimen: ' ) ) {
res . specimen = line . substring ( 15 ) . replace ( /^\s+|\s+$/g , '' ) ;
} else if ( line . startsWith ( 'Provider: ' ) ) {
res . practitioner = line . substring ( 10 ) . replace ( /^\s+|\s+$/g , '' ) ;
} else if ( line . startsWith ( 'Comment on specimen:' ) ) {
res . comment = lines . slice ( i , idx _body ) . join ( '\n' ) . substring ( 20 ) . replace ( /^\s+|\s+$/g , '' ) ;
break
}
}
var idx _footer = lines . indexOf ( '=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--' )
if ( idx _footer > idx _body ) {
res . body = lines . slice ( idx _body , idx _footer ) . join ( '\n' ) . replace ( /^\s+|\s+$/g , '' ) ;
res . footer = lines . slice ( idx _footer + 1 ) . join ( '\n' ) . replace ( /^\s+|\s+$/g , '' ) ;
} else res . body = lines . slice ( idx _body ) . join ( '\n' ) . replace ( /^\s+|\s+$/g , '' ) ;
return res ;
}
export function measurement _parse ( data ) {
var extras = [ ] ;
var res = data . map ( function ( row ) {
if ( row . charAt ( 0 ) != ' ' ) {
var res = { } , idx = 0 , value , m ;
res . measurement _ien = row . substring ( 0 , idx = row . indexOf ( '^' ) ) ;
if ( res . measurement _ien == '0' ) return ; // '0^NO VITALS/MEASUREMENTS ENTERED WITHIN THIS PERIOD'
res . datetime = new Date ( row . substring ( idx + 1 , idx = row . indexOf ( ' ' , idx ) ) ) ;
res . name = row . substring ( idx + 3 , idx = row . indexOf ( ': ' , idx ) ) ;
value = row . substring ( idx + 4 , idx = row . indexOf ( ' _' , idx ) ) ;
2022-09-22 22:06:38 -04:00
res . user = row . substring ( idx + 2 ) ;
2022-09-22 07:10:08 -04:00
m = value . match ( /^(?:(.*?)(?: (\S+))?)(\*)?(?: \((?:(.*?)(?: (\S+))?)\))?\s*$/ ) ;
res . value = m [ 4 ] ? m [ 4 ] : m [ 1 ] ;
res . unit = m [ 4 ] ? m [ 5 ] : m [ 2 ] ;
res . flag = m [ 3 ] ;
res . value _american = m [ 4 ] ? m [ 1 ] : m [ 4 ] ;
res . unit _american = m [ 4 ] ? m [ 2 ] : m [ 5 ] ;
if ( res . value . charAt ( res . value . length - 1 ) == '%' ) {
res . unit = '%' ;
res . value = res . value . substring ( 0 , res . value . length - 1 ) ;
}
if ( res . name == 'B/P' ) {
var bpsplit = res . value . split ( '/' ) ;
extras . push ( { ... res , name : 'SBP' , range : '90 - 120' , unit : 'mmHg' , value : bpsplit [ 0 ] } ) ;
extras . push ( { ... res , name : 'DBP' , range : '60 - 80' , unit : 'mmHg' , value : bpsplit [ 1 ] } ) ;
}
return res ;
}
} ) . filter ( x => x ) ;
res . push ( ... extras ) ;
return res ;
}