-
Notifications
You must be signed in to change notification settings - Fork 0
/
my wc.js
236 lines (197 loc) · 5.1 KB
/
my wc.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
solution wc (word count)
Abdullah Fatota
*/
const fs = require("fs");
const log = console.log;
/*
0 - node executable
1 - current file
2 - ... optional command line arguments
last - optional file name
*/
const args = process.argv;
const argsLength = args.length;
let userOptions;
let fileName;
let text;
let buffer;
const formatter = new Intl.NumberFormat();
const SPACE = " ";
const OPTIONS = {
byte: "-c",
line: "-l",
word: "-w",
"multi-byte": "-m",
debug: "-debug",
help: "-help",
};
const OK_EXIT = 0;
const ERROR_EXIT = 1;
function exit(status) {
process.exit(status);
}
function formatNumber(val) {
return formatter.format(val);
}
function enclose(str) {
return '"' + str + '"';
}
function printHelp() {
const [program, script] = args;
log(`*** help ***
Usage:
${program.includes(SPACE) ? enclose(program) : program} ${
script.includes(SPACE) ? enclose(script) : script
} NON_MANDATORY_OPTIONS FILE
NON_MANDATORY_OPTIONS, one or more of following:
${OPTIONS["byte"]} to output number of bytes in FILE
${OPTIONS["line"]} to output number of lines in FILE
${OPTIONS["word"]} to output number of words in FILE
${OPTIONS["multi-byte"]} to outputs number of \\u0000-\\uFFFF
unicode characters in FILE
${OPTIONS["debug"]} to outuput developmmnt debug information
${OPTIONS["help"]} to print this help information
FILE, can be either
a local file name or,
a full file path
== end of help ==
`);
}
function printDebug() {
log(`*** debug information ***`);
log({
args,
userOptions,
fileName,
"buffer instanceof Buffer": buffer instanceof Buffer,
});
log(`
== end of debug information ==
`);
}
/* parse cli arguments */
/* system error */
if (argsLength < 2) {
log(`System Error related to handling command line arguments,
length of arguments should be at least 2,
however currnet arguments length is {argsLength},
process.argv = {args}`);
exit(ERROR_EXIT);
}
log();
log("My wc (word count among other things) utility");
log("by Abdullah Fatota - 2023");
log();
/* no file name */
if (argsLength === 2) {
printHelp();
exit(ERROR_EXIT);
}
/* open file as binary sequence */
function initBuffer() {
if (!(buffer === undefined)) {
return;
}
buffer = fs.readFileSync(fileName, null);
}
/* convert buffer to text */
function initText() {
if (!(text === undefined)) {
return;
}
initBuffer();
text = buffer.toString("utf8");
}
/* get byte count */
function getByteCount() {
initBuffer();
return buffer.length;
}
/* get line count */
function getLineCount() {
initText();
const matches = text.match(/\n/g);
/* matches.length + 1 ? */
return matches === null ? 0 : matches.length;
}
/* get word count */
function getWordCount() {
initText();
/* any sequence of non-whitespace characters */
const matches = text.match(/\S+/g);
return matches === null ? 0 : matches.length;
}
/* get multi-byte characters */
function getMultibyteCharacterCount() {
initText();
/* (not all) multibyte characters */
const matches = text.match(/[\u0000-\uffff]/g);
return matches === null ? 0 : matches.length;
}
/* file name available; but empty userOptions */
if (argsLength === 3) {
/* last argument is the file name */
fileName = args.at(-1);
userOptions = [
OPTIONS.help,
OPTIONS.debug,
OPTIONS.byte,
OPTIONS.line,
OPTIONS.word,
OPTIONS["multi-byte"],
];
} else if (argsLength >= 4) {
/* 3rd to (last - 1) elements are OPTIONS
last is file name */
fileName = args.at(-1);
userOptions = args.slice(2, -1);
/* convert all user userOptions to lower case */
userOptions = userOptions.map((str) => str.toLowerCase());
/* ignore whitespace sequences */
userOptions = userOptions.filter((str) => /\s+/.test(str) === false);
}
/* start printing stuff per userOptions */
/* print help */
if (userOptions.includes("-help")) {
printHelp();
}
/* print debug info */
if (userOptions.includes("-debug")) {
userOptions = userOptions.concat([
OPTIONS.byte,
OPTIONS.line,
OPTIONS.word,
OPTIONS["multi-byte"],
]);
printDebug();
}
/* print file name */
if (
userOptions.includes(OPTIONS.byte) ||
userOptions.includes(OPTIONS.line) ||
userOptions.includes(OPTIONS.word) ||
userOptions.includes(OPTIONS["multi-byte"])
) {
log(
"File target:",
fileName.includes(SPACE) ? enclose(fileName) : fileName
);
log();
}
if (userOptions.includes(OPTIONS.byte)) {
log(`(${OPTIONS.byte}) byte count:`, formatNumber(getByteCount()));
}
if (userOptions.includes(OPTIONS.line)) {
log(`(${OPTIONS.line}) line count:`, formatNumber(getLineCount()));
}
if (userOptions.includes(OPTIONS.word)) {
log(`(${OPTIONS.word}) word count:`, formatNumber(getWordCount()));
}
if (userOptions.includes(OPTIONS["multi-byte"])) {
log(
`(${OPTIONS["multi-byte"]}) multi-byte characters:`,
formatNumber(getMultibyteCharacterCount())
);
}
exit(OK_EXIT);