|
| 1 | +const fs = require('fs'); |
| 2 | + |
| 3 | +async function readStream(stream: NodeJS.ReadStream): Promise<Buffer> { |
| 4 | + const chunks = []; |
| 5 | + for await (const chunk of stream) { |
| 6 | + chunks.push(Buffer.from(chunk)); |
| 7 | + } |
| 8 | + return Buffer.concat(chunks); |
| 9 | +} |
| 10 | + |
| 11 | +function byteCount(filename: string): number { |
| 12 | + return fs.statSync(filename).size; |
| 13 | +} |
| 14 | + |
| 15 | +function lineCount(text: string): number { |
| 16 | + return text.split(/\r\n|\r|\n/).length - 1; |
| 17 | +} |
| 18 | + |
| 19 | +function wordCount(text: string): number { |
| 20 | + if (text.length <= 0) { |
| 21 | + return 0; |
| 22 | + } |
| 23 | + return text.trim().split(/\s+/).length; |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * This function is the unix wc command implementation |
| 28 | + * |
| 29 | + * @async |
| 30 | + * @param {string[]} argv - The first two are reserved arguments consering the |
| 31 | + * node call to the file |
| 32 | + * @param {?NodeJS.ReadStream} [stream] - This can be a file read stream or the |
| 33 | + * stdin stream |
| 34 | + * @returns {Promise<string>} |
| 35 | + */ |
| 36 | +async function myWC( |
| 37 | + argv: string[], |
| 38 | + stream?: NodeJS.ReadStream |
| 39 | +): Promise<string> { |
| 40 | + // Option is given, file is given |
| 41 | + if (argv.length === 4) { |
| 42 | + // console.log('file + option'); |
| 43 | + const option = argv[2]; |
| 44 | + const filename = argv[3]; |
| 45 | + if (fs.existsSync(filename)) { |
| 46 | + const fileContents = fs.readFileSync(filename, 'utf8').toString(); |
| 47 | + switch (option) { |
| 48 | + case '-c': |
| 49 | + return byteCount(filename).toString() + ' ' + filename; |
| 50 | + case '-l': |
| 51 | + return lineCount(fileContents).toString() + ' ' + filename; |
| 52 | + case '-w': |
| 53 | + return wordCount(fileContents).toString() + ' ' + filename; |
| 54 | + default: |
| 55 | + throw new Error('Invalid option'); |
| 56 | + } |
| 57 | + } |
| 58 | + if (typeof stream === 'undefined') { |
| 59 | + throw new Error('Invalid file'); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Only filename is given |
| 64 | + if (argv.length === 3) { |
| 65 | + // console.log('only option'); |
| 66 | + const filename = argv[2]; |
| 67 | + if (fs.existsSync(filename)) { |
| 68 | + const fileContents = fs.readFileSync(filename, 'utf8'); |
| 69 | + const line = lineCount(fileContents).toString(); |
| 70 | + const word = wordCount(fileContents).toString(); |
| 71 | + const bytes = byteCount(filename).toString(); |
| 72 | + return line + ' ' + word + ' ' + bytes + ' ' + filename; |
| 73 | + } |
| 74 | + if (typeof stream === 'undefined') { |
| 75 | + throw new Error('Invalid file'); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // Checking for stream |
| 80 | + if (typeof stream !== 'undefined') { |
| 81 | + // console.log('stream'); |
| 82 | + try { |
| 83 | + // If option is given |
| 84 | + const buffer = await readStream(stream); |
| 85 | + const fileContents = buffer.toString(); |
| 86 | + if (argv.length === 3) { |
| 87 | + const option = argv[2]; |
| 88 | + switch (option) { |
| 89 | + case '-c': |
| 90 | + return buffer.length.toString(); |
| 91 | + case '-l': |
| 92 | + return lineCount(fileContents).toString(); |
| 93 | + case '-w': |
| 94 | + return wordCount(fileContents).toString(); |
| 95 | + default: |
| 96 | + throw new Error('Invalid option'); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + // If no option is given |
| 101 | + if (argv.length == 2) { |
| 102 | + const line = lineCount(fileContents).toString(); |
| 103 | + const word = wordCount(fileContents).toString(); |
| 104 | + const bytes = buffer.length.toString(); |
| 105 | + return line + ' ' + word + ' ' + bytes; |
| 106 | + } |
| 107 | + } catch (err) { |
| 108 | + // If the error is not an expected TypeError |
| 109 | + if (!(err instanceof TypeError)) { |
| 110 | + throw err; |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + throw new Error('Invalid input or file'); |
| 115 | +} |
| 116 | + |
| 117 | +export { myWC }; |
0 commit comments