easyjs-maths 0.1.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.
Files changed (3) hide show
  1. package/README.md +26 -0
  2. package/package.json +16 -0
  3. package/src/index.js +82 -0
package/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # easyjs-maths
2
+
3
+ Maths plugin for EasyJS. Basic loads by `easy.use(maths)`. Advanced loads by `easy.use(maths.advanced)`.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm i easyjs-maths
9
+ ```
10
+
11
+ ## Use
12
+
13
+ ```js
14
+ const easy = require("easyjs-empty");
15
+ const maths = require("easyjs-maths");
16
+
17
+ // basic
18
+
19
+ easy.use(maths);
20
+
21
+ // advanced
22
+
23
+ easy.use(maths.advanced);
24
+ ```
25
+
26
+ Refer to the `easyjs-empty` Readme for more info.
package/package.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "name": "easyjs-maths",
3
+ "version": "0.1.0",
4
+ "description": "EasyJS maths plugin (Python-style math/random helpers).",
5
+ "license": "ISC",
6
+ "type": "commonjs",
7
+ "main": "src/index.js",
8
+ "keywords": [
9
+ "easyjs",
10
+ "plugin",
11
+ "math",
12
+ "maths",
13
+ "random",
14
+ "javascript"
15
+ ]
16
+ }
package/src/index.js ADDED
@@ -0,0 +1,82 @@
1
+ // easyjs-maths
2
+ const createSets = () => {
3
+ // --- basic math functions ---
4
+ const basic = {
5
+ sqrt: (n) => Math.sqrt(n),
6
+ pow: (a, b) => Math.pow(a, b),
7
+ floor: (n) => Math.floor(n),
8
+ ceil: (n) => Math.ceil(n),
9
+ round: (n) => Math.round(n),
10
+ abs: (n) => Math.abs(n),
11
+ min: (...nums) => Math.min(...nums),
12
+ max: (...nums) => Math.max(...nums),
13
+ };
14
+
15
+ // --- advanced math functions ---
16
+ const advanced = {
17
+ factorial: (n) => {
18
+ if (n < 0) throw new Error("factorial only accepts non-negative numbers");
19
+ let res = 1;
20
+ for (let i = 2; i <= n; i++) res *= i;
21
+ return res;
22
+ },
23
+ gcd: (a, b) => { while (b !== 0) [a, b] = [b, a % b]; return a; },
24
+ lcm: (a, b) => (a * b) / (function gcd(a, b){ while (b !== 0) [a, b] = [b, a % b]; return a; })(a, b),
25
+ lerp: (a, b, t) => a + (b - a) * t,
26
+ sin: Math.sin,
27
+ cos: Math.cos,
28
+ tan: Math.tan,
29
+ asin: Math.asin,
30
+ acos: Math.acos,
31
+ atan: Math.atan,
32
+ log: (x, base = Math.E) => Math.log(x) / Math.log(base),
33
+ log10: Math.log10,
34
+ pi: Math.PI,
35
+ e: Math.E,
36
+ };
37
+
38
+ // --- random functions ---
39
+ const randomFuncs = {
40
+ random: Math.random,
41
+ randint: (min, max) => Math.floor(Math.random() * (max - min + 1)) + min,
42
+ choice: (arr) => arr[Math.floor(Math.random() * arr.length)],
43
+ shuffle: (arr) => {
44
+ const copy = [...arr];
45
+ for (let i = copy.length - 1; i > 0; i--) {
46
+ const j = Math.floor(Math.random() * (i + 1));
47
+ [copy[i], copy[j]] = [copy[j], copy[i]];
48
+ }
49
+ return copy;
50
+ },
51
+ uniform: (min, max) => Math.random() * (max - min) + min,
52
+ sample: (arr, n) => {
53
+ const shuffled = [...arr].sort(() => 0.5 - Math.random());
54
+ return shuffled.slice(0, n);
55
+ }
56
+ };
57
+
58
+ // --- helper to add functions with override warnings ---
59
+ const addFuncs = (target, funcs) => {
60
+ for (const [name, fn] of Object.entries(funcs)) {
61
+ if (target[name]) {
62
+ console.log(`-- WARNING - overwriting ${name} - WARNING --`);
63
+ }
64
+ target[name] = fn;
65
+ }
66
+ };
67
+
68
+ return { basic, advanced, randomFuncs, addFuncs };
69
+ };
70
+
71
+ const maths = (easy) => {
72
+ const { basic, addFuncs } = createSets();
73
+ addFuncs(easy, basic);
74
+ };
75
+
76
+ // advanced plugin (function) for easy.use(maths.advanced)
77
+ maths.advanced = (easy) => {
78
+ const { basic, advanced, randomFuncs, addFuncs } = createSets();
79
+ addFuncs(easy, { ...basic, ...advanced, ...randomFuncs });
80
+ };
81
+
82
+ module.exports = maths;