@carlsebastian/jsu 1.1.0 → 1.1.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/global.js CHANGED
@@ -1,3 +1,4 @@
1
+ import "./src/utils/arithmetic/arithmetic.js";
1
2
  import "./src/utils/custom/custom.js";
2
3
  import "./src/utils/custom/error/error.js";
3
4
  import "./src/utils/dom/dom.js";
@@ -5,4 +6,3 @@ import "./src/utils/guards/guards.js";
5
6
  import "./src/utils/primitives/primitives.js";
6
7
  import "./src/utils/storage/storage.js";
7
8
  import "./src/utils/variables.js";
8
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@carlsebastian/jsu",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "A ready-to-use JavaScripts custom utilities such as types validations, formats validations, formatter, and many more!",
5
5
  "main": "./global.js",
6
6
  "types": "./src/types/global.d.ts",
@@ -13,6 +13,10 @@
13
13
  "require": "./global.js",
14
14
  "types": "./src/types/global.d.ts"
15
15
  },
16
+ "./arithmetics": {
17
+ "require": "./src/utils/arithmetic/arithmetic.js",
18
+ "types": "./src/types/arithmetic/arithmetic.d.ts"
19
+ },
16
20
  "./custom": {
17
21
  "require": "./src/utils/custom/custom.js",
18
22
  "types": "./src/types/custom/custom.d.ts"
@@ -49,7 +53,11 @@
49
53
  "url": "https://github.com/Sebastian-Carl/JSU/issues"
50
54
  },
51
55
  "homepage": "https://github.com/Sebastian-Carl/JSU#readme",
52
- "keywords": [],
56
+ "keywords": [
57
+ "utils",
58
+ "js-utils",
59
+ "jsu"
60
+ ],
53
61
  "author": "",
54
62
  "license": "ISC"
55
63
  }
@@ -0,0 +1,281 @@
1
+ interface ArithmeticAPI {
2
+ /**
3
+ * Accumulates the total divided value of the first and second numerical values.
4
+ *
5
+ * ***Note***:
6
+ * - Using a zero (`0`) to divide other given numerical values would always resulted to `NaN`.
7
+ *
8
+ * @param num1 - The first or primary base numerical value of this operation.
9
+ * @param num2 - The second numerical value to divide with primary value.
10
+ * @returns The total accumulated result of this operation.
11
+ */
12
+ Divide(num1: number, num2: number): number;
13
+
14
+ /**
15
+ * Accumulates the total divided value of the first, second, and third numerical values.
16
+ *
17
+ * ***Note***:
18
+ * - Using a zero (`0`) to divide other given numerical values would always resulted to `NaN`.
19
+ *
20
+ * @param num1 - The first or primary base numerical value of this operation.
21
+ * @param num2 - The second numerical value to divide with primary value.
22
+ * @param num3 - The third numerical value to divide with the given first 2 numerical values.
23
+ * @returns The total accumulated result of this operation.
24
+ */
25
+ Divide(num1: number, num2: number, num3: number): number;
26
+
27
+ /**
28
+ * Accumulates the total divided value of the first, second, third, and fourth numerical values.
29
+ *
30
+ * ***Note***:
31
+ * - Using a zero (`0`) to divide other given numerical values would always resulted to `NaN`.
32
+ *
33
+ * @param num1 - The first or primary base numerical value of this operation.
34
+ * @param num2 - The second numerical value to divide with primary value.
35
+ * @param num3 - The third numerical value to divide with the given first 2 numerical values.
36
+ * @param num4 - The fourth numerical value to divide with the given first 3 numerical values.
37
+ * @returns The total accumulated result of this operation.
38
+ */
39
+ Divide(num1: number, num2: number, num3: number, num4: number): number;
40
+
41
+ /**
42
+ * Accumulates the total divided value of the first, second, third, and fourth numerical values.
43
+ *
44
+ * ***Note***:
45
+ * - Using a zero (`0`) to divide other given numerical values would always resulted to `NaN`.
46
+ *
47
+ * @param num1 - The first or primary base numerical value of this operation.
48
+ * @param num2 - The second numerical value to divide with primary value.
49
+ * @param num3 - The third numerical value to divide with the given first 2 numerical values.
50
+ * @param num4 - The fourth numerical value to divide with the given first 3 numerical values.
51
+ * @returns The total accumulated result of this operation.
52
+ */
53
+ Divide(num1: number, num2: number, num3: number, num4: number): number;
54
+
55
+ /**
56
+ * Accumulates the total divided value of the first, second, third, fourth, and fifth numerical values.
57
+ *
58
+ * ***Note***:
59
+ * - Using a zero (`0`) to divide other given numerical values would always resulted to `NaN`.
60
+ *
61
+ * @param num1 - The first or primary base numerical value of this operation.
62
+ * @param num2 - The second numerical value to divide with primary value.
63
+ * @param num3 - The third numerical value to divide with the given first 2 numerical values.
64
+ * @param num4 - The fourth numerical value to divide with the given first 3 numerical values.
65
+ * @param num5 - The fifth numerical value to divide with the other given 4 numerical values.
66
+ * @returns The total accumulated result of this operation.
67
+ */
68
+ Divide(num1: number, num2: number, num3: number, num4: number, num5: number): number;
69
+
70
+ /**
71
+ * Accumulates the total divided value of the given collection of numerical values.
72
+ *
73
+ * ***Note***:
74
+ * - Using a zero (`0`) to divide other given numerical values would always resulted to `NaN`.
75
+ *
76
+ * @param nums - The collection of numerical values to divide.
77
+ * @returns The total accumulated result of this operation.
78
+ */
79
+ Divide(...nums: number[]): number;
80
+
81
+ /**
82
+ * Accumulates the total multiplied value of the first and second numerical values.
83
+ *
84
+ * ***Note***:
85
+ * - Using a zero (`0`) to multiply other given numerical values would always resulted to (`0`).
86
+ *
87
+ * @param num1 - The first or primary base numerical value of this operation.
88
+ * @param num2 - The second numerical value to multiply with primary value.
89
+ * @returns The total accumulated result of this operation.
90
+ */
91
+ Multiply(num1: number, num2: number): number;
92
+
93
+ /**
94
+ * Accumulates the total multiplied value of the first, second, and third numerical values.
95
+ *
96
+ * ***Note***:
97
+ * - Using a zero (`0`) to multiply other given numerical values would always resulted to (`0`).
98
+ *
99
+ * @param num1 - The first or primary base numerical value of this operation.
100
+ * @param num2 - The second numerical value to multiply with primary value.
101
+ * @param num3 - The third numerical value to multiply with the given first 2 numerical values.
102
+ * @returns The total accumulated result of this operation.
103
+ */
104
+ Multiply(num1: number, num2: number, num3: number): number;
105
+
106
+ /**
107
+ * Accumulates the total multiplied value of the first, second, third, and fourth numerical values.
108
+ *
109
+ * ***Note***:
110
+ * - Using a zero (`0`) to multiply other given numerical values would always resulted to (`0`).
111
+ *
112
+ * @param num1 - The first or primary base numerical value of this operation.
113
+ * @param num2 - The second numerical value to multiply with primary value.
114
+ * @param num3 - The third numerical value to multiply with the given first 2 numerical values.
115
+ * @param num4 - The fourth numerical value to multiply with the given first 3 numerical values.
116
+ * @returns The total accumulated result of this operation.
117
+ */
118
+ Multiply(num1: number, num2: number, num3: number, num4: number): number;
119
+
120
+ /**
121
+ * Accumulates the total multiplied value of the first, second, third, and fourth numerical values.
122
+ *
123
+ * ***Note***:
124
+ * - Using a zero (`0`) to multiply other given numerical values would always resulted to (`0`).
125
+ *
126
+ * @param num1 - The first or primary base numerical value of this operation.
127
+ * @param num2 - The second numerical value to multiply with primary value.
128
+ * @param num3 - The third numerical value to multiply with the given first 2 numerical values.
129
+ * @param num4 - The fourth numerical value to multiply with the given first 3 numerical values.
130
+ * @returns The total accumulated result of this operation.
131
+ */
132
+ Multiply(num1: number, num2: number, num3: number, num4: number): number;
133
+
134
+ /**
135
+ * Accumulates the total multiplied value of the first, second, third, fourth, and fifth numerical values.
136
+ *
137
+ * ***Note***:
138
+ * - Using a zero (`0`) to multiply other given numerical values would always resulted to (`0`).
139
+ *
140
+ * @param num1 - The first or primary base numerical value of this operation.
141
+ * @param num2 - The second numerical value to multiply with primary value.
142
+ * @param num3 - The third numerical value to multiply with the given first 2 numerical values.
143
+ * @param num4 - The fourth numerical value to multiply with the given first 3 numerical values.
144
+ * @param num5 - The fifth numerical value to multiply with the other given 4 numerical values.
145
+ * @returns The total accumulated result of this operation.
146
+ */
147
+ Multiply(num1: number, num2: number, num3: number, num4: number, num5: number): number;
148
+
149
+ /**
150
+ * Accumulates the total multiplied value of the given collection of numerical values.
151
+ *
152
+ * ***Note***:
153
+ * - Using a zero (`0`) to multiply other given numerical values would always resulted to (`0`).
154
+ *
155
+ * @param nums - The collection of numerical values to multiply.
156
+ * @returns The total accumulated result of this operation.
157
+ */
158
+ Multiply(...nums: number[]): number;
159
+
160
+ /**
161
+ * Accumulates the total subtracted value of the first and second numerical values.
162
+ *
163
+ * @param num1 - The first or primary base numerical value of this operation.
164
+ * @param num2 - The second numerical value to subtract with primary value.
165
+ * @returns The total accumulated result of this operation.
166
+ */
167
+ Subtract(num1: number, num2: number): number;
168
+
169
+ /**
170
+ * Accumulates the total subtracted value of the first, second, and third numerical values.
171
+ *
172
+ * @param num1 - The first or primary base numerical value of this operation.
173
+ * @param num2 - The second numerical value to subtract with primary value.
174
+ * @param num3 - The third numerical value to subtract with the given first 2 numerical values.
175
+ * @returns The total accumulated result of this operation.
176
+ */
177
+ Subtract(num1: number, num2: number, num3: number): number;
178
+
179
+ /**
180
+ * Accumulates the total subtracted value of the first, second, third, and fourth numerical values.
181
+ *
182
+ * @param num1 - The first or primary base numerical value of this operation.
183
+ * @param num2 - The second numerical value to subtract with primary value.
184
+ * @param num3 - The third numerical value to subtract with the given first 2 numerical values.
185
+ * @param num4 - The fourth numerical value to subtract with the given first 3 numerical values.
186
+ * @returns The total accumulated result of this operation.
187
+ */
188
+ Subtract(num1: number, num2: number, num3: number, num4: number): number;
189
+
190
+ /**
191
+ * Accumulates the total subtracted value of the first, second, third, and fourth numerical values.
192
+ *
193
+ * @param num1 - The first or primary base numerical value of this operation.
194
+ * @param num2 - The second numerical value to subtract with primary value.
195
+ * @param num3 - The third numerical value to subtract with the given first 2 numerical values.
196
+ * @param num4 - The fourth numerical value to subtract with the given first 3 numerical values.
197
+ * @returns The total accumulated result of this operation.
198
+ */
199
+ Subtract(num1: number, num2: number, num3: number, num4: number): number;
200
+
201
+ /**
202
+ * Accumulates the total subtracted value of the first, second, third, fourth, and fifth numerical values.
203
+ *
204
+ * @param num1 - The first or primary base numerical value of this operation.
205
+ * @param num2 - The second numerical value to subtract with primary value.
206
+ * @param num3 - The third numerical value to subtract with the given first 2 numerical values.
207
+ * @param num4 - The fourth numerical value to subtract with the given first 3 numerical values.
208
+ * @param num5 - The fifth numerical value to subtract with the other given 4 numerical values.
209
+ * @returns The total accumulated result of this operation.
210
+ */
211
+ Subtract(num1: number, num2: number, num3: number, num4: number, num5: number): number;
212
+
213
+ /**
214
+ * Accumulates the total subtracted value of the given collection of numerical values.
215
+ *
216
+ * @param nums - The collection of numerical values to subtract.
217
+ * @returns The total accumulated result of this operation.
218
+ */
219
+ Subtract(...nums: number[]): number;
220
+
221
+ /**
222
+ * Accumulates the total summed value of the first and second numerical values.
223
+ *
224
+ * @param num1 - The first or primary base numerical value of this operation.
225
+ * @param num2 - The second numerical value to sum with primary value.
226
+ * @returns The total accumulated result of this operation.
227
+ */
228
+ Sum(num1: number, num2: number): number;
229
+
230
+ /**
231
+ * Accumulates the total summed value of the first, second, and third numerical values.
232
+ *
233
+ * @param num1 - The first or primary base numerical value of this operation.
234
+ * @param num2 - The second numerical value to sum with primary value.
235
+ * @param num3 - The third numerical value to sum with the given first 2 numerical values.
236
+ * @returns The total accumulated result of this operation.
237
+ */
238
+ Sum(num1: number, num2: number, num3: number): number;
239
+
240
+ /**
241
+ * Accumulates the total summed value of the first, second, third, and fourth numerical values.
242
+ *
243
+ * @param num1 - The first or primary base numerical value of this operation.
244
+ * @param num2 - The second numerical value to sum with primary value.
245
+ * @param num3 - The third numerical value to sum with the given first 2 numerical values.
246
+ * @param num4 - The fourth numerical value to sum with the given first 3 numerical values.
247
+ * @returns The total accumulated result of this operation.
248
+ */
249
+ Sum(num1: number, num2: number, num3: number, num4: number): number;
250
+
251
+ /**
252
+ * Accumulates the total summed value of the first, second, third, and fourth numerical values.
253
+ *
254
+ * @param num1 - The first or primary base numerical value of this operation.
255
+ * @param num2 - The second numerical value to sum with primary value.
256
+ * @param num3 - The third numerical value to sum with the given first 2 numerical values.
257
+ * @param num4 - The fourth numerical value to sum with the given first 3 numerical values.
258
+ * @returns The total accumulated result of this operation.
259
+ */
260
+ Sum(num1: number, num2: number, num3: number, num4: number): number;
261
+
262
+ /**
263
+ * Accumulates the total summed value of the first, second, third, fourth, and fifth numerical values.
264
+ *
265
+ * @param num1 - The first or primary base numerical value of this operation.
266
+ * @param num2 - The second numerical value to sum with primary value.
267
+ * @param num3 - The third numerical value to sum with the given first 2 numerical values.
268
+ * @param num4 - The fourth numerical value to sum with the given first 3 numerical values.
269
+ * @param num5 - The fifth numerical value to sum with the other given 4 numerical values.
270
+ * @returns The total accumulated result of this operation.
271
+ */
272
+ Sum(num1: number, num2: number, num3: number, num4: number, num5: number): number;
273
+
274
+ /**
275
+ * Accumulates the total summed value of the given collection of numerical values.
276
+ *
277
+ * @param nums - The collection of numerical values to sum.
278
+ * @returns The total accumulated result of this operation.
279
+ */
280
+ Sum(...nums: number[]): number;
281
+ }
@@ -27,6 +27,31 @@ type CustomUtilitiesAPI = {
27
27
  */
28
28
  ConstructorOrTypeOf(arg: any): string | undefined;
29
29
 
30
+ /**
31
+ * Defines the given key-pair data to the target object.
32
+ *
33
+ * ***Options***:
34
+ * - `def` - Allows the defined key-pair data to be **writable**, **configurable**, and **enumerable**. (Default)
35
+ * - `hard` - Prevents the defined key-pair data to be **writable**, **configurable**, and **enumerable**.
36
+ * - `med` - Prevents the defined key-pair data to be **writable** and **configurable** but, can be **enumerable**.
37
+ * - `soft` - Prevents the defined key-pair data to be **writable** but, can be **configurable** and **enumerable**.
38
+ *
39
+ * @param obj - The target object to define the key-pair data at.
40
+ * @param key - The key of key-pair data.
41
+ * @param data - The data of key-pair data.
42
+ * @param opt - A options of what would be the state configuration of the defined key-pair data.
43
+ */
44
+ DefineProperty<K, D>(obj: { [prop: string]: any }, key: K, data: D, opt?: 'def' | 'hard' | 'med' | 'soft'): { [prop: string]: any } & { [prop in K]: D; };
45
+
46
+ /**
47
+ * Defines the given key-pair data to the `globalThis` or `window` API.
48
+ *
49
+ * @param key - The key of key-pair data.
50
+ * @param data - The data of key-pair data.
51
+ * @param opt - A options of what would be the state configuration of the defined key-pair data.
52
+ */
53
+ Global(key: string, data: any, opt?: 'def' | 'hard' | 'med' | 'soft'): void;
54
+
30
55
  /**
31
56
  * Retrieves the `name` property of the specified object.
32
57
  *
@@ -39,4 +64,62 @@ type CustomUtilitiesAPI = {
39
64
  * @returns The `name` property value of the object.
40
65
  */
41
66
  NameOf(obj: {}): string;
67
+
68
+ /**
69
+ * Validates and normalize the given numerical value to valid numerical value.
70
+ *
71
+ * @param num1 - The numerical value to normalize.
72
+ * @returns The normalized numerical value.
73
+ */
74
+ NormalizeNumbers(num1: number): number;
75
+
76
+ /**
77
+ * Validates and normalize the given collection of numerical values to a valid numerical values.
78
+ *
79
+ * @param num1 - The first numerical value to normalize.
80
+ * @param num2 - The second numerical value to normalize.
81
+ * @returns The normalized collection of numerical values.
82
+ */
83
+ NormalizeNumbers(num1: number, num2: number): number[];
84
+
85
+ /**
86
+ * Validates and normalize the given collection of numerical values to a valid numerical values.
87
+ *
88
+ * @param num1 - The first numerical value to normalize.
89
+ * @param num2 - The second numerical value to normalize.
90
+ * @param num3 - The third numerical value to normalize.
91
+ * @returns The normalized collection of numerical values.
92
+ */
93
+ NormalizeNumbers(num1: number, num2: number, num3: number): number[];
94
+
95
+ /**
96
+ * Validates and normalize the given collection of numerical values to a valid numerical values.
97
+ *
98
+ * @param num1 - The first numerical value to normalize.
99
+ * @param num2 - The second numerical value to normalize.
100
+ * @param num3 - The third numerical value to normalize.
101
+ * @param num4 - The fourth numerical value to normalize.
102
+ * @returns The normalized collection of numerical values.
103
+ */
104
+ NormalizeNumbers(num1: number, num2: number, num3: number, num4: number): number[];
105
+
106
+ /**
107
+ * Validates and normalize the given collection of numerical values to a valid numerical values.
108
+ *
109
+ * @param num1 - The first numerical value to normalize.
110
+ * @param num2 - The second numerical value to normalize.
111
+ * @param num3 - The third numerical value to normalize.
112
+ * @param num4 - The fourth numerical value to normalize.
113
+ * @param num5 - The fifth numerical value to normalize.
114
+ * @returns The normalized collection of numerical values.
115
+ */
116
+ NormalizeNumbers(num1: number, num2: number, num3: number, num4: number, num5: number): number[];
117
+
118
+ /**
119
+ * Validates and normalize the given collection of numerical values to a valid numerical values.
120
+ *
121
+ * @param nums - The collection of numerical values to normalize.
122
+ * @returns The normalized collection of numerical values.
123
+ */
124
+ NormalizeNumbers(...nums: number[]): number[];
42
125
  }
@@ -1,5 +1,6 @@
1
1
  /// <reference types="./api/api.d.ts" />
2
2
  /// <reference types="./api/element.d.ts" />
3
+ /// <reference types="./arithmetic/arithmetic.d.ts" />
3
4
  /// <reference types="./dom/dom.d.ts" />
4
5
  /// <reference types="./custom/custom.d.ts" />
5
6
  /// <reference types="./custom/error/error.d.ts" />
@@ -8,6 +9,7 @@
8
9
  /// <reference types="./storage/storage.d.ts" />
9
10
 
10
11
  declare global {
12
+ readonly var Arithmetic: ArithmeticAPI;
11
13
  readonly var Custom: CustomAPI;
12
14
  readonly var DOM: DomAPI;
13
15
  readonly var ERROR: ErrorAPI;
@@ -0,0 +1,20 @@
1
+ import { DefineProperty, Global } from '../custom/utils/custom.utils.js';
2
+ import { IsNullOrUndefined } from '../guards/data-types/data-types.js';
3
+ import { HasProperty, IsStrEmpty } from '../guards/formats/formats.js';
4
+ import Divide from './operations/operation.divide.js';
5
+ import Multiply from './operations/operation.multiply.js';
6
+ import Subtract from './operations/operation.subtract.js';
7
+ import Sum from './operations/operation.sum.js';
8
+
9
+ function Arithmetics() {
10
+ const ArithmeticAPI = {};
11
+
12
+ for (const Method of [Divide, Multiply, Subtract, Sum]) {
13
+ const Key = Method.name;
14
+ if (!IsNullOrUndefined(Key) && !IsStrEmpty(Key) && !HasProperty(ArithmeticAPI, Key))
15
+ DefineProperty(ArithmeticAPI, Key, Method, "med");
16
+ }
17
+
18
+ Global("Arithmetic", ArithmeticAPI, "soft");
19
+ }
20
+ Arithmetics();
@@ -1,6 +1,6 @@
1
1
  import Raise from '../../custom/error/builder/error.builder.js';
2
+ import { NormalizeNumbers } from '../../custom/utils/custom.utils.js';
2
3
  import { IsNum } from '../../guards/data-types/data-types.js';
3
- import { MapOf } from '../../primitives/obj/obj.iterator.js';
4
4
 
5
5
  /**
6
6
  * Accumulates the total divided value of the given numerical values.
@@ -53,27 +53,26 @@ export default function Divide(...nums) {
53
53
  if (ICtr < 2)
54
54
  Raise._MissingParameterError(Method, P[ICtr], undefined);
55
55
 
56
+ const NUMS = NormalizeNumbers(...nums);
56
57
  if (ICtr === 2) {
57
- const [A, B] = MapOf(nums, (num, pos) =>
58
- !IsNum(parseInt(num, 10)) ? Raise._ArgumentError(Method, P[pos], num, "Number")
59
- : parseInt(num, 10)
60
- );
58
+ const [A, B] = NUMS;
61
59
 
62
- return A === 0 ? NaN : A / B;
60
+ return A === 0 || B === 0 ? NaN : A / B;
63
61
  }
64
62
 
65
- const Base = parseInt(nums[0], 10);
66
- if (!IsNum(Base))
67
- Raise._ArgumentError(Method, P[0], nums[0], "Number");
68
-
69
- if (Base === 0)
63
+ const BASE = NUMS[0];
64
+ if (BASE === 0)
70
65
  return NaN;
71
66
 
72
- return nums.slice(1).reduce((acc, num, pos) => {
73
- const pN = parseInt(num, 10);
74
- if (!IsNum(pN))
75
- Raise._ArgumentError(Method, ICtr > 5 ? "nums" : P[pos + 1], num, "Number");
67
+ let Accumulated = BASE;
68
+ for (const NUM of NUMS.slice(1)) {
69
+ if (NUM === 0) {
70
+ Accumulated = NaN;
71
+ break;
72
+ }
73
+
74
+ return Accumulated += NUM;
75
+ }
76
76
 
77
- return acc /= pN;
78
- }, Base);
77
+ return Accumulated;
79
78
  }
@@ -1,6 +1,6 @@
1
1
  import Raise from '../../custom/error/builder/error.builder.js';
2
+ import { NormalizeNumbers } from '../../custom/utils/custom.utils.js';
2
3
  import { IsNum } from '../../guards/data-types/data-types.js';
3
- import { MapOf } from '../../primitives/obj/obj.iterator.js';
4
4
 
5
5
  /**
6
6
  * Accumulates the total multiplied value of the given numerical values.
@@ -53,27 +53,23 @@ export default function Multiply(...nums) {
53
53
  if (ICtr < 2)
54
54
  Raise._MissingParameterError(Method, P[ICtr], undefined);
55
55
 
56
+ const NUMS = NormalizeNumbers(...nums);
56
57
  if (ICtr === 2) {
57
- const [A, B] = MapOf(nums, (num, pos) =>
58
- !IsNum(parseInt(num, 10)) ? Raise._ArgumentError(Method, P[pos], num, "Number")
59
- : parseInt(num, 10)
60
- );
58
+ const [A, B] = NUMS;
61
59
 
62
- return A === 0 ? A : A + B;
60
+ return A === 0 || B === 0 ? 0 : A * B;
63
61
  }
64
62
 
65
- const Base = parseInt(nums[0], 10);
66
- if (!IsNum(Base))
67
- Raise._ArgumentError(Method, P[0], nums[0], "Number");
63
+ const BASE = NUMS[0];
64
+ let Accumulated = BASE;
65
+ for (const NUM of NUMS.slice(1)) {
66
+ if (NUM === 0) {
67
+ Accumulated = 0;
68
+ break;
69
+ }
68
70
 
69
- if (Base === 0)
70
- return 0;
71
-
72
- return nums.slice(1).reduce((acc, num, pos) => {
73
- const pN = parseInt(num, 10);
74
- if (!IsNum(pN))
75
- Raise._ArgumentError(Method, ICtr > 5 ? "nums" : P[pos + 1], num, "Number");
71
+ Accumulated *= NUM;
72
+ }
76
73
 
77
- return acc += pN;
78
- }, Base);
74
+ return Accumulated;
79
75
  }
@@ -1,6 +1,6 @@
1
1
  import Raise from '../../custom/error/builder/error.builder.js';
2
+ import { NormalizeNumbers } from '../../custom/utils/custom.utils.js';
2
3
  import { IsNum } from '../../guards/data-types/data-types.js';
3
- import { MapOf } from '../../primitives/obj/obj.iterator.js';
4
4
 
5
5
  /**
6
6
  * Accumulates the total subtracted value of the given numerical values.
@@ -53,24 +53,12 @@ export default function Subtract(...nums) {
53
53
  if (ICtr < 2)
54
54
  Raise._MissingParameterError(Method, P[ICtr], undefined);
55
55
 
56
+ const NUMS = NormalizeNumbers(...nums);
56
57
  if (ICtr === 2) {
57
- const [A, B] = MapOf(nums, (num, pos) =>
58
- !IsNum(parseInt(num, 10)) ? Raise._ArgumentError(Method, P[pos], num, "Number")
59
- : parseInt(num, 10)
60
- );
58
+ const [A, B] = NUMS;
61
59
 
62
- return A + B;
60
+ return A - B;
63
61
  }
64
62
 
65
- const Base = parseInt(nums[0], 10);
66
- if (!IsNum(Base))
67
- Raise._ArgumentError(Method, P[0], nums[0], "Number");
68
-
69
- return nums.slice(1).reduce((acc, num, pos) => {
70
- const pN = parseInt(num, 10);
71
- if (!IsNum(pN))
72
- Raise._ArgumentError(Method, ICtr > 5 ? "nums" : P[pos + 1], num, "Number");
73
-
74
- return acc += pN;
75
- }, Base);
63
+ return NUMS.reduce((acc, num) => acc -= num, 0);
76
64
  }
@@ -1,4 +1,5 @@
1
1
  import Raise from '../../custom/error/builder/error.builder.js';
2
+ import { NormalizeNumbers } from '../../custom/utils/custom.utils.js';
2
3
  import { IsNum } from '../../guards/data-types/data-types.js';
3
4
  import { MapOf } from '../../primitives/obj/obj.iterator.js';
4
5
 
@@ -53,24 +54,12 @@ export default function Sum(...nums) {
53
54
  if (ICtr < 2)
54
55
  Raise._MissingParameterError(Method, ICtr === 0 ? "num1" : "num2", undefined);
55
56
 
57
+ const NUMS = NormalizeNumbers(...nums);
56
58
  if (ICtr === 2) {
57
- let [n1, n2] = MapOf(nums, (num, pos) =>
58
- !IsNum(parseInt(num)) ? Raise._ArgumentError(Method, P[pos], num, "Number")
59
- : parseInt(num, 10)
60
- );
59
+ const [A, B] = NUMS;
61
60
 
62
- return n1 + n2;
61
+ return A + B;
63
62
  }
64
63
 
65
- const Base = parseInt(nums[0], 10);
66
- if (!IsNum(Base))
67
- Raise._ArgumentError(Method, P[0], nums[0], "Number");
68
-
69
- return nums.slice(1).reduce((acc, num, pos) => {
70
- const pN = parseInt(num, 10);
71
- if (!IsNum(pN))
72
- Raise._ArgumentError(Method, ICtr > 5 ? "nums" : P[pos + 1], num, "Number");
73
-
74
- return acc += pN;
75
- }, Base);
64
+ return NUMS.reduce((acc, num) => acc += num, 0);
76
65
  }
@@ -1,4 +1,4 @@
1
- import { IsNullOrUndefined, IsNum, IsPlainObj, IsStr } from '../../guards/data-types/data-types.js';
1
+ import { IsNullOrUndefined, IsNum, IsPlainObj, IsStr, IsArr } from '../../guards/data-types/data-types.js';
2
2
  import { IsStrEmpty } from '../../guards/formats/formats.js';
3
3
  import { EachOf, MapOf, SomeOf } from '../../primitives/obj/obj.iterator.js';
4
4
  import Raise from '../error/builder/error.builder.js';
@@ -148,3 +148,67 @@ export function Global(key, data, opt = "def") {
148
148
 
149
149
  DefineProperty(globalThis, key, data, opt);
150
150
  }
151
+
152
+ /**
153
+ * Validates the given collection of numerical values whether if its a valid numerical value, and returns
154
+ * the parsed numeric collection of it.
155
+ *
156
+ * ***Note***:
157
+ * - This automatically parse a argument data if its a numerical value in string format.
158
+ *
159
+ * @overload
160
+ * @param { number } num1 - The first numerical value to validate and normalize.
161
+ * @returns { number } The validate and parsed numerical value.
162
+ *
163
+ * @overload
164
+ * @param { number } num1 - The first numerical value to validate and normalize.
165
+ * @param { number } num2 - The second numerical value to validate and normalize.
166
+ * @returns { number[] } The validated and parsed collection of numerical values.
167
+ *
168
+ * @overload
169
+ * @param { number } num1 - The first numerical value to validate and normalize.
170
+ * @param { number } num2 - The second numerical value to validate and normalize.
171
+ * @param { number } num3 - The third numerical value to validate and normalize.
172
+ * @returns { number[] } The validated and parsed collection of numerical values.
173
+ *
174
+ * @overload
175
+ * @param { number } num1 - The first numerical value to validate and normalize.
176
+ * @param { number } num2 - The second numerical value to validate and normalize.
177
+ * @param { number } num3 - The third numerical value to validate and normalize.
178
+ * @param { number } num4 - The fourth numerical value to validate and normalize.
179
+ * @returns { number[] } The validated and parsed collection of numerical values.
180
+ *
181
+ * @overload
182
+ * @param { number } num1 - The first numerical value to validate and normalize.
183
+ * @param { number } num2 - The second numerical value to validate and normalize.
184
+ * @param { number } num3 - The third numerical value to validate and normalize.
185
+ * @param { number } num4 - The fourth numerical value to validate and normalize.
186
+ * @param { number } num5 - The fifth numerical value to validate and normalize.
187
+ * @returns { number[] } The validated and parsed collection of numerical values.
188
+ *
189
+ * @overload
190
+ * @param { ...number } nums - The given collection of numerical values to validate.
191
+ * @returns { number[] } The validated and parsed collection of numerical values.
192
+ */
193
+ export function NormalizeNumbers(...nums) {
194
+ const Method = "NormalizeNumbers", ICtr = nums.length, P = ["num1", "num2", "num3", "num4", "num5"];
195
+
196
+ if (ICtr === 0)
197
+ Raise._MissingParameterError(Method, P[ICtr], undefined);
198
+
199
+ return nums.reduce((acc, num, pos) => {
200
+ const pN = parseInt(num, 10);
201
+ if (!IsNum(pN))
202
+ Raise._ArgumentError(Method, ICtr > 5 ? "nums" : P[pos], num, "Number");
203
+
204
+ if (ICtr > 1) {
205
+ if (!IsArr(acc))
206
+ acc = [];
207
+
208
+ acc.push(pN);
209
+ } else
210
+ acc = pN;
211
+
212
+ return acc;
213
+ });
214
+ }
File without changes