@inglorious/utils 2.3.0 → 3.0.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 (42) 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 -4
  14. package/src/math/{geometry/line.test.js → line.test.js} +11 -10
  15. package/src/math/{geometry/point.js → point.js} +5 -5
  16. package/src/math/{geometry/point.test.js → point.test.js} +23 -22
  17. package/src/math/{linear-algebra/quaternion.js → quaternion.js} +3 -2
  18. package/src/math/{linear-algebra/quaternion.test.js → quaternion.test.js} +1 -1
  19. package/src/math/{linear-algebra/quaternions.test.js → quaternions.test.js} +1 -1
  20. package/src/math/{geometry/rectangle.js → rectangle.js} +3 -3
  21. package/src/math/{geometry/rectangle.test.js → rectangle.test.js} +13 -12
  22. package/src/math/{geometry/segment.js → segment.js} +5 -5
  23. package/src/math/{geometry/segment.test.js → segment.test.js} +48 -47
  24. package/src/math/{geometry/types.d.ts → types.d.ts} +10 -0
  25. package/src/math/{linear-algebra/vector.js → vector.js} +23 -32
  26. package/src/math/{linear-algebra/vector.test.js → vector.test.js} +42 -35
  27. package/src/math/{linear-algebra/vectors.test.js → vectors.test.js} +12 -30
  28. package/src/physics/acceleration.js +3 -3
  29. package/src/physics/friction.js +2 -2
  30. package/src/physics/friction.test.js +7 -6
  31. package/src/physics/gravity.js +2 -2
  32. package/src/physics/gravity.test.js +10 -9
  33. package/src/physics/index.js +7 -0
  34. package/src/physics/jump.js +1 -1
  35. package/src/physics/velocity.js +3 -3
  36. package/src/v.js +18 -0
  37. package/src/math/linear-algebra/types.d.ts +0 -5
  38. /package/src/math/{geometry/hitmask.js → hitmask.js} +0 -0
  39. /package/src/math/{linear-algebra/quaternions.js → quaternions.js} +0 -0
  40. /package/src/math/{geometry/triangle.js → triangle.js} +0 -0
  41. /package/src/math/{geometry/triangle.test.js → triangle.test.js} +0 -0
  42. /package/src/math/{linear-algebra/vectors.js → vectors.js} +0 -0
@@ -1,65 +1,47 @@
1
1
  import { expect, test } from "vitest"
2
2
 
3
+ import { v } from "../v.js"
3
4
  import { cross, distance, dot, subtract, sum } from "./vectors.js"
4
5
 
5
6
  test("it should compute the cross product between two vectors (aka vectorProduct)", () => {
6
- const vector1 = [1, 2, 3]
7
- const vector2 = [4, 5, 6]
8
- const expectedResult = [-3, 6, -3]
7
+ const vector1 = v(1, 2, 3)
8
+ const vector2 = v(4, 5, 6)
9
+ const expectedResult = v(-3, 6, -3)
9
10
 
10
11
  expect(cross(vector1, vector2)).toStrictEqual(expectedResult)
11
12
  })
12
13
 
13
14
  test("it should compute the cross product of multiple vectors (aka vectorProduct)", () => {
14
- const vectors = [
15
- [1, 2, 3],
16
- [4, 5, 6],
17
- [7, 8, 9],
18
- ]
19
- const expectedResult = [78, 6, -66]
15
+ const vectors = [v(1, 2, 3), v(4, 5, 6), v(7, 8, 9)]
16
+ const expectedResult = v(78, 6, -66)
20
17
 
21
18
  expect(cross(...vectors)).toStrictEqual(expectedResult)
22
19
  })
23
20
 
24
21
  test("it should compute the distance between multiple vectors", () => {
25
- const vectors = [
26
- [0, 0, 0],
27
- [4, 0, 3],
28
- ]
22
+ const vectors = [v(0, 0, 0), v(4, 0, 3)]
29
23
  const expectedResult = 5
30
24
 
31
25
  expect(distance(...vectors)).toStrictEqual(expectedResult)
32
26
  })
33
27
 
34
28
  test("it should compute the dot product of multiple vectors (aka scalarProduct)", () => {
35
- const vectors = [
36
- [1, 2, 3],
37
- [4, 5, 6],
38
- [7, 8, 9],
39
- ]
29
+ const vectors = [v(1, 2, 3), v(4, 5, 6), v(7, 8, 9)]
40
30
  const expectedResult = 270
41
31
 
42
32
  expect(dot(...vectors)).toStrictEqual(expectedResult)
43
33
  })
44
34
 
45
35
  test("it should subtract multiple vectors", () => {
46
- const vectors = [
47
- [1, 2, 3],
48
- [4, 5, 6],
49
- [7, 8, 9],
50
- ]
51
- const expectedResult = [-10, -11, -12]
36
+ const vectors = [v(1, 2, 3), v(4, 5, 6), v(7, 8, 9)]
37
+ const expectedResult = v(-10, -11, -12)
52
38
 
53
39
  expect(subtract(...vectors)).toStrictEqual(expectedResult)
54
40
  })
55
41
 
56
42
  test("it should sum multiple vectors (aka add)", () => {
57
- const vectors = [
58
- [1, 2, 3],
59
- [4, 5, 6],
60
- [7, 8, 9],
61
- ]
62
- const expectedResult = [12, 15, 18]
43
+ const vectors = [v(1, 2, 3), v(4, 5, 6), v(7, 8, 9)]
44
+ const expectedResult = v(12, 15, 18)
63
45
 
64
46
  expect(sum(...vectors)).toStrictEqual(expectedResult)
65
47
  })
@@ -1,9 +1,9 @@
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
  import { applyFriction } from "./friction.js"
8
8
 
9
9
  const DEFAULT_DT = 1 // Default time delta for the applyAcceleration function.
@@ -1,8 +1,8 @@
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 { magnitude, setMagnitude, zero } from "../math/linear-algebra/vector.js"
5
+ import { magnitude, setMagnitude, zero } from "../math/vector.js"
6
6
 
7
7
  const DEFAULT_DT = 1 // Default time delta for the applyFriction function.
8
8
  const NO_FRICTION = 0 // No friction constant.
@@ -1,18 +1,19 @@
1
1
  import { expect, test } from "vitest"
2
2
 
3
- import { zero } from "../math/linear-algebra/vector.js"
3
+ import { zero } from "../math/vector.js"
4
+ import { v } from "../v.js"
4
5
  import { applyFriction } from "./friction.js"
5
6
 
6
7
  test("it should apply friction to some velocity", () => {
7
- const velocity = [8, 0, 6]
8
+ const velocity = v(8, 0, 6)
8
9
  const friction = 5
9
- const expectedResult = [4, 0, 3]
10
+ const expectedResult = v(4, 0, 3)
10
11
 
11
12
  expect(applyFriction({ velocity, friction })).toStrictEqual(expectedResult)
12
13
  })
13
14
 
14
15
  test("it should stop movement when friction is equal to velocity", () => {
15
- const velocity = [8, 0, 6]
16
+ const velocity = v(8, 0, 6)
16
17
  const friction = 5
17
18
  const dt = 2
18
19
 
@@ -27,14 +28,14 @@ test("it should apply no friction on no velocity", () => {
27
28
  })
28
29
 
29
30
  test("it should apply no friction if no friction is passed", () => {
30
- const velocity = [8, 0, 6]
31
+ const velocity = v(8, 0, 6)
31
32
  const friction = 0
32
33
 
33
34
  expect(applyFriction({ velocity, friction })).toStrictEqual(velocity)
34
35
  })
35
36
 
36
37
  test("it should apply no friction if no time passed", () => {
37
- const velocity = [8, 0, 6]
38
+ const velocity = v(8, 0, 6)
38
39
  const friction = 5
39
40
  const dt = 0
40
41
 
@@ -1,10 +1,10 @@
1
1
  /**
2
- * @typedef {import('../math/linear-algebra/types').Vector3} Vector3
2
+ * @typedef {import('../math/types.js').Vector3} Vector3
3
3
  */
4
4
 
5
5
  /* eslint-disable no-magic-numbers */
6
6
 
7
- import { zero } from "../math/linear-algebra/vector.js"
7
+ import { zero } from "../math/vector.js"
8
8
 
9
9
  const DEFAULT_DT = 1 // Default delta time for the applyGravity function.
10
10
  const DEFAULT_JUMP = 0 // Default maximum jump height.
@@ -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 */
@@ -1,9 +1,9 @@
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.
package/src/v.js ADDED
@@ -0,0 +1,18 @@
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
+ *
7
+ * @param {...number} coords - The coordinates of the vector.
8
+ * @returns {Vector} The created vector.
9
+ */
10
+ export function v(...coords) {
11
+ Object.defineProperty(coords, "__isVector__", {
12
+ value: true,
13
+ enumerable: false,
14
+ configurable: false,
15
+ writable: false,
16
+ })
17
+ return coords
18
+ }
@@ -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
File without changes