Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env node
- 'use-strict'
- const process = require('process');
- const fs = require('fs');
- try {
- let string = 0;
- let word = 0;
- let symbol = 0;
- let now = 0;
- if (process.argv.length === 2) {
- const stdin = process.openStdin();
- const func = (x) => {
- now = 0;
- const str = x.toString('utf-8');
- string++;
- symbol += str.length;
- if (str.length > 1) {
- for (let i = 0; i < str.length; i++) {
- if (i !== 0) {
- if (str[i] === '\n') {
- if (str[i - 1] !== ' ' && str[i - 1] !== '\n') now++;
- }
- else {
- if (str[i] === ' ') {
- if (str[i - 1] !== ' ' && str[i - 1] !== '\n') now++;
- }
- }
- }
- }
- }
- word += now;
- };
- const f = () => {
- console.log(string + ' ' + word + ' ' + symbol);
- };
- stdin.addListener('data', func);
- process.stdin.on('end', f);
- }
- if (process.argv.length === 3) {
- const file = fs.readFileSync(process.argv[2], 'utf8');
- let pos = 0;
- symbol = file.length;
- while (pos < file.length) {
- if (pos !== 0) {
- if (file[pos] === '\n') {
- string++;
- if (file[pos - 1] !== ' ' && file[pos - 1] !== '\n') word++;
- }
- else {
- if (file[pos] === ' ') {
- if (file[pos - 1] !== ' ' && file[pos - 1] !== '\n') word++;
- }
- }
- }
- pos++;
- }
- console.log(string + ' ' + word + ' ' + symbol + ' ' + process.argv[2]);
- }
- if (process.argv.length === 4) {
- if ('-c' === process.argv[2]) {
- console.log(fs.statSync(process.argv[3]).size + ' ' + process.argv[3]);
- }
- if ('-m' === process.argv[2]) {
- console.log(fs.readFileSync(process.argv[3], 'utf8').length + ' ' + process.argv[3]);
- }
- if ('-l' === process.argv[2]) {
- const file = fs.readFileSync(process.argv[3], 'utf8');
- let pos = 0;
- while (pos < file.length) {
- if (file[pos] === '\n') string++;
- pos++;
- }
- console.log(string + ' ' + process.argv[3]);
- }
- if ('-w' === process.argv[2]) {
- const file = fs.readFileSync(process.argv[3], 'utf8');
- let pos = 0;
- while (pos < file.length) {
- if (pos !== 0) {
- if (file[pos] === '\n') {
- string++;
- if (file[pos - 1] !== ' ' && file[pos - 1] !== '\n') word++;
- }
- else {
- if (file[pos] === ' ') {
- if (file[pos - 1] !== ' ' && file[pos - 1] !== '\n') word++;
- }
- }
- }
- pos++;
- }
- console.log(word + ' ' + process.argv[3]);
- }
- }
- } catch (e) {
- console.error(e.message);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement