File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed
Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ {
2+ "type" : " module"
3+ }
Original file line number Diff line number Diff line change 1+ import { promises as fs } from "node:fs" ;
2+ async function wcJsImplement ( ) {
3+ let totalLines = 0 , totalWords = 0 , TotalBytes = 0 ;
4+ let words ;
5+ const commandLineArray = process . argv . slice ( 2 ) ;
6+ console . log ( commandLineArray ) ;
7+ for ( const file of commandLineArray ) {
8+ const fileContent = await fs . readFile ( file , 'utf-8' ) ;
9+ const lines = fileContent . split ( / \r ? \n / ) ;
10+ let linesCount , wordsCount = 0 ;
11+ //Calculate count of lines
12+ if ( lines [ lines . length - 1 ] . trim ( ) === "" ) {
13+ lines . pop ( ) ;
14+ linesCount = lines . length ;
15+ totalLines += linesCount ;
16+ }
17+ //calculate count of words
18+ for ( const line of lines ) {
19+ words = line . trim ( ) . split ( / \s + / ) ;
20+ if ( words [ 0 ] . trim ( ) !== "" ) {
21+ wordsCount += words . length ;
22+ }
23+ }
24+ totalWords = + wordsCount ;
25+
26+ console . log ( file ) ;
27+ console . log ( fileContent )
28+ console . log ( lines ) ;
29+ console . log ( linesCount ) ;
30+ console . log ( totalLines ) ;
31+ console . log ( words ) ;
32+ console . log ( wordsCount ) ;
33+ console . log ( totalWords ) ;
34+ }
35+ }
36+ wcJsImplement ( ) ;
You can’t perform that action at this time.
0 commit comments