trung-cli-calc 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/app.js ADDED
@@ -0,0 +1,133 @@
1
+ #!/usr/bin/env node
2
+
3
+ const args = process.argv.slice(2);
4
+
5
+ function showHelp() {
6
+ console.log(`
7
+ Simple CLI Calculator
8
+
9
+ Usage:
10
+ calc <a> <op> <b>
11
+ calc <command> <a> <b>
12
+ calc sqrt <a>
13
+
14
+ Operators:
15
+ + add
16
+ - subtract
17
+ * multiply
18
+ / divide
19
+ ** power
20
+ % modulo
21
+
22
+ Commands:
23
+ add <a> <b>
24
+ sub <a> <b>
25
+ mul <a> <b>
26
+ div <a> <b>
27
+ pow <a> <b>
28
+ mod <a> <b>
29
+ sqrt <a>
30
+
31
+ Examples:
32
+ calc 5 + 7
33
+ calc add 10 20
34
+ calc pow 2 5
35
+ calc 2 ** 5
36
+ calc mod 10 3
37
+ calc sqrt 9
38
+ `);
39
+ }
40
+
41
+ if (args.length === 0 || args[0] === "help" || args[0] === "--help") {
42
+ showHelp();
43
+ process.exit(0);
44
+ }
45
+
46
+ let a, b, op;
47
+
48
+ // Parse arguments
49
+ if (args.length === 3) {
50
+ // calc 5 + 7 OR calc add 5 7
51
+ if (["+", "-", "*", "/", "**", "%"].includes(args[1])) {
52
+ a = Number(args[0]);
53
+ op = args[1];
54
+ b = Number(args[2]);
55
+ } else {
56
+ op = args[0];
57
+ a = Number(args[1]);
58
+ b = Number(args[2]);
59
+ }
60
+ } else if (args.length === 2 && args[0] === "sqrt") {
61
+ // calc sqrt 9
62
+ op = "sqrt";
63
+ a = Number(args[1]);
64
+ b = null;
65
+ } else {
66
+ console.error("Invalid arguments");
67
+ showHelp();
68
+ process.exit(1);
69
+ }
70
+
71
+ // Validate numbers
72
+ if (op !== "sqrt" && (Number.isNaN(a) || Number.isNaN(b))) {
73
+ console.error("Arguments must be numbers");
74
+ process.exit(1);
75
+ }
76
+
77
+ if (op === "sqrt" && Number.isNaN(a)) {
78
+ console.error("Argument must be a number");
79
+ process.exit(1);
80
+ }
81
+
82
+ let result;
83
+
84
+ switch (op) {
85
+ case "+":
86
+ case "add":
87
+ result = a + b;
88
+ break;
89
+
90
+ case "-":
91
+ case "sub":
92
+ result = a - b;
93
+ break;
94
+
95
+ case "*":
96
+ case "mul":
97
+ result = a * b;
98
+ break;
99
+
100
+ case "/":
101
+ case "div":
102
+ if (b === 0) {
103
+ console.error("Error: division by zero");
104
+ process.exit(1);
105
+ }
106
+ result = a / b;
107
+ break;
108
+
109
+ case "**":
110
+ case "pow":
111
+ result = Math.pow(a, b);
112
+ break;
113
+
114
+ case "%":
115
+ case "mod":
116
+ result = a % b;
117
+ break;
118
+
119
+ case "sqrt":
120
+ if (a < 0) {
121
+ console.error("Error: sqrt of negative number");
122
+ process.exit(1);
123
+ }
124
+ result = Math.sqrt(a);
125
+ break;
126
+
127
+ default:
128
+ console.error("Unknown operator:", op);
129
+ process.exit(1);
130
+ }
131
+
132
+ const output = Number.isInteger(result) ? result : Number(result.toFixed(4));
133
+ console.log("Result:", output);
@@ -0,0 +1,133 @@
1
+ #!/usr/bin/env node
2
+
3
+ const args = process.argv.slice(2);
4
+
5
+ function showHelp() {
6
+ console.log(`
7
+ Simple CLI Calculator
8
+
9
+ Usage:
10
+ calc <a> <op> <b>
11
+ calc <command> <a> <b>
12
+ calc sqrt <a>
13
+
14
+ Operators:
15
+ + add
16
+ - subtract
17
+ * multiply
18
+ / divide
19
+ ** power
20
+ % modulo
21
+
22
+ Commands:
23
+ add <a> <b>
24
+ sub <a> <b>
25
+ mul <a> <b>
26
+ div <a> <b>
27
+ pow <a> <b>
28
+ mod <a> <b>
29
+ sqrt <a>
30
+
31
+ Examples:
32
+ calc 5 + 7
33
+ calc add 10 20
34
+ calc pow 2 5
35
+ calc 2 ** 5
36
+ calc mod 10 3
37
+ calc sqrt 9
38
+ `);
39
+ }
40
+
41
+ if (args.length === 0 || args[0] === "help" || args[0] === "--help") {
42
+ showHelp();
43
+ process.exit(0);
44
+ }
45
+
46
+ let a, b, op;
47
+
48
+ // Parse arguments
49
+ if (args.length === 3) {
50
+ // calc 5 + 7 OR calc add 5 7
51
+ if (["+", "-", "*", "/", "**", "%"].includes(args[1])) {
52
+ a = Number(args[0]);
53
+ op = args[1];
54
+ b = Number(args[2]);
55
+ } else {
56
+ op = args[0];
57
+ a = Number(args[1]);
58
+ b = Number(args[2]);
59
+ }
60
+ } else if (args.length === 2 && args[0] === "sqrt") {
61
+ // calc sqrt 9
62
+ op = "sqrt";
63
+ a = Number(args[1]);
64
+ b = null;
65
+ } else {
66
+ console.error("Invalid arguments");
67
+ showHelp();
68
+ process.exit(1);
69
+ }
70
+
71
+ // Validate numbers
72
+ if (op !== "sqrt" && (Number.isNaN(a) || Number.isNaN(b))) {
73
+ console.error("Arguments must be numbers");
74
+ process.exit(1);
75
+ }
76
+
77
+ if (op === "sqrt" && Number.isNaN(a)) {
78
+ console.error("Argument must be a number");
79
+ process.exit(1);
80
+ }
81
+
82
+ let result;
83
+
84
+ switch (op) {
85
+ case "+":
86
+ case "add":
87
+ result = a + b;
88
+ break;
89
+
90
+ case "-":
91
+ case "sub":
92
+ result = a - b;
93
+ break;
94
+
95
+ case "*":
96
+ case "mul":
97
+ result = a * b;
98
+ break;
99
+
100
+ case "/":
101
+ case "div":
102
+ if (b === 0) {
103
+ console.error("Error: division by zero");
104
+ process.exit(1);
105
+ }
106
+ result = a / b;
107
+ break;
108
+
109
+ case "**":
110
+ case "pow":
111
+ result = Math.pow(a, b);
112
+ break;
113
+
114
+ case "%":
115
+ case "mod":
116
+ result = a % b;
117
+ break;
118
+
119
+ case "sqrt":
120
+ if (a < 0) {
121
+ console.error("Error: sqrt of negative number");
122
+ process.exit(1);
123
+ }
124
+ result = Math.sqrt(a);
125
+ break;
126
+
127
+ default:
128
+ console.error("Unknown operator:", op);
129
+ process.exit(1);
130
+ }
131
+
132
+ const output = Number.isInteger(result) ? result : Number(result.toFixed(4));
133
+ console.log("Result:", output);
@@ -0,0 +1,133 @@
1
+ #!/usr/bin/env node
2
+
3
+ const args = process.argv.slice(2);
4
+
5
+ function showHelp() {
6
+ console.log(`
7
+ Simple CLI Calculator
8
+
9
+ Usage:
10
+ calc <a> <op> <b>
11
+ calc <command> <a> <b>
12
+ calc sqrt <a>
13
+
14
+ Operators:
15
+ + add
16
+ - subtract
17
+ * multiply
18
+ / divide
19
+ ** power
20
+ % modulo
21
+
22
+ Commands:
23
+ add <a> <b>
24
+ sub <a> <b>
25
+ mul <a> <b>
26
+ div <a> <b>
27
+ pow <a> <b>
28
+ mod <a> <b>
29
+ sqrt <a>
30
+
31
+ Examples:
32
+ calc 5 + 7
33
+ calc add 10 20
34
+ calc pow 2 5
35
+ calc 2 ** 5
36
+ calc mod 10 3
37
+ calc sqrt 9
38
+ `);
39
+ }
40
+
41
+ if (args.length === 0 || args[0] === "help" || args[0] === "--help") {
42
+ showHelp();
43
+ process.exit(0);
44
+ }
45
+
46
+ let a, b, op;
47
+
48
+ // Parse arguments
49
+ if (args.length === 3) {
50
+ // calc 5 + 7 OR calc add 5 7
51
+ if (["+", "-", "*", "/", "**", "%"].includes(args[1])) {
52
+ a = Number(args[0]);
53
+ op = args[1];
54
+ b = Number(args[2]);
55
+ } else {
56
+ op = args[0];
57
+ a = Number(args[1]);
58
+ b = Number(args[2]);
59
+ }
60
+ } else if (args.length === 2 && args[0] === "sqrt") {
61
+ // calc sqrt 9
62
+ op = "sqrt";
63
+ a = Number(args[1]);
64
+ b = null;
65
+ } else {
66
+ console.error("Invalid arguments");
67
+ showHelp();
68
+ process.exit(1);
69
+ }
70
+
71
+ // Validate numbers
72
+ if (op !== "sqrt" && (Number.isNaN(a) || Number.isNaN(b))) {
73
+ console.error("Arguments must be numbers");
74
+ process.exit(1);
75
+ }
76
+
77
+ if (op === "sqrt" && Number.isNaN(a)) {
78
+ console.error("Argument must be a number");
79
+ process.exit(1);
80
+ }
81
+
82
+ let result;
83
+
84
+ switch (op) {
85
+ case "+":
86
+ case "add":
87
+ result = a + b;
88
+ break;
89
+
90
+ case "-":
91
+ case "sub":
92
+ result = a - b;
93
+ break;
94
+
95
+ case "*":
96
+ case "mul":
97
+ result = a * b;
98
+ break;
99
+
100
+ case "/":
101
+ case "div":
102
+ if (b === 0) {
103
+ console.error("Error: division by zero");
104
+ process.exit(1);
105
+ }
106
+ result = a / b;
107
+ break;
108
+
109
+ case "**":
110
+ case "pow":
111
+ result = Math.pow(a, b);
112
+ break;
113
+
114
+ case "%":
115
+ case "mod":
116
+ result = a % b;
117
+ break;
118
+
119
+ case "sqrt":
120
+ if (a < 0) {
121
+ console.error("Error: sqrt of negative number");
122
+ process.exit(1);
123
+ }
124
+ result = Math.sqrt(a);
125
+ break;
126
+
127
+ default:
128
+ console.error("Unknown operator:", op);
129
+ process.exit(1);
130
+ }
131
+
132
+ const output = Number.isInteger(result) ? result : Number(result.toFixed(4));
133
+ console.log("Result:", output);
package/hello.js ADDED
File without changes
package/math.js ADDED
@@ -0,0 +1,26 @@
1
+ function add(a, b) {
2
+ return a + b;
3
+ }
4
+
5
+ function sub(a, b) {
6
+ return a - b;
7
+ }
8
+
9
+ function mul(a, b) {
10
+ return a * b;
11
+ }
12
+
13
+ function div(a, b) {
14
+ if (b === 0) throw new Error("Không thể chia cho 0");
15
+ return a / b;
16
+ }
17
+
18
+ function pow(a, b) {
19
+ return Math.pow(a, b);
20
+ }
21
+
22
+ function mod(a, b) {
23
+ return a % b;
24
+ }
25
+
26
+ module.exports = { add, sub, mul, div, pow, mod };
package/package.jon ADDED
@@ -0,0 +1,67 @@
1
+ {
2
+ "name": "calc-cli",
3
+ "version": "1.0.0",
4
+ "description": "Simple calculator CLI",
5
+ "main": "app.js",
6
+ "bin": {
7
+ "calc": "./app.js"
8
+ },
9
+ "type": "commonjs"
10
+ }
11
+ {
12
+ "name": "trung-cli-calc",
13
+ "version": "1.0.0",
14
+ "description": "Simple CLI calculator by Trung",
15
+ "bin": {
16
+ "calc": "./app.js"
17
+ },
18
+ "main": "app.js",
19
+ "scripts": {
20
+ "start": "node app.js"
21
+ },
22
+ "keywords": [
23
+ "cli",
24
+ "calculator",
25
+ "node"
26
+ ],
27
+ "author": "Trung",
28
+ "license": "MIT"
29
+ }
30
+ {
31
+ "name": "trung-cli-calc",
32
+ "version": "1.0.0",
33
+ "description": "Simple CLI calculator by Trung",
34
+ "bin": {
35
+ "calc": "./app.js"
36
+ },
37
+ "main": "app.js",
38
+ "scripts": {
39
+ "start": "node app.js"
40
+ },
41
+ "keywords": [
42
+ "cli",
43
+ "calculator",
44
+ "node"
45
+ ],
46
+ "author": "Trung",
47
+ "license": "MIT"
48
+ }
49
+ {
50
+ "name": "trung-cli-calc",
51
+ "version": "1.0.0",
52
+ "description": "Simple CLI calculator by Trung",
53
+ "bin": {
54
+ "calc": "./app.js"
55
+ },
56
+ "main": "app.js",
57
+ "scripts": {
58
+ "start": "node app.js"
59
+ },
60
+ "keywords": [
61
+ "cli",
62
+ "calculator",
63
+ "node"
64
+ ],
65
+ "author": "Trung",
66
+ "license": "MIT"
67
+ }
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "trung-cli-calc",
3
+ "version": "1.0.1",
4
+ "description": "Simple CLI calculator by Trung",
5
+ "bin": {
6
+ "calc": "./app.js"
7
+ },
8
+ "main": "app.js",
9
+ "scripts": {
10
+ "start": "node app.js"
11
+ },
12
+ "keywords": [
13
+ "cli",
14
+ "calculator",
15
+ "node"
16
+ ],
17
+ "author": "Trung",
18
+ "license": "MIT",
19
+ "type": "commonjs"
20
+ }
@@ -0,0 +1,67 @@
1
+ {
2
+ "name": "calc-cli",
3
+ "version": "1.0.0",
4
+ "description": "Simple calculator CLI",
5
+ "main": "app.js",
6
+ "bin": {
7
+ "calc": "./app.js"
8
+ },
9
+ "type": "commonjs"
10
+ }
11
+ {
12
+ "name": "trung-cli-calc",
13
+ "version": "1.0.0",
14
+ "description": "Simple CLI calculator by Trung",
15
+ "bin": {
16
+ "calc": "./app.js"
17
+ },
18
+ "main": "app.js",
19
+ "scripts": {
20
+ "start": "node app.js"
21
+ },
22
+ "keywords": [
23
+ "cli",
24
+ "calculator",
25
+ "node"
26
+ ],
27
+ "author": "Trung",
28
+ "license": "MIT"
29
+ }
30
+ {
31
+ "name": "trung-cli-calc",
32
+ "version": "1.0.0",
33
+ "description": "Simple CLI calculator by Trung",
34
+ "bin": {
35
+ "calc": "./app.js"
36
+ },
37
+ "main": "app.js",
38
+ "scripts": {
39
+ "start": "node app.js"
40
+ },
41
+ "keywords": [
42
+ "cli",
43
+ "calculator",
44
+ "node"
45
+ ],
46
+ "author": "Trung",
47
+ "license": "MIT"
48
+ }
49
+ {
50
+ "name": "trung-cli-calc",
51
+ "version": "1.0.0",
52
+ "description": "Simple CLI calculator by Trung",
53
+ "bin": {
54
+ "calc": "./app.js"
55
+ },
56
+ "main": "app.js",
57
+ "scripts": {
58
+ "start": "node app.js"
59
+ },
60
+ "keywords": [
61
+ "cli",
62
+ "calculator",
63
+ "node"
64
+ ],
65
+ "author": "Trung",
66
+ "license": "MIT"
67
+ }
Binary file