@@ -27,116 +27,92 @@ if (positionals.length === 0) {
2727 process . exit ( 1 ) ;
2828}
2929
30- const results = [ ] ;
30+ const fileCounts = [ ] ;
3131
32- // read file and calculate counts
3332for ( const path of positionals ) {
34- const content = fs . readFileSync ( path , "utf-8" ) ;
33+ fileCounts . push ( countFile ( path ) ) ;
34+ }
3535
36- const lineCount = [ ...content ] . filter ( ( char ) => char === "\n" ) . length ;
36+ const totalCounts = {
37+ lineCount : 0 ,
38+ wordCount : 0 ,
39+ byteCount : 0 ,
40+ } ;
3741
38- const trimmedContent = content . trim ( ) ;
42+ for ( const file of fileCounts ) {
43+ totalCounts . lineCount += file . lineCount ;
44+ totalCounts . wordCount += file . wordCount ;
45+ totalCounts . byteCount += file . byteCount ;
46+ }
3947
40- const wordCount =
41- trimmedContent === "" ? 0 : trimmedContent . split ( / \s + / ) . length ;
48+ // output formatting
49+ const largestByteCount =
50+ fileCounts . length > 1 ? totalCounts . byteCount : fileCounts [ 0 ] . byteCount ;
4251
43- const byteCount = Buffer . byteLength ( content ) ;
52+ const width = String ( largestByteCount ) . length ;
4453
45- results . push ( {
46- path,
47- lineCount,
48- wordCount,
49- byteCount,
50- } ) ;
54+ for ( const file of fileCounts ) {
55+ const formattedCounts = formatCounts ( file , width ) ;
56+
57+ process . stdout . write ( `${ formattedCounts } ${ file . path } \n` ) ;
5158}
5259
53- let totalLines = 0 ;
54- let totalWords = 0 ;
55- let totalBytes = 0 ;
60+ if ( fileCounts . length > 1 ) {
61+ const formattedTotals = formatCounts ( totalCounts , width ) ;
5662
57- // calculate total counts
58- for ( const result of results ) {
59- totalLines += result . lineCount ;
60- totalWords += result . wordCount ;
61- totalBytes += result . byteCount ;
63+ process . stdout . write ( `${ formattedTotals } total\n` ) ;
6264}
6365
64- const allCounts = [ ] ;
66+ function countFile ( path ) {
67+ const content = fs . readFileSync ( path , "utf-8" ) ;
6568
66- for ( const result of results ) {
67- const counts = getSelectedCounts (
68- result . lineCount ,
69- result . wordCount ,
70- result . byteCount ,
71- ) ;
69+ const lineCount = [ ...content ] . filter ( ( char ) => char === "\n" ) . length ;
7270
73- allCounts . push ( ...counts ) ;
74- }
71+ const trimmedContent = content . trim ( ) ;
7572
76- if ( results . length > 1 ) {
77- const totalCounts = getSelectedCounts ( totalLines , totalWords , totalBytes ) ;
73+ const wordCount =
74+ trimmedContent === "" ? 0 : trimmedContent . split ( / \s + / ) . length ;
7875
79- allCounts . push ( ...totalCounts ) ;
76+ const byteCount = Buffer . byteLength ( content ) ;
77+
78+ return {
79+ lineCount,
80+ wordCount,
81+ byteCount,
82+ path,
83+ } ;
8084}
8185
82- // flags handling function
8386function getSelectedCounts ( lineCount , wordCount , byteCount ) {
84- const counts = [ ] ;
87+ const selectedCounts = [ ] ;
8588
8689 if ( values . lines ) {
87- counts . push ( lineCount ) ;
90+ selectedCounts . push ( lineCount ) ;
8891 }
8992
9093 if ( values . words ) {
91- counts . push ( wordCount ) ;
94+ selectedCounts . push ( wordCount ) ;
9295 }
9396
9497 if ( values . bytes ) {
95- counts . push ( byteCount ) ;
98+ selectedCounts . push ( byteCount ) ;
9699 }
97100
98- if ( counts . length === 0 ) {
99- counts . push ( lineCount , wordCount , byteCount ) ;
101+ if ( selectedCounts . length === 0 ) {
102+ selectedCounts . push ( lineCount , wordCount , byteCount ) ;
100103 }
101104
102- return counts ;
105+ return selectedCounts ;
103106}
104107
105- // output formatting
106- let largestByteCount = 0 ;
107-
108- for ( const result of results ) {
109- if ( result . byteCount > largestByteCount ) {
110- largestByteCount = result . byteCount ;
111- }
112- }
113-
114- if ( results . length > 1 && totalBytes > largestByteCount ) {
115- largestByteCount = totalBytes ;
116- }
117-
118- const width = String ( largestByteCount ) . length ;
119-
120- for ( const result of results ) {
121- const counts = getSelectedCounts (
122- result . lineCount ,
123- result . wordCount ,
124- result . byteCount ,
108+ function formatCounts ( counts , width ) {
109+ const selectedCounts = getSelectedCounts (
110+ counts . lineCount ,
111+ counts . wordCount ,
112+ counts . byteCount ,
125113 ) ;
126114
127- const formattedCounts = counts
115+ return selectedCounts
128116 . map ( ( count ) => String ( count ) . padStart ( width ) )
129117 . join ( " " ) ;
130-
131- process . stdout . write ( `${ formattedCounts } ${ result . path } \n` ) ;
132- }
133-
134- if ( results . length > 1 ) {
135- const totalCounts = getSelectedCounts ( totalLines , totalWords , totalBytes ) ;
136-
137- const formattedTotals = totalCounts
138- . map ( ( count ) => String ( count ) . padStart ( width ) )
139- . join ( " " ) ;
140-
141- process . stdout . write ( `${ formattedTotals } total\n` ) ;
142118}
0 commit comments