Skip to content

Commit 6aa96b7

Browse files
authored
Updated Version 1.0.3
1 parent 25fe781 commit 6aa96b7

File tree

7 files changed

+4503
-0
lines changed

7 files changed

+4503
-0
lines changed

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
this is the Source code of Node js and Typescript Based Calculator Runs on the Command line Interface , its package Also published at NPM Marketplace. to run calculator do :
2+
3+
1) npm i project-04-cli-based-calculator-node-and-typescript
4+
5+
2) npx ahmedcacli
6+
7+
3) initialized typescript enviroment through npx tsc -init
8+
9+
4) intialized node js enviroment npm init -yes
10+
11+
5) npm i chalk
12+
13+
6) npm i chalk-animation
14+
15+
7) npm i inquirer
16+
17+
8) npm i nanospinner
18+
19+
9) npm install typescript --save-dev
20+
21+
10) npm i -g typescript
22+
23+
11) npx tsc --init
24+
25+
12) npm i ts-node-dev
26+
27+
13) npm install nodemon --save-dev
28+
29+
14) npm install @types/node --save-dev
30+
31+
15)Add .js file with this at top : #! /usr/bin/env node
32+
33+
16)setup Json file(example) :
34+
35+
"bin": { "multiply": "bin/index.js" },
36+
37+
17)Test package locally(for example :
38+
39+
npx multiply
40+
41+
18)Add Dependencies and import it to your main file :
42+
43+
npm i prompt-sync
44+
45+
19)create or login your npm account
46+
47+
npm login
48+
49+
20)publish your package
50+
51+
21) npm publish
52+
53+
22) added calculator basic opetaions features - , + , * , /

lib/index.js

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#! /usr/bin/env node
2+
import chalk from "chalk";
3+
import inquirer from "inquirer";
4+
import chalkAnimation from "chalk-animation";
5+
import { createSpinner } from "nanospinner";
6+
let playerName;
7+
const sleep = (ms = 2000) => new Promise((r) => setTimeout(r, ms));
8+
//Welcome Screen
9+
const welcome = async () => {
10+
const rainbowTitle = chalkAnimation.rainbow(`This is the simple Calculator?
11+
Developed by Ahmed Ali Ansari \n`);
12+
await sleep();
13+
rainbowTitle.stop();
14+
};
15+
// User Name Input
16+
const askName = async () => {
17+
const answers = await inquirer.prompt({
18+
name: "user_name",
19+
type: "input",
20+
message: "What is your name? ",
21+
default() {
22+
return "User";
23+
},
24+
});
25+
playerName = answers.user_name;
26+
console.log(`Hey There ! ${playerName}`);
27+
};
28+
let calculator = {
29+
add(number1, number2) {
30+
return number1 + number2;
31+
},
32+
sub(number1, number2) {
33+
return number1 - number2;
34+
},
35+
division: (number1, number2) => number1 / number2,
36+
Multiplication: (number1, number2) => number1 * number2,
37+
};
38+
console.clear();
39+
await welcome();
40+
await askName();
41+
let want_to_Continue;
42+
do {
43+
//Input Option and numbers from user
44+
const question = async () => {
45+
const operators = await inquirer.prompt([
46+
{
47+
name: "available_operations",
48+
type: "list",
49+
message: "Please use Arrow key to Select your option!\n",
50+
choices: ["Addition", "Substraction", "Division", "Multiplication"],
51+
},
52+
{
53+
name: "num1",
54+
type: "number",
55+
message: "enter first number : ",
56+
default() {
57+
return console.error();
58+
},
59+
},
60+
{
61+
name: "num2",
62+
type: "number",
63+
message: "enter second number : ",
64+
default() {
65+
return console.error();
66+
},
67+
},
68+
]);
69+
// await question();
70+
return handleAnswer(operators.available_operations, operators.num1, operators.num2);
71+
};
72+
//making a choice function
73+
const handleAnswer = async (choose_operations, number1, number2) => {
74+
let answer;
75+
const spinner = createSpinner("Calculating...").start();
76+
await sleep();
77+
switch (choose_operations) {
78+
case "Addition":
79+
answer = calculator.add(number1, number2);
80+
spinner.success({
81+
text: `\nThe Result of Addition is : ${chalk.bold.bgBlueBright(answer)}`,
82+
});
83+
break;
84+
case "Substraction":
85+
answer = calculator.sub(number1, number2);
86+
spinner.success({
87+
text: `\nThe Result of Substraction is : ${chalk.bold.bgBlueBright(answer)}`,
88+
});
89+
break;
90+
case "Division":
91+
answer = calculator.division(number1, number2);
92+
spinner.success({
93+
text: `\nThe Result of Division is : ${chalk.bold.bgBlueBright(answer)}`,
94+
});
95+
break;
96+
case "Multiplication":
97+
answer = calculator.Multiplication(number1, number2);
98+
spinner.success({
99+
text: `\nThe Result of Multiplication is : ${chalk.bold.bgBlueBright(answer)}`,
100+
});
101+
break;
102+
default:
103+
console.log("Invalid Choice !");
104+
}
105+
};
106+
const want_to_calculate_more = async () => {
107+
const choice = await inquirer.prompt({
108+
name: "want_to_calculate_more",
109+
type: "confirm",
110+
message: "Do you want to continue for more operations ? ",
111+
});
112+
want_to_Continue = choice.want_to_calculate_more;
113+
};
114+
await question();
115+
await want_to_calculate_more();
116+
console.clear();
117+
} while (want_to_Continue == true);

0 commit comments

Comments
 (0)