Advertisement
Void-voiD

Untitled

Dec 21st, 2018
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. #! /usr/bin/env node
  2. 'use-strict'
  3.  
  4. const process = require('process');
  5. const fs = require('fs');
  6.  
  7. try {
  8. let string = 0;
  9. let word = 0;
  10. let symbol = 0;
  11. let now = 0;
  12. if (process.argv.length === 2) {
  13. const stdin = process.openStdin();
  14. const func = (x) => {
  15. now = 0;
  16. const str = x.toString('utf-8');
  17. string++;
  18. symbol += str.length;
  19. if (str.length > 1) {
  20. for (let i = 0; i < str.length; i++) {
  21. if (i !== 0) {
  22. if (str[i] === '\n') {
  23. if (str[i - 1] !== ' ' && str[i - 1] !== '\n') now++;
  24. }
  25. else {
  26. if (str[i] === ' ') {
  27. if (str[i - 1] !== ' ' && str[i - 1] !== '\n') now++;
  28. }
  29. }
  30. }
  31. }
  32. }
  33. word += now;
  34. };
  35. const f = () => {
  36. console.log(string + ' ' + word + ' ' + symbol);
  37. };
  38. stdin.addListener('data', func);
  39. process.stdin.on('end', f);
  40. }
  41. if (process.argv.length === 3) {
  42. const file = fs.readFileSync(process.argv[2], 'utf8');
  43. let pos = 0;
  44. symbol = file.length;
  45. while (pos < file.length) {
  46. if (pos !== 0) {
  47. if (file[pos] === '\n') {
  48. string++;
  49. if (file[pos - 1] !== ' ' && file[pos - 1] !== '\n') word++;
  50. }
  51. else {
  52. if (file[pos] === ' ') {
  53. if (file[pos - 1] !== ' ' && file[pos - 1] !== '\n') word++;
  54. }
  55. }
  56. }
  57. pos++;
  58. }
  59. console.log(string + ' ' + word + ' ' + symbol + ' ' + process.argv[2]);
  60. }
  61. if (process.argv.length === 4) {
  62. if ('-c' === process.argv[2]) {
  63. console.log(fs.statSync(process.argv[3]).size + ' ' + process.argv[3]);
  64. }
  65. if ('-m' === process.argv[2]) {
  66. console.log(fs.readFileSync(process.argv[3], 'utf8').length + ' ' + process.argv[3]);
  67. }
  68. if ('-l' === process.argv[2]) {
  69. const file = fs.readFileSync(process.argv[3], 'utf8');
  70. let pos = 0;
  71. while (pos < file.length) {
  72. if (file[pos] === '\n') string++;
  73. pos++;
  74. }
  75. console.log(string + ' ' + process.argv[3]);
  76. }
  77. if ('-w' === process.argv[2]) {
  78. const file = fs.readFileSync(process.argv[3], 'utf8');
  79. let pos = 0;
  80. while (pos < file.length) {
  81. if (pos !== 0) {
  82. if (file[pos] === '\n') {
  83. string++;
  84. if (file[pos - 1] !== ' ' && file[pos - 1] !== '\n') word++;
  85. }
  86. else {
  87. if (file[pos] === ' ') {
  88. if (file[pos - 1] !== ' ' && file[pos - 1] !== '\n') word++;
  89. }
  90. }
  91. }
  92. pos++;
  93. }
  94. console.log(word + ' ' + process.argv[3]);
  95. }
  96. }
  97. } catch (e) {
  98. console.error(e.message);
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement