forked from MarufHasan24/mathlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
168 lines (166 loc) · 4.96 KB
/
index.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
Title : Mathlib
Author : Maruf Hasan
Description : mathlib is an laibrary of math where we can find our day to day life's almost all small and frequently used maths solvance whice are mostly make hard our programming practice.
Date : 24 September, 2021
Version : 1.0.0
*/
//dependencies
const { existsSync, mkdirSync, writeFileSync, readFileSync } = require("fs");
const { record, error } = require("./.localhandelar");
// all is perfect?
let recorDir = __dirname + "/.record";
if (!existsSync(recorDir)) {
mkdirSync(recorDir);
mkdirSync(recorDir + "/.trush");
mkdirSync(recorDir + "/.restored");
} else if (!existsSync(recorDir + "/.trush")) {
mkdirSync(recorDir + "/.trush");
} else if (!existsSync(recorDir + "/.restored")) {
mkdirSync(recorDir + "/.restored");
}
//module scaffolding
const math = {
...require(`${__dirname}/constants.json`),
...a(),
...require("./handelar/index.js"),
add: (...numbers) => {
if (numbers.length > 1) {
let result = 0;
for (let i = 0; i < numbers.length; i++) {
if (typeof numbers[i] === "number") {
result += numbers[i];
} else {
error("a number", i + " no.", "add");
}
}
return record(result, numbers, "add");
} else {
throw `Please enter at least two numbers to sum.`;
}
},
mul: (...numbers) => {
if (numbers.length > 1) {
let result = 1;
for (let i = 0; i < numbers.length; i++) {
if (typeof numbers[i] === "number") {
result *= numbers[i];
} else {
error("a number", i + " no.", "mul");
}
}
return record(result, numbers, "mul");
} else {
throw `Please enter at least two numbers to multiply.`;
}
},
sub: (...numbers) => {
if (numbers.length > 1) {
let result = numbers[0];
for (let i = 1; i < numbers.length; i++) {
if (typeof numbers[i] === "number") {
result -= numbers[i];
} else {
error("a number", i + " no.", "sub");
}
}
return record(result, numbers, "sub");
} else {
throw `Please enter at least two numbers to subtract.`;
}
},
div: (...numbers) => {
if (numbers.length > 1) {
let result = numbers[0];
for (let i = 1; i < numbers.length; i++) {
if (typeof numbers[i] === "number") {
result /= numbers[i];
} else {
error("a number", i + " no.", "div");
}
}
return record(result, numbers, "div");
} else {
throw `Please enter at least two numbers to divide.`;
}
},
sums: (start, end, func) => {
let Start =
typeof start === "number" && !Number.isNaN(start) ? start : null;
let End = typeof end === "number" && !Number.isNaN(end) ? end : null;
let Func = typeof func === "function" ? func : null;
if (Start !== null && End !== null && Func !== null) {
try {
r = func(start);
if (r === undefined)
error("a function", "func", "sum", SyntaxError, "return is absent");
else if (typeof r !== "number")
error(
"a function which return a number",
"func",
"sum",
SyntaxError,
"return is not a number"
);
} catch (err) {
error("a function", "func", "sum", SyntaxError);
}
if (Start <= End) {
let result = 0;
for (let i = start; i <= end; i++) {
result += func(i);
}
return record(result, [start, end, func], "sum");
} else {
error(
"start should be less than end",
"start and end",
"sum",
RangeError
);
}
} else {
if (Start === null) error("a number", "start", "sum");
if (End === null) error("a number", "end", "sum");
if (Func === null) error("a function", "func", "sum");
}
},
setmode: (mode, status) => {
let modepath = __dirname + "/handelar/mode/mode.json";
if (mode === "sci" || mode === "fix") {
//change mode
if (status > 12) status = 12;
else if (status < 0) status = 0;
if (mode === "sci") mode = "science";
else if (mode === "fix") mode = "fixed";
//write mode
writeFileSync(modepath, JSON.stringify({ mode, status }));
return { mode, status };
} else {
//write mode
writeFileSync(modepath, JSON.stringify({ mode: "normal", status: null }));
return { mode: "normal", status: null };
}
},
getmode: () => {
let modepath = __dirname + "/handelar/mode/mode.json";
let data = readFileSync(modepath, "utf8");
let obj = JSON.parse(data);
return obj;
},
};
//length
math.length = Object.keys(math).length + 1;
//redirects: old versions of functions
math.multiply = math.mul;
math.average = math.avg;
math.sum = math.add;
/*--add the math object--*/
function a() {
let e = Object.getOwnPropertyNames(Math),
t = {};
for (let n = 0; n < e.length; n++) t[e[n]] = Math[e[n]];
return t;
}
require("./handelar/trush")();
module.exports = math;