File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ const fs = require ( "fs" ) ;
2+
3+ const args = process . argv . slice ( 2 ) ;
4+
5+ let numberAllLines = false ;
6+ let numberNonBlankLines = false ;
7+ let startIndex = 0 ;
8+ let lineNumber = 1 ;
9+
10+ // 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 ;
17+ }
18+
19+ // Read each file
20+ for ( let i = startIndex ; i < args . length ; i ++ ) {
21+ let fileName = args [ i ] ;
22+
23+ try {
24+ let content = fs . readFileSync ( fileName , "utf8" ) ;
25+
26+ let lines = content . split ( "\n" ) ;
27+
28+ for ( let j = 0 ; j < lines . length ; j ++ ) {
29+ let line = lines [ j ] ;
30+
31+ if ( numberAllLines ) {
32+ console . log ( lineNumber + " " + line ) ;
33+ lineNumber ++ ;
34+ } else if ( numberNonBlankLines ) {
35+ if ( line . trim ( ) === "" ) {
36+ console . log ( line ) ;
37+ } else {
38+ console . log ( lineNumber + " " + line ) ;
39+ lineNumber ++ ;
40+ }
41+ } else {
42+ console . log ( line ) ;
43+ }
44+ }
45+ } catch ( error ) {
46+ console . log ( "Cannot read file: " + fileName ) ;
47+ }
48+ }
Original file line number Diff line number Diff line change 1+ const fs = require ( "fs" ) ;
2+
3+ const args = process . argv . slice ( 2 ) ;
4+
5+ let onePerLine = false ;
6+ let showHidden = false ;
7+ let path = "." ; // Current directory by default
8+
9+ // Check for flags
10+ for ( let i = 0 ; i < args . length ; i ++ ) {
11+ if ( args [ i ] === "-1" ) {
12+ onePerLine = true ;
13+ } else if ( args [ i ] === "-a" ) {
14+ showHidden = true ;
15+ } else {
16+ path = args [ i ] ;
17+ }
18+ }
19+
20+ try {
21+ // Check if the path is a file
22+ if ( fs . statSync ( path ) . isFile ( ) ) {
23+ console . log ( path ) ;
24+ } else {
25+ let files = fs . readdirSync ( path ) ;
26+
27+ // Print each file
28+ for ( let i = 0 ; i < files . length ; i ++ ) {
29+ let file = files [ i ] ;
30+
31+ // Skip hidden files unless -a is used
32+ if ( ! showHidden && file . startsWith ( "." ) ) {
33+ continue ;
34+ }
35+
36+ if ( onePerLine ) {
37+ console . log ( file ) ;
38+ } else {
39+ process . stdout . write ( file + " " ) ;
40+ }
41+ }
42+
43+ if ( ! onePerLine ) {
44+ console . log ( ) ;
45+ }
46+ }
47+ } catch ( error ) {
48+ console . log ( "Cannot access: " + path ) ;
49+ }
Original file line number Diff line number Diff line change 1+ const fs = require ( "fs" ) ;
2+
3+ // Get arguments after "node wc.js"
4+ const args = process . argv . slice ( 2 ) ;
5+
6+ let countLines = false ;
7+ let countWords = false ;
8+ let countBytes = false ;
9+
10+ let files = [ ] ;
11+
12+ // Read arguments
13+ for ( let arg of args ) {
14+ if ( arg === "-l" ) {
15+ countLines = true ;
16+ } else if ( arg === "-w" ) {
17+ countWords = true ;
18+ } else if ( arg === "-c" ) {
19+ countBytes = true ;
20+ } else {
21+ files . push ( arg ) ;
22+ }
23+ }
24+
25+ // If no flags are given, wc shows all three
26+ if ( ! countLines && ! countWords && ! countBytes ) {
27+ countLines = true ;
28+ countWords = true ;
29+ countBytes = true ;
30+ }
31+
32+ // Function to count one file
33+ function countFile ( fileName ) {
34+ const content = fs . readFileSync ( fileName , "utf8" ) ;
35+
36+ let lines = content . split ( "\n" ) . length - 1 ;
37+
38+ let words = content
39+ . trim ( )
40+ . split ( / \s + / )
41+ . filter ( ( word ) => word . length > 0 ) . length ;
42+
43+ let bytes = Buffer . byteLength ( content ) ;
44+
45+ let result = "" ;
46+
47+ if ( countLines ) {
48+ result += lines + " " ;
49+ }
50+
51+ if ( countWords ) {
52+ result += words + " " ;
53+ }
54+
55+ if ( countBytes ) {
56+ result += bytes + " " ;
57+ }
58+
59+ result += fileName ;
60+
61+ console . log ( result ) ;
62+ }
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