smath 1.2.0 → 1.3.0

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 CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  ![NPM Downloads](https://img.shields.io/npm/dt/smath)
4
4
  ![NPM Version](https://img.shields.io/npm/v/smath)
5
- ![Relative date](https://img.shields.io/date/1710998183)
5
+ ![Relative date](https://img.shields.io/date/1711134970)
6
6
  ![GitHub watchers](https://img.shields.io/github/watchers/nicfv/npm)
7
7
  ![GitHub forks](https://img.shields.io/github/forks/nicfv/npm)
8
8
  ![GitHub Repo stars](https://img.shields.io/github/stars/nicfv/npm)
@@ -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.2.0
15
+ npm i smath@1.3.0
16
16
  ```
17
17
 
18
18
  ## Bugs and Requests
@@ -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) {
@@ -0,0 +1,154 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SMath = void 0;
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 () {
15
+ function SMath() {
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
+ */
26
+ SMath.avg = function () {
27
+ var n = [];
28
+ for (var _i = 0; _i < arguments.length; _i++) {
29
+ n[_i] = arguments[_i];
30
+ }
31
+ return n.reduce(function (prev, curr) { return prev + curr; }) / n.length;
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
+ */
45
+ SMath.clamp = function (n, min, max) {
46
+ if (n < min) {
47
+ return min;
48
+ }
49
+ if (n > max) {
50
+ return max;
51
+ }
52
+ return n;
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
+ */
66
+ SMath.approx = function (a, b, epsilon) {
67
+ if (epsilon === void 0) { epsilon = 1e-6; }
68
+ return a - b < epsilon && b - a < epsilon;
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
+ */
81
+ SMath.normalize = function (n, min, max) {
82
+ if (min === max) {
83
+ return 0;
84
+ }
85
+ return (n - min) / (max - min);
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
+ */
98
+ SMath.expand = function (n, min, max) {
99
+ return (max - min) * n + min;
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
+ */
115
+ SMath.translate = function (n, min1, max1, min2, max2) {
116
+ return this.expand(this.normalize(n, min1, max1), min2, max2);
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
+ */
130
+ SMath.linspace = function (min, max, count) {
131
+ var space = [];
132
+ for (var i = 0; i < count; i++) {
133
+ space[i] = this.translate(i, 0, count - 1, min, max);
134
+ }
135
+ return space;
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
+ */
149
+ SMath.logspace = function (min, max, count) {
150
+ return this.linspace(min, max, count).map(function (n) { return Math.pow(10, n); });
151
+ };
152
+ return SMath;
153
+ }());
154
+ exports.SMath = SMath;
package/package.json CHANGED
@@ -1,22 +1,21 @@
1
1
  {
2
2
  "name": "smath",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Small math function library",
5
5
  "homepage": "https://npm.nicfv.com/smath",
6
- "bin": "dist/bin.js",
7
- "main": "dist/index.js",
8
- "types": "types/index.d.ts",
6
+ "bin": "dist/src/bin.js",
7
+ "main": "dist/src/index.js",
8
+ "types": "types/src/index.d.ts",
9
9
  "files": [
10
- "dist",
11
- "types"
10
+ "dist/src",
11
+ "types/src"
12
12
  ],
13
13
  "scripts": {
14
- "build": "rm -rf dist && tsc --removeComments",
15
- "types": "rm -rf types && tsc --declaration --declarationDir types --emitDeclarationOnly",
16
- "test": "tsc --noEmit",
14
+ "build": "rm -rf dist && tsc",
15
+ "test": "npm run build && node dist/test/index.js",
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 && npm run types",
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.2"
52
+ "typescript": "5.4.3",
53
+ "xpt": "0.2.0"
54
54
  }
55
- }
55
+ }
package/dist/index.js DELETED
@@ -1,51 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SMath = void 0;
4
- var SMath = (function () {
5
- function SMath() {
6
- }
7
- SMath.avg = function () {
8
- var n = [];
9
- for (var _i = 0; _i < arguments.length; _i++) {
10
- n[_i] = arguments[_i];
11
- }
12
- return n.reduce(function (prev, curr) { return prev + curr; }) / n.length;
13
- };
14
- SMath.clamp = function (n, min, max) {
15
- if (n < min) {
16
- return min;
17
- }
18
- if (n > max) {
19
- return max;
20
- }
21
- return n;
22
- };
23
- SMath.approx = function (a, b, epsilon) {
24
- if (epsilon === void 0) { epsilon = 1e-6; }
25
- return a - b < epsilon && b - a < epsilon;
26
- };
27
- SMath.normalize = function (n, min, max) {
28
- if (min === max) {
29
- return 0;
30
- }
31
- return (n - min) / (max - min);
32
- };
33
- SMath.expand = function (n, min, max) {
34
- return (max - min) * n + min;
35
- };
36
- SMath.translate = function (n, min1, max1, min2, max2) {
37
- return this.expand(this.normalize(n, min1, max1), min2, max2);
38
- };
39
- SMath.linspace = function (min, max, count) {
40
- var space = [];
41
- for (var i = 0; i < count; i++) {
42
- space[i] = this.translate(i, 0, count - 1, min, max);
43
- }
44
- return space;
45
- };
46
- SMath.logspace = function (min, max, count) {
47
- return this.linspace(min, max, count).map(function (n) { return Math.pow(10, n); });
48
- };
49
- return SMath;
50
- }());
51
- exports.SMath = SMath;
File without changes
File without changes