smath 1.2.0 → 1.3.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/README.md +2 -2
- package/dist/bin.js +3 -0
- package/dist/index.js +104 -1
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|

|
|
4
4
|

|
|
5
|
-

|
|
6
6
|

|
|
7
7
|

|
|
8
8
|

|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
smath can be installed from the official [npm package repository](https://www.npmjs.com/package/smath). It is highly recommended to install the latest version, which is installed by default with the following command.
|
|
13
13
|
|
|
14
14
|
```shell
|
|
15
|
-
npm i smath@1.
|
|
15
|
+
npm i smath@1.3.1
|
|
16
16
|
```
|
|
17
17
|
|
|
18
18
|
## Bugs and Requests
|
package/dist/bin.js
CHANGED
|
@@ -3,6 +3,9 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
var _1 = require(".");
|
|
5
5
|
var args = process.argv.slice(2);
|
|
6
|
+
/**
|
|
7
|
+
* Try to convert an argument into a numeric value.
|
|
8
|
+
*/
|
|
6
9
|
function N(index, defaultVal) {
|
|
7
10
|
if (defaultVal === void 0) { defaultVal = NaN; }
|
|
8
11
|
if (index >= args.length) {
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,28 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SMath = void 0;
|
|
4
|
-
|
|
4
|
+
/**
|
|
5
|
+
* @packageDocumentation
|
|
6
|
+
* Small math function library
|
|
7
|
+
*
|
|
8
|
+
* Exports the public-facing API for `smath`
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Contains a small math function library including
|
|
12
|
+
* useful interpolation and extrapolation functions.
|
|
13
|
+
*/
|
|
14
|
+
var SMath = /** @class */ (function () {
|
|
5
15
|
function SMath() {
|
|
6
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* Compute the average, or mean, of a set of numbers.
|
|
19
|
+
* @param n Any amount of numeric inputs
|
|
20
|
+
* @returns The average, or mean
|
|
21
|
+
* @example
|
|
22
|
+
* ```js
|
|
23
|
+
* const mean = SMath.avg(1, 2, 3, 4); // 2.5
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
7
26
|
SMath.avg = function () {
|
|
8
27
|
var n = [];
|
|
9
28
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
@@ -11,6 +30,18 @@ var SMath = (function () {
|
|
|
11
30
|
}
|
|
12
31
|
return n.reduce(function (prev, curr) { return prev + curr; }) / n.length;
|
|
13
32
|
};
|
|
33
|
+
/**
|
|
34
|
+
* Clamp a number within a range.
|
|
35
|
+
* @param n The number to clamp
|
|
36
|
+
* @param min The minimum value of the range
|
|
37
|
+
* @param max The maximum value of the range
|
|
38
|
+
* @returns A clamped number
|
|
39
|
+
* @example
|
|
40
|
+
* ```js
|
|
41
|
+
* const n1 = SMath.clamp(5, 0, 10), // 5
|
|
42
|
+
* n2 = SMath.clamp(-2, 0, 10); // 0
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
14
45
|
SMath.clamp = function (n, min, max) {
|
|
15
46
|
if (n < min) {
|
|
16
47
|
return min;
|
|
@@ -20,22 +51,82 @@ var SMath = (function () {
|
|
|
20
51
|
}
|
|
21
52
|
return n;
|
|
22
53
|
};
|
|
54
|
+
/**
|
|
55
|
+
* Check if two numbers are approximately equal with a maximum abolute error.
|
|
56
|
+
* @param a Any number
|
|
57
|
+
* @param b Any number
|
|
58
|
+
* @param epsilon Maximum absolute error
|
|
59
|
+
* @returns True if `a` is approximately `b`
|
|
60
|
+
* @example
|
|
61
|
+
* ```js
|
|
62
|
+
* const b1 = SMath.approx(1 / 3, 0.33, 1e-6), // false
|
|
63
|
+
* b2 = SMath.approx(1 / 3, 0.33, 1e-2); // true
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
23
66
|
SMath.approx = function (a, b, epsilon) {
|
|
24
67
|
if (epsilon === void 0) { epsilon = 1e-6; }
|
|
25
68
|
return a - b < epsilon && b - a < epsilon;
|
|
26
69
|
};
|
|
70
|
+
/**
|
|
71
|
+
* Normalize the number `n` from the range `min, max` to the range `0, 1`
|
|
72
|
+
* @param n The number to normalize
|
|
73
|
+
* @param min The minimum value in the range
|
|
74
|
+
* @param max The maximum value in the range
|
|
75
|
+
* @returns A normalized value
|
|
76
|
+
* @example
|
|
77
|
+
* ```js
|
|
78
|
+
* const y = SMath.normalize(18, 9, 99); // 0.1
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
27
81
|
SMath.normalize = function (n, min, max) {
|
|
28
82
|
if (min === max) {
|
|
29
83
|
return 0;
|
|
30
84
|
}
|
|
31
85
|
return (n - min) / (max - min);
|
|
32
86
|
};
|
|
87
|
+
/**
|
|
88
|
+
* Expand a normalized number `n` to the range `min, max`
|
|
89
|
+
* @param n A normalized number
|
|
90
|
+
* @param min The minimum value in the range
|
|
91
|
+
* @param max The maximum value in the range
|
|
92
|
+
* @returns A value within the number range
|
|
93
|
+
* @example
|
|
94
|
+
* ```js
|
|
95
|
+
* const y = SMath.expand(0.25, 4, 6); // 4.5
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
33
98
|
SMath.expand = function (n, min, max) {
|
|
34
99
|
return (max - min) * n + min;
|
|
35
100
|
};
|
|
101
|
+
/**
|
|
102
|
+
* Translate a number `n` from the range `min1, max1` to the range `min2, max2`
|
|
103
|
+
* @param n The number to translate
|
|
104
|
+
* @param min1 The minimum value from the initial range
|
|
105
|
+
* @param max1 The maximum value from the initial range
|
|
106
|
+
* @param min2 The minimum value for the final range
|
|
107
|
+
* @param max2 The maximum value for the final range
|
|
108
|
+
* @returns A translated number in the final range
|
|
109
|
+
* @example
|
|
110
|
+
* ```js
|
|
111
|
+
* const C = 20,
|
|
112
|
+
* F = SMath.translate(C, 0, 100, 32, 212); // 68
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
36
115
|
SMath.translate = function (n, min1, max1, min2, max2) {
|
|
37
116
|
return this.expand(this.normalize(n, min1, max1), min2, max2);
|
|
38
117
|
};
|
|
118
|
+
/**
|
|
119
|
+
* Generate an array of linearly spaced numbers.
|
|
120
|
+
* @param min The initial value of the linear space
|
|
121
|
+
* @param max The final value of the linear space
|
|
122
|
+
* @param count The number of values in the space
|
|
123
|
+
* @returns The linear space as an array of numbers
|
|
124
|
+
* @example
|
|
125
|
+
* ```js
|
|
126
|
+
* const space = SMath.linspace(1, 5, 6);
|
|
127
|
+
* // [ 1, 1.8, 2.6, 3.4, 4.2, 5 ]
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
39
130
|
SMath.linspace = function (min, max, count) {
|
|
40
131
|
var space = [];
|
|
41
132
|
for (var i = 0; i < count; i++) {
|
|
@@ -43,6 +134,18 @@ var SMath = (function () {
|
|
|
43
134
|
}
|
|
44
135
|
return space;
|
|
45
136
|
};
|
|
137
|
+
/**
|
|
138
|
+
* Generate an array of logarithmically spaced numbers.
|
|
139
|
+
* @param min The initial magnitude of the space
|
|
140
|
+
* @param max The final magnitude of the space
|
|
141
|
+
* @param count The number of values in the space
|
|
142
|
+
* @returns The logarithmic space as an array of numbers
|
|
143
|
+
* @example
|
|
144
|
+
* ```js
|
|
145
|
+
* const space = SMath.logspace(0, 2, 5);
|
|
146
|
+
* // [ 1, 3.2, 10, 31.6, 100 ]
|
|
147
|
+
* ```
|
|
148
|
+
*/
|
|
46
149
|
SMath.logspace = function (min, max, count) {
|
|
47
150
|
return this.linspace(min, max, count).map(function (n) { return Math.pow(10, n); });
|
|
48
151
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "smath",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.1",
|
|
4
4
|
"description": "Small math function library",
|
|
5
5
|
"homepage": "https://npm.nicfv.com/smath",
|
|
6
6
|
"bin": "dist/bin.js",
|
|
@@ -11,12 +11,11 @@
|
|
|
11
11
|
"types"
|
|
12
12
|
],
|
|
13
13
|
"scripts": {
|
|
14
|
-
"build": "rm -rf dist && tsc
|
|
15
|
-
"
|
|
16
|
-
"test": "tsc --noEmit",
|
|
14
|
+
"build": "rm -rf dist && tsc",
|
|
15
|
+
"test": "tsc --outDir tmp test/index.ts && { node tmp/test/index.js ; rm -r tmp ; }",
|
|
17
16
|
"clean": "rm -rf node_modules package-lock.json dist types docs",
|
|
18
17
|
"docs": "rm -rf docs && typedoc --includeVersion --disableSources --hideGenerator src",
|
|
19
|
-
"prepack": "npm run build
|
|
18
|
+
"prepack": "npm run build",
|
|
20
19
|
"postpack": "rm -rf dist types"
|
|
21
20
|
},
|
|
22
21
|
"keywords": [
|
|
@@ -50,6 +49,7 @@
|
|
|
50
49
|
"devDependencies": {
|
|
51
50
|
"@types/node": "20.11.30",
|
|
52
51
|
"typedoc": "0.25.12",
|
|
53
|
-
"typescript": "5.4.
|
|
52
|
+
"typescript": "5.4.3",
|
|
53
|
+
"xpt": "0.2.0"
|
|
54
54
|
}
|
|
55
|
-
}
|
|
55
|
+
}
|