File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -8,12 +8,16 @@ let startIndex = 0;
88let lineNumber = 1 ;
99
1010// Check for flags
11- if ( args [ 0 ] === "-n" ) {
12- numberAllLines = true ;
13- startIndex = 1 ;
14- } else if ( args [ 0 ] === "-b" ) {
15- numberNonBlankLines = true ;
16- startIndex = 1 ;
11+ for ( let i = 0 ; i < args . length ; i ++ ) {
12+ if ( args [ i ] === "-n" ) {
13+ numberAllLines = true ;
14+ startIndex ++ ;
15+ } else if ( args [ i ] === "-b" ) {
16+ numberNonBlankLines = true ;
17+ startIndex ++ ;
18+ } else {
19+ break ;
20+ }
1721}
1822
1923// Read each file
@@ -23,7 +27,10 @@ for (let i = startIndex; i < args.length; i++) {
2327 try {
2428 let content = fs . readFileSync ( fileName , "utf8" ) ;
2529
26- let lines = content . split ( "\n" ) ;
30+ // Remove the extra empty line if the file ends with "\n"
31+ let lines = content . endsWith ( "\n" )
32+ ? content . slice ( 0 , - 1 ) . split ( "\n" )
33+ : content . split ( "\n" ) ;
2734
2835 for ( let j = 0 ; j < lines . length ; j ++ ) {
2936 let line = lines [ j ] ;
Original file line number Diff line number Diff line change @@ -29,40 +29,75 @@ if (!countLines && !countWords && !countBytes) {
2929 countBytes = true ;
3030}
3131
32+ // Totals
33+ let totalLines = 0 ;
34+ let totalWords = 0 ;
35+ let totalBytes = 0 ;
36+ let filesCounted = 0 ;
37+
3238// Function to count one file
3339function countFile ( fileName ) {
34- const content = fs . readFileSync ( fileName , "utf8" ) ;
40+ try {
41+ const content = fs . readFileSync ( fileName , "utf8" ) ;
42+
43+ let lines = content . split ( "\n" ) . length - 1 ;
44+
45+ let words = content
46+ . trim ( )
47+ . split ( / \s + / )
48+ . filter ( ( word ) => word . length > 0 ) . length ;
49+
50+ let bytes = Buffer . byteLength ( content ) ;
51+
52+ totalLines += lines ;
53+ totalWords += words ;
54+ totalBytes += bytes ;
55+ filesCounted ++ ;
56+
57+ let result = "" ;
3558
36- let lines = content . split ( "\n" ) . length - 1 ;
59+ if ( countLines ) {
60+ result += lines + " " ;
61+ }
3762
38- let words = content
39- . trim ( )
40- . split ( / \s + / )
41- . filter ( ( word ) => word . length > 0 ) . length ;
63+ if ( countWords ) {
64+ result += words + " " ;
65+ }
4266
43- let bytes = Buffer . byteLength ( content ) ;
67+ if ( countBytes ) {
68+ result += bytes + " " ;
69+ }
4470
71+ result += fileName ;
72+
73+ console . log ( result ) ;
74+ } catch ( error ) {
75+ console . log ( "Cannot read file: " + fileName ) ;
76+ }
77+ }
78+
79+ // Run wc for every file
80+ for ( let file of files ) {
81+ countFile ( file ) ;
82+ }
83+
84+ // Print totals if more than one file was counted
85+ if ( filesCounted > 1 ) {
4586 let result = "" ;
4687
4788 if ( countLines ) {
48- result += lines + " " ;
89+ result += totalLines + " " ;
4990 }
5091
5192 if ( countWords ) {
52- result += words + " " ;
93+ result += totalWords + " " ;
5394 }
5495
5596 if ( countBytes ) {
56- result += bytes + " " ;
97+ result += totalBytes + " " ;
5798 }
5899
59- result += fileName ;
100+ result += "total" ;
60101
61102 console . log ( result ) ;
62103}
63-
64- // Run wc for every file
65-
66- for ( let file of files ) {
67- countFile ( file ) ;
68- }
You can’t perform that action at this time.
0 commit comments