@@ -5,25 +5,20 @@ import { analyzeFile, calculateComplexity } from './analyze'
55import { getFileContents , walkSync } from './traverseProject'
66import { getTimesDependedOn , getProjectDeps } from './dependencies'
77import type {
8- ParsedEntity ,
9- ParsedFile ,
10- AnalyzedFile ,
118 AnalyzedDirectory ,
129 AnalyzedEntity ,
10+ AnalyzedFile ,
11+ AssembledOptions ,
12+ FullyAnalyzedDirectory ,
1313 FullyAnalyzedEntity ,
1414 FullyAnalyzedFile ,
15- FullyAnalyzedDirectory ,
16- AssembledOptions ,
15+ ParsedEntity ,
16+ ParsedFile ,
17+ ResultsSummary ,
1718} from './types'
1819import { buildOptions , getConfiguration } from './options'
19- import { flattenEntireTree } from './util'
2020import { generateBadge } from './badge'
21-
22- export interface ResultsSummary {
23- average : number
24- median : number
25- worst : number
26- }
21+ import { getResultsAsList , getResultsSummary } from './utils'
2722
2823export interface Results {
2924 options : AssembledOptions
@@ -153,66 +148,5 @@ const analyzeProject = (rawPath: string, isCliContext?: boolean): Results => {
153148 }
154149}
155150
156- const getResultsAsList = (
157- analyzedEntities : FullyAnalyzedEntity [ ] ,
158- limit ?: number
159- ) : FullyAnalyzedFile [ ] => {
160- const flatFileResults : FullyAnalyzedFile [ ] = flattenEntireTree <
161- FullyAnalyzedFile
162- > ( analyzedEntities )
163- . filter ( ( entity ) => {
164- return entity . type === 'file' && ! ! entity . complexityReport
165- } )
166- // Sort by codehawk score, ascending (most complex files are first in the list)
167- . sort ( ( entityA , entityB ) => {
168- return (
169- entityA . complexityReport . codehawkScore -
170- entityB . complexityReport . codehawkScore
171- )
172- } )
173-
174- return limit ? flatFileResults . slice ( 0 , limit ) : flatFileResults
175- }
176-
177- const getMedian = ( numbers : number [ ] ) : number => {
178- const sorted = numbers . slice ( ) . sort ( ( a , b ) => a - b )
179- const middle = Math . floor ( sorted . length / 2 )
180-
181- if ( sorted . length % 2 === 0 ) {
182- return ( sorted [ middle - 1 ] + sorted [ middle ] ) / 2
183- }
184-
185- return sorted [ middle ]
186- }
187-
188- const getResultsSummary = (
189- resultsAsList : FullyAnalyzedFile [ ]
190- ) : ResultsSummary => {
191- const allScores : number [ ] = resultsAsList . reduce ( ( arr : number [ ] , current ) => {
192- if ( current . complexityReport ) {
193- arr . push ( current . complexityReport . codehawkScore )
194- }
195- return arr
196- } , [ ] )
197- const total = allScores . reduce ( ( total : number , score ) => {
198- return total + score
199- } , 0 )
200- const worst = allScores . reduce ( ( worst : number , score ) => {
201- if ( score < worst ) {
202- return score
203- }
204- return worst
205- } , 100 ) // Start with max maintainability, work downwards
206-
207- const average = total / allScores . length
208- const median = getMedian ( allScores )
209-
210- return {
211- average,
212- median,
213- worst,
214- }
215- }
216-
217151// Public APIs
218152export { calculateComplexity , analyzeProject , generateBadge }
0 commit comments