smath 0.0.3 → 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.
package/README.md CHANGED
@@ -1,2 +1,49 @@
1
- # smath
2
- Small math function library
1
+ Small math function library
2
+
3
+ ![NPM Version](https://img.shields.io/npm/v/smath)
4
+ ![NPM Downloads](https://img.shields.io/npm/dt/smath)
5
+
6
+ ## Installation
7
+
8
+ `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.
9
+
10
+ ```shell
11
+ npm i smath@latest
12
+ ```
13
+
14
+ ## Getting Started
15
+
16
+ **Example:** A temperature conversion tool.
17
+
18
+ ```js
19
+ import { SMath, Polate } from 'smath';
20
+
21
+ // Define some constants to
22
+ // define the number ranges
23
+ const C_Freeze = 0,
24
+ C_Boil = 100,
25
+ F_Freeze = 32,
26
+ F_Boil = 212;
27
+
28
+ // Use the `Polate` class to
29
+ // translate the temperature in the
30
+ // C number range to a temperature
31
+ // in the F number range
32
+ const C = 20,
33
+ F_expected = 68,
34
+ F_actual = Polate.translate(C, C_Freeze, C_Boil, F_Freeze, F_Boil);
35
+
36
+ // Use the `SMath` class to
37
+ // determine whether the number
38
+ // translation is valid
39
+ const valid = SMath.approx(F_expected, F_actual);
40
+ if (!valid) {
41
+ throw 'Invalid result.';
42
+ }
43
+ ```
44
+
45
+ Visit the [official documentation](https://npm.nicfv.com/smath/) to learn more!
46
+
47
+ ## Contribute
48
+
49
+ `smath` is an open source software package hosted on a [GitHub repository](https://github.com/nicfv/npm). Bug reports and feature requests can be submitted in [issues](https://github.com/nicfv/npm/issues). Contributions are also accepted by submitting a [pull request](https://github.com/nicfv/npm/pulls). Please follow the code styling if submitting a pull request.
package/dist/Polate.js ADDED
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Polate = void 0;
4
+ var Polate = (function () {
5
+ function Polate() {
6
+ }
7
+ Polate.normalize = function (n, min, max) {
8
+ if (min === max) {
9
+ return min;
10
+ }
11
+ return (n - min) / (max - min);
12
+ };
13
+ Polate.expand = function (n, min, max) {
14
+ return (max - min) * n + min;
15
+ };
16
+ Polate.translate = function (n, min1, max1, min2, max2) {
17
+ return this.expand(this.normalize(n, min1, max1), min2, max2);
18
+ };
19
+ return Polate;
20
+ }());
21
+ exports.Polate = Polate;
package/dist/SMath.js ADDED
@@ -0,0 +1,25 @@
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.isNumber = function (n) {
8
+ return typeof n === 'number';
9
+ };
10
+ SMath.clamp = function (n, min, max) {
11
+ if (n < min) {
12
+ return min;
13
+ }
14
+ if (n > max) {
15
+ return max;
16
+ }
17
+ return n;
18
+ };
19
+ SMath.approx = function (a, b, epsilon) {
20
+ if (epsilon === void 0) { epsilon = 1e-6; }
21
+ return a - b < epsilon && b - a < epsilon;
22
+ };
23
+ return SMath;
24
+ }());
25
+ exports.SMath = SMath;
package/dist/index.js CHANGED
@@ -14,4 +14,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./Interpolator"), exports);
17
+ __exportStar(require("./Polate"), exports);
18
+ __exportStar(require("./SMath"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "smath",
3
- "version": "0.0.3",
3
+ "version": "1.0.1",
4
4
  "description": "Small math function library",
5
5
  "main": "dist/index.js",
6
6
  "types": "types/index.d.ts",
@@ -14,15 +14,30 @@
14
14
  "types": "rm -rf types && tsc --declaration --declarationDir types --emitDeclarationOnly",
15
15
  "test": "tsc --noEmit",
16
16
  "clean": "rm -rf node_modules package-lock.json dist types docs",
17
- "docs": "rm -rf docs && typedoc src",
17
+ "docs": "rm -rf docs && typedoc --includeVersion --disableSources --hideGenerator src",
18
18
  "prepublishOnly": "npm run build && npm run types"
19
19
  },
20
- "keywords": [],
20
+ "keywords": [
21
+ "small",
22
+ "math",
23
+ "function",
24
+ "library",
25
+ "simple",
26
+ "number",
27
+ "interpolate",
28
+ "interpolation",
29
+ "extrapolate",
30
+ "extrapolation"
31
+ ],
21
32
  "author": {
22
33
  "name": "Nicolas Ventura",
23
34
  "email": "smath@nicolasventura.com",
24
35
  "url": "https://nicolasventura.com/"
25
36
  },
37
+ "funding": {
38
+ "type": "paypal",
39
+ "url": "https://www.paypal.com/donate/?business=UM6EEKPW8GXA2&no_recurring=0&item_name=Open+source+development&currency_code=USD"
40
+ },
26
41
  "repository": {
27
42
  "type": "git",
28
43
  "url": "https://github.com/nicfv/npm",
@@ -31,7 +46,7 @@
31
46
  "homepage": "https://npm.nicfv.com/smath",
32
47
  "license": "MIT",
33
48
  "devDependencies": {
34
- "typedoc": "^0.25.9",
49
+ "typedoc": "^0.25.10",
35
50
  "typescript": "^5.3.3"
36
51
  }
37
52
  }
@@ -1,13 +1,17 @@
1
1
  /**
2
- * Contains useful interpolation functions.
2
+ * Contains useful interpolation and extrapolation functions.
3
3
  */
4
- export declare class Interpolator {
4
+ export declare abstract class Polate {
5
5
  /**
6
6
  * Normalize the number `n` from the range `min, max` to the range `0, 1`
7
7
  * @param n The number to normalize
8
8
  * @param min The minimum value in the range
9
9
  * @param max The maximum value in the range
10
10
  * @returns A normalized value
11
+ * @example
12
+ * ```js
13
+ * const y = Polate.normalize(18, 9, 99); // 0.1
14
+ * ```
11
15
  */
12
16
  static normalize(n: number, min: number, max: number): number;
13
17
  /**
@@ -16,6 +20,10 @@ export declare class Interpolator {
16
20
  * @param min The minimum value in the range
17
21
  * @param max The maximum value in the range
18
22
  * @returns A value within the number range
23
+ * @example
24
+ * ```js
25
+ * const y = Polate.expand(0.25, 4, 6); // 4.5
26
+ * ```
19
27
  */
20
28
  static expand(n: number, min: number, max: number): number;
21
29
  /**
@@ -26,6 +34,11 @@ export declare class Interpolator {
26
34
  * @param min2 The minimum value for the final range
27
35
  * @param max2 The maximum value for the final range
28
36
  * @returns A translated number in the final range
37
+ * @example
38
+ * ```js
39
+ * const C = 20,
40
+ * F = Polate.translate(C, 0, 100, 32, 212); // 68
41
+ * ```
29
42
  */
30
43
  static translate(n: number, min1: number, max1: number, min2: number, max2: number): number;
31
44
  }
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Contains a small math function library.
3
+ */
4
+ export declare abstract class SMath {
5
+ /**
6
+ * Determine if a value is numeric.
7
+ * @param n Any value to check
8
+ * @returns True if `n` is a number
9
+ * @example
10
+ * ```js
11
+ * const b1 = SMath.isNumber(2), // true
12
+ * b2 = SMath.isNumber('2'); // false
13
+ * ```
14
+ */
15
+ static isNumber(n: any): boolean;
16
+ /**
17
+ * Clamp a number within a range.
18
+ * @param n The number to clamp
19
+ * @param min The minimum value of the range
20
+ * @param max The maximum value of the range
21
+ * @returns A clamped number
22
+ * @example
23
+ * ```js
24
+ * const n1 = SMath.clamp(5, 0, 10), // 5
25
+ * n2 = SMath.clamp(-2, 0, 10); // 0
26
+ * ```
27
+ */
28
+ static clamp(n: number, min: number, max: number): number;
29
+ /**
30
+ * Check if two numbers are approximately equal with a maximum abolute error.
31
+ * @param a Any number
32
+ * @param b Any number
33
+ * @param epsilon Maximum absolute error
34
+ * @returns True if `a` is approximately `b`
35
+ * @example
36
+ * ```js
37
+ * const b1 = SMath.approx(1 / 3, 0.33, 1e-6), // false
38
+ * b2 = SMath.approx(1 / 3, 0.33, 1e-2); // true
39
+ * ```
40
+ */
41
+ static approx(a: number, b: number, epsilon?: number): boolean;
42
+ }
package/types/index.d.ts CHANGED
@@ -1 +1,8 @@
1
- export * from './Interpolator';
1
+ /**
2
+ * @packageDocumentation
3
+ * Small math function library
4
+ *
5
+ * Exports the public-facing API for `smath`
6
+ */
7
+ export * from './Polate';
8
+ export * from './SMath';
@@ -1,21 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Interpolator = void 0;
4
- var Interpolator = (function () {
5
- function Interpolator() {
6
- }
7
- Interpolator.normalize = function (n, min, max) {
8
- if (min === max) {
9
- return min;
10
- }
11
- return (n - min) / (max - min);
12
- };
13
- Interpolator.expand = function (n, min, max) {
14
- return (max - min) * n + min;
15
- };
16
- Interpolator.translate = function (n, min1, max1, min2, max2) {
17
- return this.expand(this.normalize(n, min1, max1), min2, max2);
18
- };
19
- return Interpolator;
20
- }());
21
- exports.Interpolator = Interpolator;