import { SourceFile } from 'ts-simple-ast' ; import { format } from '../helpers' ; const KEY = 'LOC (total)' ; const KEY1 = 'LOC (mean)' ; const KEY2 = 'LOC (median)' ; const KEY3 = 'LOC (mode)' ; export default function ( sourcesFiles ) { const lineNumbers = [ ] ; const loc = { } ; let total = 0 ; let mean = '0' ; let mode = '0' ; let median = '0' ; sourcesFiles . map ( sourceFile => { const endLineNumber = sourceFile . getEndLineNumber ( ) - 1 ; lineNumbers . push ( endLineNumber ) ; loc [ endLineNumber ] = ( loc [ endLineNumber ] || 0 ) + 1 ; } ) . sort ( ) ; if ( lineNumbers . length > 0 ) { total = lineNumbers . reduce ( ( a , b ) => a + b , 0 ) ; mean = ( total / lineNumbers . length ) . toFixed ( 2 ) ; let middle = ( lineNumbers . length / 2 ) | 0 ; median = lineNumbers [ middle ] . toFixed ( 2 ) ; if ( lineNumbers . length % 2 === 0 ) { median = ( ( lineNumbers [ middle ] + lineNumbers [ middle + 1 ] ) / 2 ) . toFixed ( 2 ) ; } mode = Object . keys ( loc ) . map ( Number ) . sort ( ( a , b ) => loc [ a ] - loc [ b ] ) . pop ( ) . toFixed ( 2 ) ; } return { keys : [ [ KEY , KEY1 , KEY2 , KEY3 ] . join ( '\n' ) ] , values : [ [ format ( total ) , format ( + mean ) , format ( + median ) , format ( + mode ) ] . join ( '\n' ) ] } ; }