@inglorious/utils 2.3.0 → 3.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 (45) hide show
  1. package/README.md +1 -1
  2. package/package.json +1 -1
  3. package/src/algorithms/decision-tree.test.js +7 -6
  4. package/src/algorithms/index.js +4 -0
  5. package/src/algorithms/path-finding.js +2 -2
  6. package/src/data-structures/index.js +10 -0
  7. package/src/functions/index.js +4 -0
  8. package/src/index.js +8 -0
  9. package/src/math/{geometry/circle.js → circle.js} +6 -6
  10. package/src/math/{geometry/circle.test.js → circle.test.js} +23 -22
  11. package/src/math/{geometry/hitmask.test.js → hitmask.test.js} +16 -15
  12. package/src/math/index.js +35 -0
  13. package/src/math/{geometry/line.js → line.js} +4 -6
  14. package/src/math/{geometry/line.test.js → line.test.js} +11 -10
  15. package/src/math/linear-interpolation.js +0 -1
  16. package/src/math/{geometry/point.js → point.js} +5 -5
  17. package/src/math/{geometry/point.test.js → point.test.js} +23 -22
  18. package/src/math/{linear-algebra/quaternion.js → quaternion.js} +4 -4
  19. package/src/math/{linear-algebra/quaternion.test.js → quaternion.test.js} +1 -1
  20. package/src/math/{linear-algebra/quaternions.js → quaternions.js} +7 -4
  21. package/src/math/{linear-algebra/quaternions.test.js → quaternions.test.js} +1 -1
  22. package/src/math/{geometry/rectangle.js → rectangle.js} +3 -5
  23. package/src/math/{geometry/rectangle.test.js → rectangle.test.js} +13 -12
  24. package/src/math/rng.js +0 -2
  25. package/src/math/{geometry/segment.js → segment.js} +5 -9
  26. package/src/math/{geometry/segment.test.js → segment.test.js} +48 -47
  27. package/src/math/{geometry/triangle.js → triangle.js} +1 -1
  28. package/src/math/{geometry/types.d.ts → types.d.ts} +10 -0
  29. package/src/math/{linear-algebra/vector.js → vector.js} +73 -80
  30. package/src/math/{linear-algebra/vector.test.js → vector.test.js} +73 -63
  31. package/src/math/{linear-algebra/vectors.js → vectors.js} +18 -13
  32. package/src/math/{linear-algebra/vectors.test.js → vectors.test.js} +12 -30
  33. package/src/physics/acceleration.js +5 -5
  34. package/src/physics/friction.js +2 -3
  35. package/src/physics/friction.test.js +7 -6
  36. package/src/physics/gravity.js +2 -2
  37. package/src/physics/gravity.test.js +10 -9
  38. package/src/physics/index.js +7 -0
  39. package/src/physics/jump.js +1 -2
  40. package/src/physics/velocity.js +3 -4
  41. package/src/v.js +17 -0
  42. package/src/v.test.js +13 -0
  43. package/src/math/linear-algebra/types.d.ts +0 -5
  44. /package/src/math/{geometry/hitmask.js → hitmask.js} +0 -0
  45. /package/src/math/{geometry/triangle.test.js → triangle.test.js} +0 -0
@@ -1,5 +1,6 @@
1
1
  import { expect, test } from "vitest"
2
2
 
3
+ import { v } from "../v.js"
3
4
  import { applyGravity } from "./gravity.js"
4
5
 
5
6
  test("it should apply gravity based on input parameters", () => {
@@ -8,13 +9,13 @@ test("it should apply gravity based on input parameters", () => {
8
9
  maxLeap: 10,
9
10
  maxSpeed: 10,
10
11
  vy: 10,
11
- position: [0, 10, 0],
12
+ position: v(0, 10, 0),
12
13
  }
13
14
 
14
15
  expect(applyGravity(params)).toStrictEqual({
15
16
  ay: -20,
16
17
  vy: -10,
17
- position: [0, -10, 0],
18
+ position: v(0, -10, 0),
18
19
  })
19
20
  })
20
21
 
@@ -23,13 +24,13 @@ test("it should compute no gravity when max jump height is not set", () => {
23
24
  maxLeap: 10,
24
25
  maxSpeed: 10,
25
26
  vy: 10,
26
- position: [0, 10, 0],
27
+ position: v(0, 10, 0),
27
28
  }
28
29
 
29
30
  expect(applyGravity(params)).toStrictEqual({
30
31
  ay: -0,
31
32
  vy: 10,
32
- position: [0, 20, 0],
33
+ position: v(0, 20, 0),
33
34
  })
34
35
  })
35
36
 
@@ -38,13 +39,13 @@ test("it should compute no gravity when max movement speed is not set", () => {
38
39
  maxJump: 10,
39
40
  maxLeap: 10,
40
41
  vy: 10,
41
- position: [0, 10, 0],
42
+ position: v(0, 10, 0),
42
43
  }
43
44
 
44
45
  expect(applyGravity(params)).toStrictEqual({
45
46
  ay: -0,
46
47
  vy: 10,
47
- position: [0, 20, 0],
48
+ position: v(0, 20, 0),
48
49
  })
49
50
  })
50
51
 
@@ -54,14 +55,14 @@ test("it should apply no gravity when no time has passed", () => {
54
55
  maxLeap: 10,
55
56
  maxSpeed: 10,
56
57
  vy: 10,
57
- position: [0, 10, 0],
58
+ position: v(0, 10, 0),
58
59
  }
59
60
  const dt = 0
60
61
 
61
62
  expect(applyGravity(params, dt)).toStrictEqual({
62
63
  ay: -20,
63
64
  vy: 10,
64
- position: [0, 10, 0],
65
+ position: v(0, 10, 0),
65
66
  })
66
67
  })
67
68
 
@@ -70,7 +71,7 @@ test("it should throw an error when max leap distance is not set", () => {
70
71
  maxJump: 10,
71
72
  maxSpeed: 10,
72
73
  vy: 10,
73
- position: [0, 10, 0],
74
+ position: v(0, 10, 0),
74
75
  }
75
76
 
76
77
  expect(() => applyGravity(params)).toThrow()
@@ -0,0 +1,7 @@
1
+ import * as acceleration from "./acceleration.js"
2
+ import * as friction from "./friction.js"
3
+ import * as gravity from "./gravity.js"
4
+ import * as jump from "./jump.js"
5
+ import * as velocity from "./velocity.js"
6
+
7
+ export { acceleration, friction, gravity, jump, velocity }
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @typedef {import('../math/linear-algebra/types').Vector3} Vector3
2
+ * @typedef {import('../math/types').Vector3} Vector3
3
3
  */
4
4
 
5
5
  /* eslint-disable no-magic-numbers */
@@ -11,7 +11,6 @@ const DEFAULT_MAX_SPEED = 0 // Default maximum speed.
11
11
  /**
12
12
  * Calculates the vertical velocity (vy) of a jump
13
13
  * based on the provided parameters.
14
- *
15
14
  * @param {Object} params - Parameters for the jump calculation.
16
15
  * @param {number} params.maxJump - Maximum jump height. Defaults to 0.
17
16
  * @param {number} params.maxLeap - Maximum leap distance. Defaults to 0.
@@ -1,16 +1,15 @@
1
1
  /**
2
- * @typedef {import('../math/linear-algebra/types').Vector3} Vector3
2
+ * @typedef {import('../math/types.js').Vector3} Vector3
3
3
  */
4
4
 
5
- import { clamp, multiply, zero } from "../math/linear-algebra/vector.js"
6
- import { sum } from "../math/linear-algebra/vectors.js"
5
+ import { clamp, multiply, zero } from "../math/vector.js"
6
+ import { sum } from "../math/vectors.js"
7
7
 
8
8
  const DEFAULT_DT = 1 // Default time delta for the applyVelocity function.
9
9
  const DEFAULT_MAX_SPEED = 0 // Default maximum speed for velocity clamping.
10
10
 
11
11
  /**
12
12
  * Applies velocity to a position based on the given parameters and options.
13
- *
14
13
  * @param {Object} params - The parameters for velocity application.
15
14
  * @param {number} [params.maxSpeed] - The maximum speed for clamping the velocity. Defaults to 0.
16
15
  * @param {Vector3} [params.velocity] - The current velocity vector. Defaults to a zero vector.
package/src/v.js ADDED
@@ -0,0 +1,17 @@
1
+ /**
2
+ * A utility function to create a vector from a list of coordinates.
3
+ * This is a shorthand for `[x, y, z, ...]`.
4
+ * It also tags the array with a non-enumerable `__isVector__` property for
5
+ * efficient type checking.
6
+ * @param {...number} coords - The coordinates of the vector.
7
+ * @returns {Vector} The created vector.
8
+ */
9
+ export function v(...coords) {
10
+ Object.defineProperty(coords, "__isVector__", {
11
+ value: true,
12
+ enumerable: false,
13
+ configurable: false,
14
+ writable: false,
15
+ })
16
+ return coords
17
+ }
package/src/v.test.js ADDED
@@ -0,0 +1,13 @@
1
+ import { expect, test } from "vitest"
2
+
3
+ import { v } from "./v"
4
+
5
+ test("a vector created with the v() function is just a number array", () => {
6
+ const vector = v(1, 2, 3)
7
+ expect(vector).toStrictEqual([1, 2, 3])
8
+ })
9
+
10
+ test("a vector created with the v() function has a __isVector__ property", () => {
11
+ const vector = v(1, 2, 3)
12
+ expect(vector.__isVector__).toBe(true)
13
+ })
@@ -1,5 +0,0 @@
1
- export type Vector = number[]
2
- export type Vector2 = [number, number]
3
- export type Vector3 = [number, number, number]
4
- export type Quaternion = [number, number, number, number]
5
- export type Vector7 = [number, number, number, number, number, number, number]
File without changes