smath 1.1.7 → 1.2.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
@@ -1,10 +1,8 @@
1
- <script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
2
-
3
- [Docs](https://npm.nicfv.com/smath/) | [GitHub](https://github.com/nicfv/npm/tree/main/smath/) | [npm](https://www.npmjs.com/package/smath) | [Changelog](https://github.com/nicfv/npm/blob/main/smath//CHANGELOG.md) | Small math function library
1
+ [Home](/) | [Docs](https://npm.nicfv.com/smath/) | [GitHub](https://github.com/nicfv/npm/tree/main/smath/) | [npm](https://www.npmjs.com/package/smath) | [Changelog](https://github.com/nicfv/npm/blob/main/smath//CHANGELOG.md) | [YouTube](https://www.youtube.com/@nciv) | Small math function library
4
2
 
5
3
  ![NPM Downloads](https://img.shields.io/npm/dt/smath)
6
4
  ![NPM Version](https://img.shields.io/npm/v/smath)
7
- ![Relative date](https://img.shields.io/date/1710879469)
5
+ ![Relative date](https://img.shields.io/date/1710998183)
8
6
  ![GitHub watchers](https://img.shields.io/github/watchers/nicfv/npm)
9
7
  ![GitHub forks](https://img.shields.io/github/forks/nicfv/npm)
10
8
  ![GitHub Repo stars](https://img.shields.io/github/stars/nicfv/npm)
@@ -14,7 +12,7 @@
14
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.
15
13
 
16
14
  ```shell
17
- npm i smath@1.1.7
15
+ npm i smath@1.2.0
18
16
  ```
19
17
 
20
18
  ## Bugs and Requests
@@ -30,31 +28,63 @@ Similar to JavaScript's builtin [`Math`](https://developer.mozilla.org/en-US/doc
30
28
 
31
29
  ## Example
32
30
 
33
- A temperature conversion tool using [`SMath.translate`](https://npm.nicfv.com/smath/classes/SMath.html#translate) to convert units and [`SMath.approx`](https://npm.nicfv.com/smath/classes/SMath.html#approx) to validate the result. The translation uses freezing and boiling points for 2 unit systems to linearly interpolate between them.
31
+ Here are a few examples written in JavaScript for a quick start into `SMath`.
32
+
33
+ ### JavaScript Math Oddities
34
34
 
35
35
  ```js
36
36
  import { SMath } from 'smath';
37
37
 
38
+ // Determine the value of 0.1 + 0.2 using vanilla JavaScript and SMath
39
+ console.log('0.1 + 0.2 == 0.3 is ' + (0.1 + 0.2 == 0.3));
40
+ console.log('SMath.approx(0.1 + 0.2, 0.3) is ' + SMath.approx(0.1 + 0.2, 0.3));
41
+ ```
42
+
43
+ ```text
44
+ 0.1 + 0.2 == 0.3 is false
45
+ SMath.approx(0.1 + 0.2, 0.3) is true
46
+ ```
47
+
48
+ ### Temperature Conversion
49
+
50
+ A temperature conversion tool using [`SMath.translate`](https://npm.nicfv.com/smath/classes/SMath.html#translate) to convert units. The translation uses freezing and boiling points for 2 unit systems to linearly interpolate between them.
51
+
52
+ ```js
53
+ import { SMath } from 'smath';
54
+
55
+ // Water always freezes at the
56
+ // same temperature, but the
57
+ // units might be different.
38
58
  // Define some constants to
39
- // define the number ranges
59
+ // create two number ranges.
40
60
  const C_Freeze = 0,
41
61
  C_Boil = 100,
42
62
  F_Freeze = 32,
43
63
  F_Boil = 212;
44
64
 
45
65
  // Use the `SMath` class to
46
- // translate the temperature in the
66
+ // generate an array of five
67
+ // linearly spaced temperature
68
+ // values from 0 to 20.
69
+ const C = SMath.linspace(0, 20, 5);
70
+
71
+ // Use the `SMath` class to linearly
72
+ // interpolate the temperature in the
47
73
  // C number range to a temperature
48
- // in the F number range
49
- const C = 20,
50
- F_expected = 68,
51
- F_actual = SMath.translate(C, C_Freeze, C_Boil, F_Freeze, F_Boil);
52
-
53
- // Determine whether the
54
- // temperature conversion
55
- // is valid using `approx`
56
- const valid = SMath.approx(F_expected, F_actual);
57
- if (!valid) {
58
- throw new Error('Invalid result.');
74
+ // in the F number range.
75
+ const F = C.map(c => SMath.translate(c, C_Freeze, C_Boil, F_Freeze, F_Boil));
76
+
77
+ // Print out each temperature
78
+ // in both units of C and F.
79
+ for (let i = 0; i < C.length; i++) {
80
+ console.log(C[i].toFixed() + 'C is ' + F[i].toFixed() + 'F')
59
81
  }
60
82
  ```
83
+
84
+ ```text
85
+ 0C is 32F
86
+ 5C is 41F
87
+ 10C is 50F
88
+ 15C is 59F
89
+ 20C is 68F
90
+ ```
package/dist/bin.js ADDED
@@ -0,0 +1,90 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ var _1 = require(".");
5
+ var args = process.argv.slice(2);
6
+ function N(index, defaultVal) {
7
+ if (defaultVal === void 0) { defaultVal = NaN; }
8
+ if (index >= args.length) {
9
+ if (Number.isFinite(defaultVal)) {
10
+ return defaultVal;
11
+ }
12
+ else {
13
+ console.error('Required argument ' + index + ' is missing!');
14
+ process.exit(1);
15
+ }
16
+ }
17
+ var arg = Number.parseFloat(args[index]);
18
+ if (Number.isFinite(arg)) {
19
+ return arg;
20
+ }
21
+ else if (Number.isFinite(defaultVal)) {
22
+ return defaultVal;
23
+ }
24
+ else {
25
+ console.error('Argument #' + index + ' is ' + arg + ' but a number was expected.');
26
+ process.exit(1);
27
+ }
28
+ }
29
+ if (args.length < 1 || args[0].includes('help')) {
30
+ console.log('Key: <required> [optional]');
31
+ console.log('Arguments:');
32
+ console.log(' help : Show this page');
33
+ console.log(' approx <a> <b> [eps] : Check if `a` and `b` are approximately equal');
34
+ console.log(' avg <c0> [c1] ... [cn] : Take an average of `n` numbers');
35
+ console.log(' clamp <n> <min> <max> : Clamp `n` between `min` and `max`');
36
+ console.log(' expand <n> <min> <max> : Expand normalized `n` between `min` and `max`');
37
+ console.log(' linspace <min> <max> <n> : Generate `n` linearly spaced numbers between `min` and `max`');
38
+ console.log(' logspace <min> <max> <n> : Generate `n` logarithmically spaced numbers between `min` and `max`');
39
+ console.log(' normalize <n> <min> <max>');
40
+ console.log(' : Normalize `n` between `min` and `max`');
41
+ console.log(' translate <n> <min1> <max1> <min2> <max2>');
42
+ console.log(' : Linearly interpolate `n` from `min1`, `max1` to `min2`, `max2`');
43
+ process.exit(1);
44
+ }
45
+ switch (args[0]) {
46
+ case ('approx'): {
47
+ console.log(_1.SMath.approx(N(1), N(2), N(3, 1e-6)));
48
+ break;
49
+ }
50
+ case ('avg'): {
51
+ if (args.length < 2) {
52
+ console.error('Need at least 1 argument.');
53
+ process.exit(1);
54
+ }
55
+ var operands = [];
56
+ for (var i = 1; i < args.length; i++) {
57
+ operands.push(N(i));
58
+ }
59
+ console.log(_1.SMath.avg.apply(_1.SMath, operands));
60
+ break;
61
+ }
62
+ case ('clamp'): {
63
+ console.log(_1.SMath.clamp(N(1), N(2), N(3)));
64
+ break;
65
+ }
66
+ case ('expand'): {
67
+ console.log(_1.SMath.expand(N(1), N(2), N(3)));
68
+ break;
69
+ }
70
+ case ('linspace'): {
71
+ console.log(_1.SMath.linspace(N(1), N(2), N(3)));
72
+ break;
73
+ }
74
+ case ('logspace'): {
75
+ console.log(_1.SMath.logspace(N(1), N(2), N(3)));
76
+ break;
77
+ }
78
+ case ('normalize'): {
79
+ console.log(_1.SMath.normalize(N(1), N(2), N(3)));
80
+ break;
81
+ }
82
+ case ('translate'): {
83
+ console.log(_1.SMath.translate(N(1), N(2), N(3), N(4), N(5)));
84
+ break;
85
+ }
86
+ default: {
87
+ console.error('Unknown argument "' + args[0] + '". Use with "help" for a list of commands.');
88
+ process.exit(1);
89
+ }
90
+ }
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "smath",
3
- "version": "1.1.7",
3
+ "version": "1.2.0",
4
4
  "description": "Small math function library",
5
5
  "homepage": "https://npm.nicfv.com/smath",
6
+ "bin": "dist/bin.js",
6
7
  "main": "dist/index.js",
7
8
  "types": "types/index.d.ts",
8
9
  "files": [
@@ -15,7 +16,8 @@
15
16
  "test": "tsc --noEmit",
16
17
  "clean": "rm -rf node_modules package-lock.json dist types docs",
17
18
  "docs": "rm -rf docs && typedoc --includeVersion --disableSources --hideGenerator src",
18
- "prepublishOnly": "npm run build && npm run types"
19
+ "prepack": "npm run build && npm run types",
20
+ "postpack": "rm -rf dist types"
19
21
  },
20
22
  "keywords": [
21
23
  "small",
@@ -46,7 +48,8 @@
46
48
  "repository": "github:nicfv/npm",
47
49
  "license": "MIT",
48
50
  "devDependencies": {
51
+ "@types/node": "20.11.30",
49
52
  "typedoc": "0.25.12",
50
53
  "typescript": "5.4.2"
51
54
  }
52
- }
55
+ }
package/types/bin.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};