@@ -23,9 +23,14 @@ if (!options.l && !options.w && !options.c) {
2323 options . c = true ;
2424}
2525
26- const showLines = options . l ;
27- const showWords = options . w ;
28- const showBytes = options . c ;
26+ function calculateOutput ( lines , words , bytes , label ) {
27+ let output = "" ;
28+ if ( options . l ) output += `${ lines . toString ( ) . padStart ( 8 ) } ` ;
29+ if ( options . w ) output += `${ words . toString ( ) . padStart ( 8 ) } ` ;
30+ if ( options . c ) output += `${ bytes . toString ( ) . padStart ( 8 ) } ` ;
31+ if ( label ) output += ` ${ label } ` ;
32+ return output ;
33+ }
2934
3035// To support multiple files and a total
3136let totalLines = 0 ;
@@ -44,25 +49,13 @@ for (const file of files) {
4449 totalWords += wordCount ;
4550 totalBytes += byteCount ;
4651
47- let output = "" ;
48- if ( showLines ) output += `${ lineCount . toString ( ) . padStart ( 8 ) } ` ;
49- if ( showWords ) output += `${ wordCount . toString ( ) . padStart ( 8 ) } ` ;
50- if ( showBytes ) output += `${ byteCount . toString ( ) . padStart ( 8 ) } ` ;
51- output += ` ${ file } ` ;
52-
53- console . log ( output ) ;
52+ console . log ( calculateOutput ( lineCount , wordCount , byteCount , file ) ) ;
5453 } catch ( err ) {
5554 console . error ( `Error reading file ${ file } : ${ err . message } ` ) ;
5655 }
5756}
5857
5958// If multiple files were given, show the total
6059if ( files . length > 1 ) {
61- let totalOutput = "" ;
62- if ( showLines ) totalOutput += `${ totalLines . toString ( ) . padStart ( 8 ) } ` ;
63- if ( showWords ) totalOutput += `${ totalWords . toString ( ) . padStart ( 8 ) } ` ;
64- if ( showBytes ) totalOutput += `${ totalBytes . toString ( ) . padStart ( 8 ) } ` ;
65- totalOutput += " total" ;
66-
67- console . log ( totalOutput ) ;
60+ console . log ( calculateOutput ( totalLines , totalWords , totalBytes , "total" ) ) ;
6861}
0 commit comments