-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwinwc.js
173 lines (158 loc) · 5.41 KB
/
winwc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env node
const fs = require('fs');
(function(){
let cmd = process.argv.slice(2)
let options = []
let files = []
let ans = [] // represent the final result
// filter out options and files
for(let i=0;i<cmd.length;i++){
if(cmd[i].startsWith('-')){
options.push(cmd[i])
}
else if(cmd[i].startsWith("*.")==false){
files.push(cmd[i])
}
}
// push all files for *.
let currentFiles = fs.readdirSync(process.cwd()) //reading cwd files
for(let i=0;i<cmd.length;i++){
if(cmd[i].startsWith("*.")){
let ext = cmd[i].split(".").pop()
for(let j=0;j<currentFiles.length;j++){
let targetExt = currentFiles[j].split(".").pop()
if(ext==targetExt && files.includes(currentFiles[j])==false){
files.push(currentFiles[j])
}
}
}
}
// if options is empty, get all three (-l, -w, -m)
if(options.length==0){
for(let j=0;j<files.length;j++){
let str = ''
let data = []
if(fs.existsSync(files[j])){ // file exist?
str = fs.readFileSync(files[j]).toString() // read file
data.push(countLines(str)) // -l
data.push(countWords(str)) // -w
data.push(countChars(str)) // -m
data.push(files[j]) // push file name
ans.push(data)
}
else{
console.log('invalid file path')
return //cancel whole operation
}
}
if(ans.length>1){ // if more than 1 file data, then show data in tabular form
let lastRow = setLastRow(ans)
ans.push(lastRow)
}
}
else{ // else set result according to options
for(let j=0;j<files.length;j++){
let str = ''
let data = []
if(fs.existsSync(files[j])){ // file exist?
str = fs.readFileSync(files[j]).toString() // read file
if(options.includes('-l')){ // -l
data.push(countLines(str))
}
if(options.includes('-w')){ // -w
data.push(countWords(str))
}
if(options.includes('-m')){ // -m
data.push(countChars(str))
}
if(options.includes('-L')){ // -L
data.push(longestLine(str))
}
data.push(files[j]) // push file name
ans.push(data)
}
else{
console.log('invalid file path')
return //cancel whole operation
}
}
if(ans.length>1){ // if more than 1 file data, then show data in tabular form
let lastRow = setLastRow(ans)
ans.push(lastRow)
}
}
// print the final result
for(let i=0;i<ans.length;i++){
let finalRes = ''
for(let j=0;j<ans[i].length;j++){
finalRes += ' ' + ans[i][j]
}
console.log(finalRes)
}
})();
// set last row for showing data in tabular form
function setLastRow(ans){
let n = ans[0].length;
let lastRow = []
for(let i=0;i<n-1;i++){
let sum = 0
for(let j=0;j<ans.length;j++){
sum += ans[j][i] // summing column wise
}
lastRow.push(sum)
}
lastRow.push('total')
return lastRow
}
// -l - count lines
function countLines(s){
s = s.split('\n') // array containing per line data
return s.length
}
// -w - count words
function countWords(s){
let words = 0;
s = s.trim()
s = s.split("\r") // array containing per line data
for(let i=0;i<s.length;i++){
let wordsPerLine = [];
wordsPerLine = s[i].split(' ') // array containing each word
for(let j=0;j<wordsPerLine.length;j++){
if(wordsPerLine[j] != '' && wordsPerLine[j] !='\n'){ // if a word found
words++;
}
}
}
return words;
}
// -m - count chars
function countChars(s){
let chars = 0;
s = s.trim()
s = s.split("\r") // array containing per line data
for(let i=0;i<s.length;i++){
let wordsPerLine = [];
wordsPerLine = s[i].split(' ') // array containing each word
for(let j=0;j<wordsPerLine.length;j++){
if(wordsPerLine[j] != '' && wordsPerLine[j] !='\n'){
chars += wordsPerLine[j].length // storing each word length
if(wordsPerLine[j].length>1 && wordsPerLine[j].startsWith('\n')){ // if word starts with \n (edge case)
chars--;
}
}
}
}
return chars;
}
// -L - longest line
function longestLine(s){
let words = 0;
s = s.trim()
s = s.split("\r") // array containing per line data
let maxChars = 0;
for(let i=0;i<s.length;i++){
let chars = s[i].startsWith('\n')==true ? s[i].length-1 : s[i].length // length of line
maxChars = Math.max(maxChars,chars);
}
return maxChars;
}