11#!/usr/bin/env node
2- const fs = require ( ' node:fs' ) ;
2+ const fs = require ( " node:fs" ) ;
33// Function to count lines, words, and bytes in a file
44function countFileContent ( content ) {
5- const lines = content . split ( '\n' ) . length ; // Count lines by splitting on newline characters
6- const words = content . trim ( ) . split ( / \s + / ) . filter ( Boolean ) . length ; // Split by whitespace and filter out empty strings
7- const bytes = Buffer . byteLength ( content , ' utf8' ) ;
8- return { lines, words, bytes } ;
5+ const lines = content . split ( "\n" ) . length ; // Count lines by splitting on newline characters
6+ const words = content . trim ( ) . split ( / \s + / ) . filter ( Boolean ) . length ; // Split by whitespace and filter out empty strings
7+ const bytes = Buffer . byteLength ( content , " utf8" ) ;
8+ return { lines, words, bytes } ;
99}
1010
1111//print counts in the format of wc according to options
1212function printCounts ( filePath , counts , options ) {
13- const parts = [ ] ;
14- if ( options . line ) parts . push ( counts . lines ) ;
15- if ( options . word ) parts . push ( counts . words ) ;
16- if ( options . byte ) parts . push ( counts . bytes ) ;
17- //if no specific count options are provided, print all counts
18- if ( ! options . line && ! options . word && ! options . byte ) {
19- //default is to print all counts
20- parts . push ( counts . lines , counts . words , counts . bytes ) ;
21- }
22- console . log ( parts . join ( '\t' ) , filePath ) ;
13+ const parts = [ ] ;
14+ if ( options . line ) parts . push ( counts . lines ) ;
15+ if ( options . word ) parts . push ( counts . words ) ;
16+ if ( options . byte ) parts . push ( counts . bytes ) ;
17+
18+ console . log ( parts . join ( "\t" ) , filePath ) ;
2319}
2420
2521function main ( ) {
26- const args = process . argv . slice ( 2 ) ;
27- const options = {
28- line : false ,
29- word : false ,
30- byte : false ,
31- } ;
22+ const args = process . argv . slice ( 2 ) ;
23+ const options = {
24+ line : false ,
25+ word : false ,
26+ byte : false ,
27+ } ;
3228
33- //Separate options from file paths
34- const files = [ ] ;
35- args . forEach ( ( arg ) => {
36- if ( arg === '-l' ) options . line = true ;
37- else if ( arg === '-w' ) options . word = true ;
38- else if ( arg === '-c' ) options . byte = true ;
39- else files . push ( arg ) ;
40- } ) ;
29+ //Separate options from file paths
30+ const files = [ ] ;
31+ args . forEach ( ( arg ) => {
32+ if ( arg === "-l" ) options . line = true ;
33+ else if ( arg === "-w" ) options . word = true ;
34+ else if ( arg === "-c" ) options . byte = true ;
35+ else files . push ( arg ) ;
36+ } ) ;
4137
42- if ( files . length === 0 ) {
43- console . error ( 'No files specified' ) ;
44- process . exit ( 1 ) ;
45- }
38+ if ( files . length === 0 ) {
39+ console . error ( "No files specified" ) ;
40+ process . exit ( 1 ) ;
41+ }
42+ //If no specific count options are provided, default to all counts (lines, words, bytes)
43+ if ( ! options . line && ! options . word && ! options . byte ) {
44+ options . line = true ;
45+ options . word = true ;
46+ options . byte = true ;
47+ }
4648
47- let totalCounts = { lines : 0 , words : 0 , bytes : 0 } ;
48- const multipleFiles = files . length > 1 ;
49-
50- files . forEach ( file => {
51- try {
52- const content = fs . readFileSync ( file , 'utf-8' ) ;
53- const counts = countFileContent ( content ) ;
49+ let totalCounts = { lines : 0 , words : 0 , bytes : 0 } ;
50+ const multipleFiles = files . length > 1 ;
5451
55- // Sum counts for total if multiple files
56- totalCounts . lines += counts . lines ;
57- totalCounts . words += counts . words ;
58- totalCounts . bytes += counts . bytes ;
52+ files . forEach ( ( file ) => {
53+ try {
54+ const content = fs . readFileSync ( file , "utf-8" ) ;
55+ const counts = countFileContent ( content ) ;
5956
60- printCounts ( file , counts , options ) ;
61- } catch ( error ) {
62- console . error ( `Error reading file ${ file } : ${ error . message } ` ) ;
63- }
64- } ) ;
65-
66- // If multiple files, print total counts
67- if ( multipleFiles ) {
68- printCounts ( 'total' , totalCounts , options ) ;
57+ // Sum counts for total if multiple files
58+ totalCounts . lines += counts . lines ;
59+ totalCounts . words += counts . words ;
60+ totalCounts . bytes += counts . bytes ;
61+
62+ printCounts ( file , counts , options ) ;
63+ } catch ( error ) {
64+ console . error ( `Error reading file ${ file } : ${ error . message } ` ) ;
6965 }
66+ } ) ;
67+
68+ // If multiple files, print total counts
69+ if ( multipleFiles ) {
70+ printCounts ( "total" , totalCounts , options ) ;
71+ }
7072}
71- main ( ) ;
73+ main ( ) ;
0 commit comments