@numio/bigmath 1.0.4 → 1.0.6
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/README.md +98 -54
- package/index.d.ts +9 -5
- package/index.js +9 -5
- package/package.json +12 -2
- package/src/compare/main.d.ts +7 -0
- package/src/compare/main.js +78 -0
- package/src/compare/types.d.ts +6 -0
- package/src/{add → mathOperations/add}/index.js +2 -2
- package/src/{add → mathOperations/add}/types.d.ts +1 -1
- package/src/mathOperations/add/utils.d.ts +5 -0
- package/src/{div → mathOperations/div}/index.js +2 -2
- package/src/mathOperations/div/types.d.ts +3 -0
- package/src/mathOperations/div/utils.d.ts +3 -0
- package/src/{mul → mathOperations/mul}/index.js +2 -2
- package/src/{mul → mathOperations/mul}/types.d.ts +1 -1
- package/src/{mul → mathOperations/mul}/utils.d.ts +2 -2
- package/src/{sub → mathOperations/sub}/index.js +2 -2
- package/src/{sub → mathOperations/sub}/types.d.ts +1 -1
- package/src/{sub → mathOperations/sub}/utils.d.ts +2 -2
- package/src/mean/main.d.ts +2 -0
- package/src/mean/main.js +4 -0
- package/src/mean/types.d.ts +1 -0
- package/src/pipe/main.js +4 -4
- package/src/quartile/main.d.ts +2 -0
- package/src/quartile/main.js +19 -0
- package/src/quartile/types.d.ts +6 -0
- package/src/shared/utils.js +7 -4
- package/src/sort/constants.d.ts +3 -0
- package/src/sort/constants.js +3 -0
- package/src/sort/main.d.ts +2 -0
- package/src/sort/main.js +43 -0
- package/src/sort/types.d.ts +6 -0
- package/src/types.d.ts +1 -0
- package/src/add/utils.d.ts +0 -5
- package/src/div/types.d.ts +0 -2
- package/src/div/utils.d.ts +0 -4
- /package/src/{add → mathOperations/add}/index.d.ts +0 -0
- /package/src/{add → mathOperations/add}/utils.js +0 -0
- /package/src/{div → mathOperations/div}/index.d.ts +0 -0
- /package/src/{div → mathOperations/div}/utils.js +0 -0
- /package/src/{mul → mathOperations/mul}/index.d.ts +0 -0
- /package/src/{mul → mathOperations/mul}/utils.js +0 -0
- /package/src/{sub → mathOperations/sub}/index.d.ts +0 -0
- /package/src/{sub → mathOperations/sub}/utils.js +0 -0
package/README.md
CHANGED
@@ -1,56 +1,48 @@
|
|
1
|
-
# @numio/bigmath
|
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
|
-
## Use Cases
|
39
|
-
|
40
|
-
This library is particularly useful in scenarios where precise calculations with
|
41
|
-
large numbers are essential, such as:
|
42
|
-
|
43
|
-
- **Financial applications:** Dealing with large sums of money or precise
|
44
|
-
interest calculations.
|
45
|
-
- **Scientific computing:** Working with very large or small numbers in
|
46
|
-
scientific simulations.
|
47
|
-
- **Cryptography:** Implementing cryptographic algorithms that require high
|
48
|
-
precision.
|
49
|
-
- **Any application** where exceeding JavaScript's number limits is a concern.
|
1
|
+
# @numio/bigmath: Precise Arithmetic Beyond JavaScript's Limits
|
2
|
+
|
3
|
+
**Do you struggle with inaccurate calculations involving very large or very small numbers, or decimal numbers with high precision in JavaScript?**
|
4
|
+
|
5
|
+
`@numio/bigmath` is your solution! This library provides a robust set of functions for performing **arbitrary-precision arithmetic**, effectively overcoming the limitations of JavaScript's built-in `Number` type. Say goodbye to unexpected rounding errors and precision loss when dealing with numbers that exceed 15 significant digits or involve complex decimal operations.
|
6
|
+
|
7
|
+
## Why Choose @numio/bigmath?
|
8
|
+
|
9
|
+
**Solve Precision Problems:**
|
10
|
+
|
11
|
+
* **Handle Numbers of Any Size:** Perform calculations on integers and decimals of virtually unlimited length, without the risk of JavaScript's `Number` limitations.
|
12
|
+
* **Eliminate Precision Loss:** Achieve accurate results even with numeric literals exceeding 15 significant digits, ensuring the integrity of your calculations.
|
13
|
+
* **Precise Decimal Operations:** Execute addition, subtraction, multiplication, and division on decimal numbers with guaranteed accuracy, including scenarios with negative values.
|
14
|
+
|
15
|
+
**Unlock Advanced Numerical Operations:**
|
16
|
+
|
17
|
+
* **Control Division Precision:** Specify the exact number of digits after the decimal point for division results, with a default precision of 20 digits for high accuracy.
|
18
|
+
* **Flexible Rounding:** Round numbers to the nearest integer or a specific number of decimal places with various rounding modes (up, down, half-up, half-down, half-even, half-odd) to meet your exact requirements.
|
19
|
+
* **Round Based on Significant Figures:** Control rounding based on the number of significant figures, crucial for scientific and engineering applications.
|
20
|
+
* **Chain Operations with Pipe:** Simplify complex calculations by chaining arithmetic operations in a readable and intuitive manner.
|
21
|
+
* **Analyze Data Distribution:** Calculate quartiles (Q1, Q2, Q3) to understand the spread and central tendency of your numerical data, helping identify outliers and the shape of the distribution.
|
22
|
+
* **Sort Numbers Accurately:** Sort arrays of numbers, including negative and decimal values, in ascending or descending order, correctly handling string representations of numbers that JavaScript's native sort might misinterpret.
|
23
|
+
* **Calculate Central Tendency:** Easily compute the mean (average) of a set of numbers.
|
24
|
+
* **Identify Extremes:** Find the maximum and minimum values within an array of numbers.
|
25
|
+
|
26
|
+
## When is @numio/bigmath essential?
|
27
|
+
|
28
|
+
This library is particularly invaluable in applications where numerical accuracy and the ability to handle large numbers are paramount:
|
29
|
+
|
30
|
+
* **Financial Applications:** Accurate calculations for large sums of money, precise interest rates, and complex financial modeling.
|
31
|
+
* **Scientific Computing:** Working with extremely large or small numbers in simulations, data analysis, and research.
|
32
|
+
* **Cryptography:** Implementing cryptographic algorithms that rely on high-precision arithmetic.
|
33
|
+
* **E-commerce and Payments:** Handling precise amounts and avoiding rounding errors in transactions.
|
34
|
+
* **Data Analysis and Statistics:** Performing accurate statistical calculations on datasets with varying scales.
|
35
|
+
* **Any Scenario Exceeding JavaScript's Number Limits:** Ensuring the reliability of your calculations when dealing with numbers beyond the safe integer limit or requiring more than 15 significant digits.
|
36
|
+
|
37
|
+
With `@numio/bigmath`, you can confidently perform complex arithmetic operations with the assurance of accuracy, regardless of the size or precision of the numbers involved.
|
50
38
|
|
51
39
|
### Latest update
|
52
40
|
|
53
|
-
Added
|
41
|
+
Added sorting, min/max, mean.
|
42
|
+
|
43
|
+
Added quartile functionality. When you might need it?
|
44
|
+
Understanding Distribution. Quartiles (Q1, Q2, and Q3) split a dataset into four groups, each representing 25% of the data.
|
45
|
+
Identifying Outliers. They help visualize the shape and spread of the data, revealing whether it's skewed or symmetrical.
|
54
46
|
|
55
47
|
# Install:
|
56
48
|
|
@@ -208,7 +200,56 @@ pipe.add(addNums) // 6
|
|
208
200
|
.div(divNums) // 6 / 4 = 1.5
|
209
201
|
.sub(subNums) // 1.5 - 0.2 - 0.3 = 1
|
210
202
|
.mul(mulNums) // 1 * 2 * 5 * 0.2 = 2
|
211
|
-
.calc()
|
203
|
+
.calc() // convert end result to readable string
|
204
|
+
```
|
205
|
+
|
206
|
+
### Quartile
|
207
|
+
```javascript
|
208
|
+
import { quartile } from "@numio/bigmath";
|
209
|
+
|
210
|
+
quartile(["1", "2", "3", "4", "5", "6", "7", "8", "9"]) // { Q1: "2.5", Q2: "5", Q3: "7.5" }
|
211
|
+
quartile(["0.001", "0.3", "0.4", "1"]) // { Q1: "0.1505", Q2: "0.35", Q3: "0.7" }
|
212
|
+
|
213
|
+
```
|
214
|
+
|
215
|
+
### Sort
|
216
|
+
```javascript
|
217
|
+
import { sort } from "@numio/bigmath";
|
218
|
+
|
219
|
+
// native js sort for strings
|
220
|
+
["1", "10", "11", "101", "11", "10", "1"].sort() // ["1", "1", "10", "10", "101", "11", "11"]
|
221
|
+
|
222
|
+
// sort from "@numio/bigmath"
|
223
|
+
sort(["1", "10", "11", "101", "11", "10", "1"]) // ["1", "1", "10", "10", "11", "11", "101"]
|
224
|
+
|
225
|
+
// ASC sorting
|
226
|
+
sort(["-0.1", "0.1", "-1"], "asc") // ["-1", "-0.1", "0.1"]
|
227
|
+
|
228
|
+
// DESC sorting
|
229
|
+
sort(["-0.1", "0.1", "-1"], "desc") // ["0.1", "-0.1", "1"]
|
230
|
+
|
231
|
+
```
|
232
|
+
|
233
|
+
### Mean
|
234
|
+
```javascript
|
235
|
+
import { mean } from "@numio/bigmath";
|
236
|
+
|
237
|
+
mean(["5", "4", "3", "2", "1", "0"]) // "2.5"
|
238
|
+
mean(["0.5", "0.4", "0.3", "0.2", "0.1", "0"]) // "0.25"
|
239
|
+
```
|
240
|
+
|
241
|
+
### Max
|
242
|
+
```javascript
|
243
|
+
import { max } from "@numio/bigmath";
|
244
|
+
|
245
|
+
max(["2", "-1", "0.1"]) // 2;
|
246
|
+
```
|
247
|
+
|
248
|
+
### Min
|
249
|
+
```javascript
|
250
|
+
import { min } from "@numio/bigmath";
|
251
|
+
|
252
|
+
min(["2", "-1", "0.1"]) // -1;
|
212
253
|
```
|
213
254
|
|
214
255
|
Does not have a limitation on the number of digits. You can use any length you'd
|
@@ -227,7 +268,10 @@ const float = mul(
|
|
227
268
|
); // 0.00000000000000000000000000000000000000000000000000000000000000000000000000018
|
228
269
|
```
|
229
270
|
|
230
|
-
Download from NPM - https://www.npmjs.com/package/@numio/bigmath
|
231
|
-
|
232
|
-
|
271
|
+
Download from NPM - https://www.npmjs.com/package/@numio/bigmath
|
272
|
+
|
273
|
+
Download from JSR - https://jsr.io/@numio/bigmath
|
274
|
+
|
275
|
+
Home page - https://github.com/shpunter/numio-bigmath/blob/main/README.md
|
276
|
+
|
233
277
|
License - https://github.com/shpunter/numio-bigmath/blob/main/LICENSE
|
package/index.d.ts
CHANGED
@@ -1,7 +1,11 @@
|
|
1
|
-
import { add } from "./src/add/index.d.ts";
|
2
|
-
import { mul } from "./src/mul/index.d.ts";
|
3
|
-
import { sub } from "./src/sub/index.d.ts";
|
4
|
-
import { div } from "./src/div/index.d.ts";
|
1
|
+
import { add } from "./src/mathOperations/add/index.d.ts";
|
2
|
+
import { mul } from "./src/mathOperations/mul/index.d.ts";
|
3
|
+
import { sub } from "./src/mathOperations/sub/index.d.ts";
|
4
|
+
import { div } from "./src/mathOperations/div/index.d.ts";
|
5
5
|
import { round } from "./src/round/index.d.ts";
|
6
6
|
import { pipe } from "./src/pipe/main.d.ts";
|
7
|
-
|
7
|
+
import { quartile } from "./src/quartile/main.d.ts";
|
8
|
+
import { sort } from "./src/sort/main.d.ts";
|
9
|
+
import { mean } from "./src/mean/main.d.ts";
|
10
|
+
import { min, max } from "./src/compare/main.d.ts";
|
11
|
+
export { add, div, max, mean, min, mul, pipe, quartile, round, sort, sub };
|
package/index.js
CHANGED
@@ -1,7 +1,11 @@
|
|
1
|
-
import { add } from "./src/add/index.js";
|
2
|
-
import { mul } from "./src/mul/index.js";
|
3
|
-
import { sub } from "./src/sub/index.js";
|
4
|
-
import { div } from "./src/div/index.js";
|
1
|
+
import { add } from "./src/mathOperations/add/index.js";
|
2
|
+
import { mul } from "./src/mathOperations/mul/index.js";
|
3
|
+
import { sub } from "./src/mathOperations/sub/index.js";
|
4
|
+
import { div } from "./src/mathOperations/div/index.js";
|
5
5
|
import { round } from "./src/round/index.js";
|
6
6
|
import { pipe } from "./src/pipe/main.js";
|
7
|
-
|
7
|
+
import { quartile } from "./src/quartile/main.js";
|
8
|
+
import { sort } from "./src/sort/main.js";
|
9
|
+
import { mean } from "./src/mean/main.js";
|
10
|
+
import { min, max } from "./src/compare/main.js";
|
11
|
+
export { add, div, max, mean, min, mul, pipe, quartile, round, sort, sub };
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@numio/bigmath",
|
3
3
|
"description": "@numio/bigmath is an arbitrary-precision arithmetic library. It can be used for basic operations with decimal numbers (integers and float)",
|
4
|
-
"version": "1.0.
|
4
|
+
"version": "1.0.6",
|
5
5
|
"keywords": [
|
6
6
|
"precision",
|
7
7
|
"arithmetic",
|
@@ -28,7 +28,17 @@
|
|
28
28
|
"divide",
|
29
29
|
"long division",
|
30
30
|
"bigmath",
|
31
|
-
"numio"
|
31
|
+
"numio",
|
32
|
+
"quartile",
|
33
|
+
"Q1",
|
34
|
+
"Q2",
|
35
|
+
"Q3",
|
36
|
+
"round",
|
37
|
+
"min",
|
38
|
+
"max",
|
39
|
+
"sort",
|
40
|
+
"mean",
|
41
|
+
"significant figures"
|
32
42
|
],
|
33
43
|
"repository": {
|
34
44
|
"type": "git",
|
@@ -0,0 +1,7 @@
|
|
1
|
+
import type { CompareRawFn, Max, MaxRawFn, Min, MinRawFn } from "./types.d.ts";
|
2
|
+
export declare const compareRawFn: CompareRawFn;
|
3
|
+
export declare const maxRawFn: MaxRawFn;
|
4
|
+
export declare const minRawFn: MinRawFn;
|
5
|
+
export declare const max: Max;
|
6
|
+
export declare const min: Min;
|
7
|
+
export declare const isGreater: (l: string, r: string) => boolean;
|
@@ -0,0 +1,78 @@
|
|
1
|
+
import { a2s, s2a } from "../shared/utils.js";
|
2
|
+
export var compareRawFn = function (l, r) {
|
3
|
+
var _a;
|
4
|
+
var idx = 0;
|
5
|
+
var _b = [l, r], left = _b[0], right = _b[1];
|
6
|
+
var lIsNeg = l.isNegative, lIntLen = l.intLength;
|
7
|
+
var rIsNeg = r.isNegative, rIntLen = r.intLength;
|
8
|
+
var lenL = l.array.length;
|
9
|
+
var lenR = r.array.length;
|
10
|
+
var bothNeg = lIsNeg && rIsNeg;
|
11
|
+
var bothPos = !lIsNeg && !rIsNeg;
|
12
|
+
var bothIntPos = lIntLen > 0 && rIntLen > 0;
|
13
|
+
var bothIntNeg = lIntLen <= 0 && rIntLen <= 0;
|
14
|
+
if (lIsNeg && !rIsNeg)
|
15
|
+
return [r, false];
|
16
|
+
if (!lIsNeg && rIsNeg)
|
17
|
+
return [l, true];
|
18
|
+
if (bothPos && bothIntPos && lIntLen < rIntLen)
|
19
|
+
return [r, false];
|
20
|
+
if (bothPos && bothIntNeg && lIntLen < rIntLen)
|
21
|
+
return [r, false];
|
22
|
+
if (bothNeg && bothIntPos && lIntLen < rIntLen)
|
23
|
+
return [l, true];
|
24
|
+
if (bothNeg && bothIntNeg && lIntLen < rIntLen)
|
25
|
+
return [l, true];
|
26
|
+
if (bothNeg && bothIntNeg && lIntLen > rIntLen)
|
27
|
+
return [r, false];
|
28
|
+
if (bothNeg && bothIntPos && lIntLen > rIntLen)
|
29
|
+
return [r, false];
|
30
|
+
if (bothPos && bothIntPos && lIntLen > rIntLen)
|
31
|
+
return [l, true];
|
32
|
+
if (bothPos && bothIntNeg && lIntLen > rIntLen)
|
33
|
+
return [l, true];
|
34
|
+
if (bothNeg)
|
35
|
+
_a = [r, l], left = _a[0], right = _a[1];
|
36
|
+
while (idx < (lenL > lenR ? lenL : lenR)) {
|
37
|
+
var numL = left.array[idx];
|
38
|
+
var numR = right.array[idx];
|
39
|
+
if (!numL && bothPos)
|
40
|
+
return [r, false];
|
41
|
+
if (!numR && bothPos)
|
42
|
+
return [l, true];
|
43
|
+
if (!numL && bothNeg)
|
44
|
+
return [l, true];
|
45
|
+
if (!numR && bothNeg)
|
46
|
+
return [r, false];
|
47
|
+
if (numL !== numR) {
|
48
|
+
return numL > numR ? [l, true] : [r, false];
|
49
|
+
}
|
50
|
+
idx += 1;
|
51
|
+
}
|
52
|
+
return [l, true];
|
53
|
+
};
|
54
|
+
export var maxRawFn = function (array) {
|
55
|
+
var max = array[0];
|
56
|
+
for (var i = 1; i < array.length; i++) {
|
57
|
+
compareRawFn(max, array[i])[1] || (max = array[i]);
|
58
|
+
}
|
59
|
+
return max;
|
60
|
+
};
|
61
|
+
export var minRawFn = function (array) {
|
62
|
+
var min = array[0];
|
63
|
+
for (var i = 1; i < array.length; i++) {
|
64
|
+
compareRawFn(min, array[i])[1] && (min = array[i]);
|
65
|
+
}
|
66
|
+
return min;
|
67
|
+
};
|
68
|
+
export var max = function (strs) {
|
69
|
+
var array = strs.map(function (str) { return s2a(str); });
|
70
|
+
return a2s(maxRawFn(array));
|
71
|
+
};
|
72
|
+
export var min = function (strs) {
|
73
|
+
var array = strs.map(function (str) { return s2a(str); });
|
74
|
+
return a2s(minRawFn(array));
|
75
|
+
};
|
76
|
+
export var isGreater = function (l, r) {
|
77
|
+
return compareRawFn(s2a(l), s2a(r))[1];
|
78
|
+
};
|
@@ -0,0 +1,6 @@
|
|
1
|
+
import type { InputData } from "../types.d.ts";
|
2
|
+
export type CompareRawFn = (left: InputData, right: InputData) => [InputData, boolean];
|
3
|
+
export type Min = (strs: string[]) => string;
|
4
|
+
export type Max = (strs: string[]) => string;
|
5
|
+
export type MinRawFn = (array: InputData[]) => InputData;
|
6
|
+
export type MaxRawFn = (array: InputData[]) => InputData;
|
@@ -1,5 +1,5 @@
|
|
1
|
-
import { DEFAULT } from "
|
2
|
-
import { a2s, s2a } from "
|
1
|
+
import { DEFAULT } from "../../shared/constant.js";
|
2
|
+
import { a2s, s2a } from "../../shared/utils.js";
|
3
3
|
import { addRoute } from "./utils.js";
|
4
4
|
/** This function adds numbers (as string). */
|
5
5
|
export function add(strs) {
|
@@ -1,2 +1,2 @@
|
|
1
|
-
import type { InputData } from "
|
1
|
+
import type { InputData } from "../../types.d.ts";
|
2
2
|
export type Addition = (L: [number[], number], R: [number[], number], isNegative: boolean) => InputData;
|
@@ -1,5 +1,5 @@
|
|
1
|
-
import { DEFAULT } from "
|
2
|
-
import { a2s, s2a } from "
|
1
|
+
import { DEFAULT } from "../../shared/constant.js";
|
2
|
+
import { a2s, s2a } from "../../shared/utils.js";
|
3
3
|
import { divRoute } from "./utils.js";
|
4
4
|
/** This function should divide numbers (as string). */
|
5
5
|
export var div = function (strs, limit) {
|
@@ -1,5 +1,5 @@
|
|
1
|
-
import { DEFAULT } from "
|
2
|
-
import { a2s, s2a } from "
|
1
|
+
import { DEFAULT } from "../../shared/constant.js";
|
2
|
+
import { a2s, s2a } from "../../shared/utils.js";
|
3
3
|
import { mulRoute } from "./utils.js";
|
4
4
|
/** This function multiplies numbers (as string). */
|
5
5
|
export var mul = function (strs) {
|
@@ -1,2 +1,2 @@
|
|
1
|
-
import type { InputData } from "
|
1
|
+
import type { InputData } from "../../types.d.ts";
|
2
2
|
export type Multiplication = (arrL: [number[], number], arrR: [number[], number], isNegative: boolean) => InputData;
|
@@ -1,5 +1,5 @@
|
|
1
|
-
import type {
|
1
|
+
import type { Route } from "../../types.d.ts";
|
2
2
|
import type { Multiplication } from "./types.d.ts";
|
3
3
|
/** This function multiplies 2 numbers (as array). */
|
4
4
|
export declare const multiplication: Multiplication;
|
5
|
-
export declare const mulRoute:
|
5
|
+
export declare const mulRoute: Route;
|
@@ -1,5 +1,5 @@
|
|
1
|
-
import { DEFAULT } from "
|
2
|
-
import { a2s, s2a } from "
|
1
|
+
import { DEFAULT } from "../../shared/constant.js";
|
2
|
+
import { a2s, s2a } from "../../shared/utils.js";
|
3
3
|
import { subRoute } from "./utils.js";
|
4
4
|
/** This function subtracts numbers (as string). */
|
5
5
|
export function sub(strs) {
|
@@ -1,2 +1,2 @@
|
|
1
|
-
import type { InputData } from "
|
1
|
+
import type { InputData } from "../../types.d.ts";
|
2
2
|
export type Subtract = (L: [number[], number], R: [number[], number]) => InputData;
|
@@ -1,5 +1,5 @@
|
|
1
|
-
import type {
|
1
|
+
import type { Route } from "../../types.d.ts";
|
2
2
|
import type { Subtract } from "./types.d.ts";
|
3
3
|
/** This function subtracts 2 numbers (as array). */
|
4
4
|
export declare const subtract: Subtract;
|
5
|
-
export declare const subRoute:
|
5
|
+
export declare const subRoute: Route;
|
package/src/mean/main.js
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export type Mean = (array: string[]) => string;
|
package/src/pipe/main.js
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
import { addRoute } from "../add/utils.js";
|
2
|
-
import { divRoute } from "../div/utils.js";
|
3
|
-
import { mulRoute } from "../mul/utils.js";
|
4
1
|
import { DEFAULT } from "../shared/constant.js";
|
5
2
|
import { a2s, s2a } from "../shared/utils.js";
|
6
|
-
import { subRoute } from "../sub/utils.js";
|
3
|
+
import { subRoute } from "../mathOperations/sub/utils.js";
|
4
|
+
import { addRoute } from "../mathOperations/add/utils.js";
|
5
|
+
import { divRoute } from "../mathOperations/div/utils.js";
|
6
|
+
import { mulRoute } from "../mathOperations/mul/utils.js";
|
7
7
|
var Pipe = /** @class */ (function () {
|
8
8
|
function Pipe() {
|
9
9
|
this.result = DEFAULT;
|
@@ -0,0 +1,19 @@
|
|
1
|
+
import { pipe } from "../pipe/main.js";
|
2
|
+
var mean = function (idx, array) {
|
3
|
+
return pipe.add([array[idx], array[idx - 1]]).div(["2"]).calc();
|
4
|
+
};
|
5
|
+
export var quartile = function (array) {
|
6
|
+
if (array.length < 3) {
|
7
|
+
throw Error("To calculate quartile you need at least 3 elements");
|
8
|
+
}
|
9
|
+
var length = array.length;
|
10
|
+
var Q2Idx = length >> 1;
|
11
|
+
var Q1Idx = Q2Idx >> 1;
|
12
|
+
var Q3Idx = length - Q1Idx;
|
13
|
+
var isEvenHalf = Q2Idx % 2 !== 0;
|
14
|
+
return {
|
15
|
+
Q1: isEvenHalf ? array[Q1Idx] : mean(Q1Idx, array),
|
16
|
+
Q2: length % 2 !== 0 ? array[Q2Idx] : mean(Q2Idx, array),
|
17
|
+
Q3: isEvenHalf ? array[Q3Idx - 1] : mean(Q3Idx, array),
|
18
|
+
};
|
19
|
+
};
|
package/src/shared/utils.js
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
var convert = function (isNegative, array) {
|
2
|
+
return (isNegative ? "-" : "") + String.fromCharCode.apply(String, array);
|
3
|
+
};
|
1
4
|
export var a2s = function (_a) {
|
2
5
|
var array = _a.array, isFloat = _a.isFloat, isNegative = _a.isNegative, intLength = _a.intLength;
|
3
6
|
var result = [];
|
@@ -21,14 +24,14 @@ export var a2s = function (_a) {
|
|
21
24
|
}
|
22
25
|
else {
|
23
26
|
for (var i = 0; i < lastValuableIdx; i++) {
|
24
|
-
|
25
|
-
result.push(46);
|
27
|
+
i === intLength && result.push(46);
|
26
28
|
result.push(array[i]);
|
27
29
|
}
|
28
30
|
}
|
29
|
-
|
31
|
+
result.at(-1) === 46 && result.pop();
|
32
|
+
return convert(isNegative, result);
|
30
33
|
}
|
31
|
-
return (isNegative
|
34
|
+
return convert(isNegative, array);
|
32
35
|
};
|
33
36
|
export var s2a = function (string) {
|
34
37
|
var array = Array(0);
|
package/src/sort/main.js
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
import { compareRawFn } from "../compare/main.js";
|
2
|
+
import { a2s, s2a } from "../shared/utils.js";
|
3
|
+
import { ASC } from "./constants.js";
|
4
|
+
var heapify = function (array, len, i, sorting) {
|
5
|
+
var _a;
|
6
|
+
var idxL = 2 * i + 1;
|
7
|
+
var idxR = idxL + 1;
|
8
|
+
var idx = i;
|
9
|
+
if (sorting === ASC) {
|
10
|
+
if (idxL < len && compareRawFn(array[idxL], array[idx])[1])
|
11
|
+
idx = idxL;
|
12
|
+
if (idxR < len && compareRawFn(array[idxR], array[idx])[1])
|
13
|
+
idx = idxR;
|
14
|
+
}
|
15
|
+
else {
|
16
|
+
if (idxL < len && compareRawFn(array[idx], array[idxL])[1])
|
17
|
+
idx = idxL;
|
18
|
+
if (idxR < len && compareRawFn(array[idx], array[idxR])[1])
|
19
|
+
idx = idxR;
|
20
|
+
}
|
21
|
+
if (idx !== i) {
|
22
|
+
_a = [array[idx], array[i]], array[i] = _a[0], array[idx] = _a[1];
|
23
|
+
heapify(array, len, idx, sorting);
|
24
|
+
}
|
25
|
+
};
|
26
|
+
var sortRawFn = function (array, sorting) {
|
27
|
+
var _a;
|
28
|
+
var len = array.length;
|
29
|
+
for (var i = (len >> 1) - 1; i >= 0; i--) {
|
30
|
+
heapify(array, len, i, sorting);
|
31
|
+
}
|
32
|
+
for (var i = len - 1; i >= 0; i--) {
|
33
|
+
_a = [array[i], array[0]], array[0] = _a[0], array[i] = _a[1];
|
34
|
+
heapify(array, i, 0, sorting);
|
35
|
+
}
|
36
|
+
return array;
|
37
|
+
};
|
38
|
+
export var sort = function (array, sorting) {
|
39
|
+
if (sorting === void 0) { sorting = ASC; }
|
40
|
+
var inputDataArray = array.map(function (str) { return s2a(str); });
|
41
|
+
sortRawFn(inputDataArray, sorting);
|
42
|
+
return inputDataArray.map(function (input) { return a2s(input); });
|
43
|
+
};
|
@@ -0,0 +1,6 @@
|
|
1
|
+
import type { InputData } from "../types.d.ts";
|
2
|
+
import type { sortingArray } from "./constants.d.ts";
|
3
|
+
export type Sorting = typeof sortingArray[number];
|
4
|
+
export type Sort = (array: string[], sorting?: Sorting) => string[];
|
5
|
+
export type SortRawFn = (array: InputData[], sorting: Sorting) => InputData[];
|
6
|
+
export type Heapify = (array: InputData[], len: number, i: number, sorting: Sorting) => void;
|
package/src/types.d.ts
CHANGED
package/src/add/utils.d.ts
DELETED
@@ -1,5 +0,0 @@
|
|
1
|
-
import type { InputData } from "../types.d.ts";
|
2
|
-
import type { Addition } from "./types.d.ts";
|
3
|
-
/** This function adds 2 numbers (as array). */
|
4
|
-
export declare const addition: Addition;
|
5
|
-
export declare const addRoute: (input: InputData[], initValue: InputData) => InputData;
|
package/src/div/types.d.ts
DELETED
package/src/div/utils.d.ts
DELETED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|