@rinxun/numeric-calculator 1.0.0 → 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.
Files changed (4) hide show
  1. package/README.md +24 -20
  2. package/index.d.ts +18 -12
  3. package/index.js +60 -32
  4. package/package.json +1 -1
package/README.md CHANGED
@@ -35,11 +35,12 @@ This project is licensed under the terms of the [MIT license](https://github.com
35
35
 
36
36
  <h2>Constructor</h2>
37
37
 
38
- - `new NC(number: number, config: object)`
38
+ - `new NC(operand: IOperand, config: IConfig)`
39
39
 
40
40
  - number: Initial numeric
41
41
 
42
42
  - optional
43
+ - `type IOperand = instance | number | string`
43
44
 
44
45
  - config: Configurations
45
46
 
@@ -57,10 +58,10 @@ This project is licensed under the terms of the [MIT license](https://github.com
57
58
 
58
59
  The instance supports chain operations.
59
60
 
60
- - `plus(operands: number[]) => instance `
61
- - `minus(operands: number[]) => instance`
62
- - `times(operands: number[]) => instance`
63
- - `divide(operands: number[]) => instance`
61
+ - `plus(operands: IOperand[]) => instance `
62
+ - `minus(operands: IOperand[]) => instance`
63
+ - `times(operands: IOperand[]) => instance`
64
+ - `divide(operands: IOperand[]) => instance`
64
65
  - `toPrecision(precision?: number) => number`
65
66
 
66
67
 
@@ -85,8 +86,10 @@ import NC from '@rinxun/numeric-calculator';
85
86
  const inst1 = new NC();
86
87
  // or with initial numeric
87
88
  const inst2 = new NC(10);
89
+ const inst3 = new NC('10');
90
+ const inst4 = new NC(new NC(10));
88
91
  // or with config
89
- const inst3 = new NC(10, { enableCheckBoundary: true });
92
+ const inst5 = new NC(10, { enableCheckBoundary: true });
90
93
  ```
91
94
 
92
95
 
@@ -95,9 +98,9 @@ const inst3 = new NC(10, { enableCheckBoundary: true });
95
98
 
96
99
  ```typescript
97
100
  const result = new NC().plus(2.134).toPrecision(); // 2.134
98
- const result = new NC().plus(2.1341, 3.09, 0.65).toPrecision(); // 5.8741
99
- const result = new NC(10).plus(2.134).toPrecision(); // 12.134
100
- const result = new NC(10).plus(2.1341, 3.09, 0.65).toPrecision(); // 15.8741
101
+ const result = new NC().plus(2.1341, new NC(1.09).plus(2), '0.65').toPrecision(); // => 2.1341+(1.09+2)+0.65 = 5.8741
102
+ const result = new NC(10).plus(2.134).toPrecision(); // => 10+2.134 = 12.134
103
+ const result = new NC(10).plus(2.1341, '3.09', 0.65).toPrecision(); // => 10+2.1341+3.09+0.65 = 15.8741
101
104
  ```
102
105
 
103
106
 
@@ -105,10 +108,10 @@ const result = new NC(10).plus(2.1341, 3.09, 0.65).toPrecision(); // 15.8741
105
108
  <h4>inst.minus</h4>
106
109
 
107
110
  ```typescript
108
- const result = new NC().minus(0.4).toPrecision(); // -0.6
109
- const result = new NC().minus(0.25, 1.5, 3.498).toPrecision(); // -4.748
110
- const result = new NC(2).minus(0.4).toPrecision(); // 1.6
111
- const result = new NC(-2).minus(0.25, 1.5, 3.498).toPrecision(); // -7.248
111
+ const result = new NC().minus(0.4).toPrecision(); // 0.4
112
+ const result = new NC().minus(0.25, new NC(2.5).minus(1), '3.498').toPrecision(); // => 0.25-(2.5-1)-3.498 -4.748
113
+ const result = new NC(2).minus(0.4).toPrecision(); // => 2-0.4 = 1.6
114
+ const result = new NC(-2).minus(0.25, 1.5, 3.498).toPrecision(); // -2-0.25-1.5-3.498 = -7.248
112
115
  ```
113
116
 
114
117
 
@@ -117,9 +120,9 @@ const result = new NC(-2).minus(0.25, 1.5, 3.498).toPrecision(); // -7.248
117
120
 
118
121
  ```typescript
119
122
  const result = new NC().times(0.12).toPrecision(); // 0.12
120
- const result = new NC().times(0.12, 100, 5).toPrecision(); // 60
121
- const result = new NC(-4).times(0.12).toPrecision(); // -0.48
122
- const result = new NC(3.2).times(0.12, 100, 5).toPrecision(); // 192
123
+ const result = new NC().times(0.12, new NC(8).times(12.5), '5').toPrecision(); // => 0.12*(8*12.5)*5 = 60
124
+ const result = new NC(-4).times(0.12).toPrecision(); // => -4*0.12 = -0.48
125
+ const result = new NC(3.2).times(0.12, '100', 5).toPrecision(); // => 3.2*0.12*100*5 = 192
123
126
  ```
124
127
 
125
128
 
@@ -128,9 +131,9 @@ const result = new NC(3.2).times(0.12, 100, 5).toPrecision(); // 192
128
131
 
129
132
  ```typescript
130
133
  const result = new NC().divide(3).toPrecision(); // 3
131
- const result = new NC().divide(100, 20, 2).toPrecision(); // 2.5
132
- const result = new NC(36.12).divide(3).toPrecision(); // 12.04
133
- const result = new NC(-1000).divide(100, 20, 2).toPrecision(); // -0.25
134
+ const result = new NC().divide(100, new NC(60).divide(3), '2').toPrecision(); // => 100/(60/3)/2 = 2.5
135
+ const result = new NC(36.12).divide(3).toPrecision(); // => 36.12/3 = 12.04
136
+ const result = new NC(-1000).divide(100, '20', 2).toPrecision(); // -1000/100/20/2 = -0.25
134
137
  ```
135
138
 
136
139
 
@@ -148,7 +151,8 @@ const result = new NC(1.2e-2).toPrecision(); // 0.012
148
151
  <h4>chain operations</h4>
149
152
 
150
153
  ```typescript
151
- const result = new NC(10).plus(2).minus(4).times(2).divide(8).toPrecision(); // 2
154
+ const result = new NC(10).plus(2).minus(4).times(2).divide(8).toPrecision(); // => (10+2-4)*2/8 = 2
155
+ const result = new NC(10).plus(new NC().times(2, 4), '6').divide(new NC(12).minus(4)).toPrecision(); // => (10+(2*4)+6)/(12-4) = 3
152
156
  ```
153
157
 
154
158
 
package/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  declare type IConfig = {
2
2
  enableCheckBoundary?: boolean;
3
3
  };
4
+ declare type IOperand = Calculator | number | string;
4
5
  /**
5
6
  * @description Calculate addition, subtaction, multiplication, division precisely, for tackling the precision issue
6
7
  * @author Janden Ma
@@ -23,10 +24,15 @@ declare class Calculator {
23
24
  */
24
25
  private checkBoundary;
25
26
  /**
26
- * get the length of the mantissa of the numeric
27
+ * @description get the length of the mantissa of the numeric
27
28
  * @param {number} num numeric
28
29
  */
29
30
  private getMantissaLen;
31
+ /**
32
+ * @description according to the type of operand, parse it to number
33
+ * @param {IOperand} operand operand
34
+ */
35
+ private parseOperandToNum;
30
36
  /**
31
37
  * get the precise value
32
38
  * @param {number} num numeric
@@ -59,16 +65,16 @@ declare class Calculator {
59
65
  private processPlus;
60
66
  /**
61
67
  * core processor for times
62
- * @param {number[]} nums operands
68
+ * @param {IOperand[]} operands operands
63
69
  * @param {"plus" | "minus" | "times" | "divide"} op operator
64
70
  */
65
71
  private calculate;
66
72
  /**
67
73
  * @constructor
68
- * @param {number|undefined} num initial operand
74
+ * @param {IOperand|undefined} operand initial operand
69
75
  * @param {Config} config Configurations
70
76
  */
71
- constructor(num?: number, config?: IConfig);
77
+ constructor(operand?: IOperand, config?: IConfig);
72
78
  /**
73
79
  * @description parse the result to precise numeric
74
80
  * @param precision default to 15
@@ -77,23 +83,23 @@ declare class Calculator {
77
83
  toPrecision(precision?: number): number;
78
84
  /**
79
85
  * @description plus operator
80
- * @param {number[]} nums operands
86
+ * @param {Array<IOperand>} operands operands
81
87
  */
82
- plus(...nums: Array<number>): this;
88
+ plus(...operands: Array<IOperand>): this;
83
89
  /**
84
90
  * @description minus operator
85
- * @param {number[]} nums operands
91
+ * @param {Array<IOperand>} operands operands
86
92
  */
87
- minus(...nums: Array<number>): this;
93
+ minus(...operands: Array<IOperand>): this;
88
94
  /**
89
95
  * @description times operator
90
- * @param {number[]} nums operands
96
+ * @param {Array<IOperand>} operands operands
91
97
  */
92
- times(...nums: Array<number>): this;
98
+ times(...operands: Array<IOperand>): this;
93
99
  /**
94
100
  * @description divide operator
95
- * @param {number[]} nums operands
101
+ * @param {Array<IOperand>} operands operands
96
102
  */
97
- divide(...nums: Array<number>): this;
103
+ divide(...operands: Array<IOperand>): this;
98
104
  }
99
105
  export default Calculator;
package/index.js CHANGED
@@ -17,10 +17,10 @@ var Calculator = /** @class */ (function () {
17
17
  // #region Constructor
18
18
  /**
19
19
  * @constructor
20
- * @param {number|undefined} num initial operand
20
+ * @param {IOperand|undefined} operand initial operand
21
21
  * @param {Config} config Configurations
22
22
  */
23
- function Calculator(num, config) {
23
+ function Calculator(operand, config) {
24
24
  // #region Private Variables
25
25
  /**
26
26
  * @private
@@ -32,8 +32,8 @@ var Calculator = /** @class */ (function () {
32
32
  * @description for debugging, if true, it will check if the value is out of the safe boundary
33
33
  */
34
34
  this._enableCheckBoundary = false;
35
- if (num !== undefined && num !== null) {
36
- this._VALUE = num;
35
+ if (operand !== undefined && operand !== null) {
36
+ this._VALUE = this.parseOperandToNum(operand);
37
37
  }
38
38
  if (config) {
39
39
  if (config.enableCheckBoundary) {
@@ -56,7 +56,7 @@ var Calculator = /** @class */ (function () {
56
56
  }
57
57
  };
58
58
  /**
59
- * get the length of the mantissa of the numeric
59
+ * @description get the length of the mantissa of the numeric
60
60
  * @param {number} num numeric
61
61
  */
62
62
  Calculator.prototype.getMantissaLen = function (num) {
@@ -67,6 +67,22 @@ var Calculator = /** @class */ (function () {
67
67
  var len = decimals.length - +exponentSuffix;
68
68
  return len > 0 ? len : 0;
69
69
  };
70
+ /**
71
+ * @description according to the type of operand, parse it to number
72
+ * @param {IOperand} operand operand
73
+ */
74
+ Calculator.prototype.parseOperandToNum = function (operand) {
75
+ if (operand instanceof Calculator) {
76
+ return operand._VALUE;
77
+ }
78
+ else if (typeof operand === "number") {
79
+ return operand;
80
+ }
81
+ else {
82
+ var val = Number(operand);
83
+ return Number.isNaN(val) ? 0 : val;
84
+ }
85
+ };
70
86
  /**
71
87
  * get the precise value
72
88
  * @param {number} num numeric
@@ -146,32 +162,44 @@ var Calculator = /** @class */ (function () {
146
162
  };
147
163
  /**
148
164
  * core processor for times
149
- * @param {number[]} nums operands
165
+ * @param {IOperand[]} operands operands
150
166
  * @param {"plus" | "minus" | "times" | "divide"} op operator
151
167
  */
152
- Calculator.prototype.calculate = function (nums, op) {
168
+ Calculator.prototype.calculate = function (operands, op) {
153
169
  var _this = this;
154
- var operands = __spreadArray([], nums, true);
170
+ var values = __spreadArray([], operands, true);
155
171
  var firstOperand = this._VALUE;
172
+ var analyzeFirstOperand = function () {
173
+ var parsedVal = _this.parseOperandToNum(values[0]);
174
+ values.shift();
175
+ if (parsedVal === undefined) {
176
+ return analyzeFirstOperand();
177
+ }
178
+ return parsedVal;
179
+ };
156
180
  if (firstOperand === undefined) {
157
- firstOperand = operands[0];
158
- operands.shift();
181
+ firstOperand = analyzeFirstOperand();
159
182
  }
160
- var result = operands.reduce(function (prev, operand) {
183
+ var result = values.reduce(function (prevOperand, currOperand) {
184
+ var prev = _this.parseOperandToNum(prevOperand);
185
+ var current = _this.parseOperandToNum(currOperand);
186
+ if (!current) {
187
+ return prev;
188
+ }
161
189
  switch (op) {
162
190
  case "plus":
163
- return _this.processPlus(prev, operand);
191
+ return _this.processPlus(prev, current);
164
192
  case "minus":
165
- return _this.processMinus(prev, operand);
193
+ return _this.processMinus(prev, current);
166
194
  case "times":
167
- return _this.processTimes(prev, operand);
195
+ return _this.processTimes(prev, current);
168
196
  case "divide":
169
- return _this.processDivide(prev, operand);
197
+ return _this.processDivide(prev, current);
170
198
  default:
171
199
  return prev;
172
200
  }
173
201
  }, firstOperand);
174
- this._VALUE = result;
202
+ this._VALUE = this.parseOperandToNum(result);
175
203
  };
176
204
  // #endregion
177
205
  // #region Public Functions
@@ -185,50 +213,50 @@ var Calculator = /** @class */ (function () {
185
213
  };
186
214
  /**
187
215
  * @description plus operator
188
- * @param {number[]} nums operands
216
+ * @param {Array<IOperand>} operands operands
189
217
  */
190
218
  Calculator.prototype.plus = function () {
191
- var nums = [];
219
+ var operands = [];
192
220
  for (var _i = 0; _i < arguments.length; _i++) {
193
- nums[_i] = arguments[_i];
221
+ operands[_i] = arguments[_i];
194
222
  }
195
- this.calculate(nums, "plus");
223
+ this.calculate(operands, "plus");
196
224
  return this;
197
225
  };
198
226
  /**
199
227
  * @description minus operator
200
- * @param {number[]} nums operands
228
+ * @param {Array<IOperand>} operands operands
201
229
  */
202
230
  Calculator.prototype.minus = function () {
203
- var nums = [];
231
+ var operands = [];
204
232
  for (var _i = 0; _i < arguments.length; _i++) {
205
- nums[_i] = arguments[_i];
233
+ operands[_i] = arguments[_i];
206
234
  }
207
- this.calculate(nums, "minus");
235
+ this.calculate(operands, "minus");
208
236
  return this;
209
237
  };
210
238
  /**
211
239
  * @description times operator
212
- * @param {number[]} nums operands
240
+ * @param {Array<IOperand>} operands operands
213
241
  */
214
242
  Calculator.prototype.times = function () {
215
- var nums = [];
243
+ var operands = [];
216
244
  for (var _i = 0; _i < arguments.length; _i++) {
217
- nums[_i] = arguments[_i];
245
+ operands[_i] = arguments[_i];
218
246
  }
219
- this.calculate(nums, "times");
247
+ this.calculate(operands, "times");
220
248
  return this;
221
249
  };
222
250
  /**
223
251
  * @description divide operator
224
- * @param {number[]} nums operands
252
+ * @param {Array<IOperand>} operands operands
225
253
  */
226
254
  Calculator.prototype.divide = function () {
227
- var nums = [];
255
+ var operands = [];
228
256
  for (var _i = 0; _i < arguments.length; _i++) {
229
- nums[_i] = arguments[_i];
257
+ operands[_i] = arguments[_i];
230
258
  }
231
- this.calculate(nums, "divide");
259
+ this.calculate(operands, "divide");
232
260
  return this;
233
261
  };
234
262
  return Calculator;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rinxun/numeric-calculator",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Calculate addition, subtaction, multiplication, division precisely, for tackling the precision issue",
5
5
  "keywords": [
6
6
  "long",