smath 0.0.4 → 1.0.2

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,5 +1,49 @@
1
- # smath
2
1
  Small math function library
3
2
 
4
3
  ![NPM Version](https://img.shields.io/npm/v/smath)
5
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 CHANGED
@@ -6,7 +6,7 @@ var Polate = (function () {
6
6
  }
7
7
  Polate.normalize = function (n, min, max) {
8
8
  if (min === max) {
9
- return min;
9
+ return 0;
10
10
  }
11
11
  return (n - min) / (max - min);
12
12
  };
package/dist/SMath.js CHANGED
@@ -5,7 +5,7 @@ var SMath = (function () {
5
5
  function SMath() {
6
6
  }
7
7
  SMath.isNumber = function (n) {
8
- return typeof n === 'number';
8
+ return typeof n === 'number' && n !== Infinity && n !== -Infinity;
9
9
  };
10
10
  SMath.clamp = function (n, min, max) {
11
11
  if (n < min) {
@@ -20,6 +20,9 @@ var SMath = (function () {
20
20
  if (epsilon === void 0) { epsilon = 1e-6; }
21
21
  return a - b < epsilon && b - a < epsilon;
22
22
  };
23
+ SMath.round = function (n, d) {
24
+ return Math.round(n * Math.pow(10, d)) / (Math.pow(10, d));
25
+ };
23
26
  return SMath;
24
27
  }());
25
28
  exports.SMath = SMath;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "smath",
3
- "version": "0.0.4",
3
+ "version": "1.0.2",
4
4
  "description": "Small math function library",
5
5
  "main": "dist/index.js",
6
6
  "types": "types/index.d.ts",
@@ -14,7 +14,7 @@
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
20
  "keywords": [
@@ -46,7 +46,7 @@
46
46
  "homepage": "https://npm.nicfv.com/smath",
47
47
  "license": "MIT",
48
48
  "devDependencies": {
49
- "typedoc": "^0.25.9",
49
+ "typedoc": "^0.25.10",
50
50
  "typescript": "^5.3.3"
51
51
  }
52
52
  }
package/types/Polate.d.ts CHANGED
@@ -1,13 +1,17 @@
1
1
  /**
2
2
  * Contains useful interpolation and extrapolation functions.
3
3
  */
4
- export declare class Polate {
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 Polate {
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 Polate {
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
  }
package/types/SMath.d.ts CHANGED
@@ -1,11 +1,16 @@
1
1
  /**
2
- * Small Math function library
2
+ * Contains a small math function library.
3
3
  */
4
- export declare class SMath {
4
+ export declare abstract class SMath {
5
5
  /**
6
6
  * Determine if a value is numeric.
7
7
  * @param n Any value to check
8
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
+ * ```
9
14
  */
10
15
  static isNumber(n: any): boolean;
11
16
  /**
@@ -14,6 +19,11 @@ export declare class SMath {
14
19
  * @param min The minimum value of the range
15
20
  * @param max The maximum value of the range
16
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
+ * ```
17
27
  */
18
28
  static clamp(n: number, min: number, max: number): number;
19
29
  /**
@@ -22,6 +32,22 @@ export declare class SMath {
22
32
  * @param b Any number
23
33
  * @param epsilon Maximum absolute error
24
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
+ * ```
25
40
  */
26
41
  static approx(a: number, b: number, epsilon?: number): boolean;
42
+ /**
43
+ * Round a number to any number of decimal places.
44
+ * @param n The number to round
45
+ * @param d The number of places on the right side of the decimal
46
+ * @returns The rounded number
47
+ * @example
48
+ * ```js
49
+ * const pi = SMath.round(3.1416, 2); // 3.14
50
+ * ```
51
+ */
52
+ static round(n: number, d: number): number;
27
53
  }
package/types/index.d.ts CHANGED
@@ -1,2 +1,8 @@
1
+ /**
2
+ * @packageDocumentation
3
+ * Small math function library
4
+ *
5
+ * Exports the public-facing API for `smath`
6
+ */
1
7
  export * from './Polate';
2
8
  export * from './SMath';