-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
131 lines (115 loc) · 3.09 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import chalk from "chalk";
import boxen from "boxen";
import inquirer from "inquirer";
// Colores
const error = chalk.bold.redBright;
const success = chalk.greenBright;
const info = chalk.yellowBright;
const title = chalk.bold.cyanBright;
const box = (text, title) =>
console.log(
boxen(text, {
padding: 1,
title: title,
titleAlignment: "center",
textAlignment: "center",
})
);
// Valid operations
const operations = {
addition: "+",
subtraction: "-",
multiplication: "*",
division: "/",
};
export const arithmeticCalculation = (operation, numbers) => {
if (numbers.length < 2) {
box(error("Please enter at least two numbers"), "ERROR");
return;
}
// convert strings to numbers, as commander parses all arguments as strings
const numbersToCalculate = numbers.map((number) => Number(number));
// validate the numbers
const validNumbers = numbersToCalculate.every((number) => !isNaN(number));
// throw error if any of the numbers is not a number
if (!validNumbers) {
box(error("Please enter valid numbers"), "ERROR");
return;
}
// validate the operation type
if (!operations[operation]) {
box(error("Please enter a valid operation"), "ERROR");
return;
}
// calculate the result
const result = numbersToCalculate.reduce((acc, number) => {
switch (operation) {
case "addition":
return acc + number;
case "subtraction":
return acc - number;
case "multiplication":
return acc * number;
case "division":
return acc / number;
default:
return acc;
}
});
// display the result
box(
`${info(numbersToCalculate.join(` ${operations[operation]} `))} = ${success(
result
)}`,
title(operation)
);
return result;
};
export const calculator = async () => {
const ask = async (basenumber) => {
// if basenumber is undefined, ask for the first number
if (!basenumber) {
const { firstNumber } = await inquirer.prompt([
{
type: "input",
name: "firstNumber",
message: "Please enter the first number",
},
]);
basenumber = firstNumber;
}
// ask for the calculation type
const { operation } = await inquirer.prompt([
{
type: "list",
name: "operation",
message: `Please select the operation to perform on ${info(
basenumber
)}`,
choices: [
...Object.keys(operations).map((operation) => ({
name: `${operation} (${operations[operation]})`,
value: operation,
})),
{ name: "Exit", value: "exit" },
],
},
]);
// if the user wants to exit, exit
if (operation === "exit") {
return;
}
// ask for the second number
const { secondNumber } = await inquirer.prompt([
{
type: "input",
name: "secondNumber",
message: "Please enter the second number",
},
]);
// calculate the result
const result = arithmeticCalculation(operation, [basenumber, secondNumber]);
return ask(result);
};
ask();
};