@@ -3,13 +3,34 @@ import process from "node:process";
33
44const args = process . argv . slice ( 2 ) ;
55
6- const showLines = args . includes ( "-l" ) ;
7- const showWords = args . includes ( "-w" ) ;
8- const showBytes = args . includes ( "-c" ) ;
6+ const expandedArgs = [ ] ;
7+ for ( const arg of args ) {
8+ if ( arg . startsWith ( "-" ) && arg . length > 2 ) {
9+ for ( const char of arg . slice ( 1 ) ) {
10+ expandedArgs . push ( `-${ char } ` ) ;
11+ }
12+ } else {
13+ expandedArgs . push ( arg ) ;
14+ }
15+ }
16+
17+ const showLines = expandedArgs . includes ( "-l" ) ;
18+ const showWords = expandedArgs . includes ( "-w" ) ;
19+ const showBytes = expandedArgs . includes ( "-c" ) ;
20+
21+ const supportedFlags = [ "-l" , "-w" , "-c" ] ;
22+ const unknownFlags = expandedArgs . filter (
23+ ( arg ) => arg . startsWith ( "-" ) && ! supportedFlags . includes ( arg ) ,
24+ ) ;
25+
26+ if ( unknownFlags . length > 0 ) {
27+ console . error ( `wc: invalid option -- '${ unknownFlags [ 0 ] . slice ( 1 ) } '` ) ;
28+ process . exit ( 1 ) ;
29+ }
930
1031const noSpecificFlag = ! showLines && ! showWords && ! showBytes ;
1132
12- const filePaths = args . filter ( ( arg ) => ! arg . startsWith ( "-" ) ) ;
33+ const filePaths = expandedArgs . filter ( ( arg ) => ! arg . startsWith ( "-" ) ) ;
1334
1435if ( filePaths . length === 0 ) {
1536 console . error ( "Usage: node wc.js [-l] [-w] [-c] <file...>" ) ;
@@ -36,47 +57,29 @@ const totalLines = results.reduce((sum, r) => sum + r.lines, 0);
3657const totalWords = results . reduce ( ( sum , r ) => sum + r . words , 0 ) ;
3758const totalBytes = results . reduce ( ( sum , r ) => sum + r . bytes , 0 ) ;
3859
39- let maxNumber ;
40-
41- if ( noSpecificFlag ) {
42- maxNumber = Math . max ( totalLines , totalWords , totalBytes ) ;
43- } else if ( showLines ) {
44- maxNumber = totalLines ;
45- } else if ( showWords ) {
46- maxNumber = totalWords ;
47- } else {
48- maxNumber = totalBytes ;
60+ function getCounts ( lines , words , bytes ) {
61+ const counts = [ ] ;
62+ if ( noSpecificFlag || showLines ) counts . push ( lines ) ;
63+ if ( noSpecificFlag || showWords ) counts . push ( words ) ;
64+ if ( noSpecificFlag || showBytes ) counts . push ( bytes ) ;
65+ return counts ;
4966}
5067
51- const width = String ( maxNumber ) . length + 2 ;
68+ const maxNumber = Math . max ( ...getCounts ( totalLines , totalWords , totalBytes ) ) ;
69+
70+ const width = String ( maxNumber ) . length + 1 ;
5271
5372function formatLine ( counts , label ) {
5473 const parts = counts . map ( ( n ) => String ( n ) . padStart ( width , " " ) ) ;
5574 return parts . join ( "" ) + " " + label ;
5675}
5776
5877for ( const { filePath, lines, words, bytes } of results ) {
59- if ( noSpecificFlag ) {
60- process . stdout . write ( formatLine ( [ lines , words , bytes ] , filePath ) + "\n" ) ;
61- } else if ( showLines ) {
62- process . stdout . write ( formatLine ( [ lines ] , filePath ) + "\n" ) ;
63- } else if ( showWords ) {
64- process . stdout . write ( formatLine ( [ words ] , filePath ) + "\n" ) ;
65- } else if ( showBytes ) {
66- process . stdout . write ( formatLine ( [ bytes ] , filePath ) + "\n" ) ;
67- }
78+ const counts = getCounts ( lines , words , bytes ) ;
79+ process . stdout . write ( formatLine ( counts , filePath ) + "\n" ) ;
6880}
6981
7082if ( results . length > 1 ) {
71- if ( noSpecificFlag ) {
72- process . stdout . write (
73- formatLine ( [ totalLines , totalWords , totalBytes ] , "total" ) + "\n" ,
74- ) ;
75- } else if ( showLines ) {
76- process . stdout . write ( formatLine ( [ totalLines ] , "total" ) + "\n" ) ;
77- } else if ( showWords ) {
78- process . stdout . write ( formatLine ( [ totalWords ] , "total" ) + "\n" ) ;
79- } else if ( showBytes ) {
80- process . stdout . write ( formatLine ( [ totalBytes ] , "total" ) + "\n" ) ;
81- }
83+ const totals = getCounts ( totalLines , totalWords , totalBytes ) ;
84+ process . stdout . write ( formatLine ( totals , "total" ) + "\n" ) ;
8285}
0 commit comments