@inglorious/utils 3.7.0 → 3.7.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.
@@ -1,77 +0,0 @@
1
- import { expect, test } from "vitest"
2
-
3
- import { v } from "../v.js"
4
- import {
5
- cross,
6
- distance,
7
- divide,
8
- dot,
9
- multiply,
10
- power,
11
- subtract,
12
- sum,
13
- } from "./vectors.js"
14
-
15
- test("it should compute the cross product between two vectors (aka vectorProduct)", () => {
16
- const vector1 = v(1, 2, 3)
17
- const vector2 = v(4, 5, 6)
18
- const expectedResult = v(-3, 6, -3)
19
-
20
- expect(cross(vector1, vector2)).toStrictEqual(expectedResult)
21
- })
22
-
23
- test("it should compute the cross product of multiple vectors (aka vectorProduct)", () => {
24
- const vectors = [v(1, 2, 3), v(4, 5, 6), v(7, 8, 9)]
25
- const expectedResult = v(78, 6, -66)
26
-
27
- expect(cross(...vectors)).toStrictEqual(expectedResult)
28
- })
29
-
30
- test("it should compute the distance between multiple vectors", () => {
31
- const vectors = [v(0, 0, 0), v(4, 0, 3)]
32
- const expectedResult = 5
33
-
34
- expect(distance(...vectors)).toStrictEqual(expectedResult)
35
- })
36
-
37
- test("it should divide multiple vectors component-wise", () => {
38
- const vectors = [v(80, 40, 20), v(2, 4, 5), v(2, 2, 2)]
39
- const expectedResult = v(20, 5, 2)
40
-
41
- expect(divide(...vectors)).toStrictEqual(expectedResult)
42
- })
43
-
44
- test("it should compute the dot product of multiple vectors (aka scalarProduct)", () => {
45
- const vectors = [v(1, 2, 3), v(4, 5, 6), v(7, 8, 9)]
46
- const expectedResult = 270
47
-
48
- expect(dot(...vectors)).toStrictEqual(expectedResult)
49
- })
50
-
51
- test("it should multiply multiple vectors component-wise (Hadamard product)", () => {
52
- const vectors = [v(1, 2, 3), v(4, 5, 6), v(2, 2, 2)]
53
- const expectedResult = v(8, 20, 36)
54
-
55
- expect(multiply(...vectors)).toStrictEqual(expectedResult)
56
- })
57
-
58
- test("it should raise a vector to the power of another vector", () => {
59
- const vectors = [v(2, 3, 4), v(4, 3, 2)]
60
- const expectedResult = v(16, 27, 16)
61
-
62
- expect(power(...vectors)).toStrictEqual(expectedResult)
63
- })
64
-
65
- test("it should subtract multiple vectors", () => {
66
- const vectors = [v(1, 2, 3), v(4, 5, 6), v(7, 8, 9)]
67
- const expectedResult = v(-10, -11, -12)
68
-
69
- expect(subtract(...vectors)).toStrictEqual(expectedResult)
70
- })
71
-
72
- test("it should sum multiple vectors (aka add)", () => {
73
- const vectors = [v(1, 2, 3), v(4, 5, 6), v(7, 8, 9)]
74
- const expectedResult = v(12, 15, 18)
75
-
76
- expect(sum(...vectors)).toStrictEqual(expectedResult)
77
- })
@@ -1,43 +0,0 @@
1
- import { expect, test } from "vitest"
2
-
3
- import { zero } from "../math/vector.js"
4
- import { v } from "../v.js"
5
- import { applyFriction } from "./friction.js"
6
-
7
- test("it should apply friction to some velocity", () => {
8
- const velocity = v(8, 0, 6)
9
- const friction = 5
10
- const expectedResult = v(4, 0, 3)
11
-
12
- expect(applyFriction({ velocity, friction })).toStrictEqual(expectedResult)
13
- })
14
-
15
- test("it should stop movement when friction is equal to velocity", () => {
16
- const velocity = v(8, 0, 6)
17
- const friction = 5
18
- const dt = 2
19
-
20
- expect(applyFriction({ velocity, friction }, dt)).toStrictEqual(zero())
21
- })
22
-
23
- test("it should apply no friction on no velocity", () => {
24
- const velocity = zero()
25
- const friction = 5
26
-
27
- expect(applyFriction({ velocity, friction })).toBe(velocity)
28
- })
29
-
30
- test("it should apply no friction if no friction is passed", () => {
31
- const velocity = v(8, 0, 6)
32
- const friction = 0
33
-
34
- expect(applyFriction({ velocity, friction })).toStrictEqual(velocity)
35
- })
36
-
37
- test("it should apply no friction if no time passed", () => {
38
- const velocity = v(8, 0, 6)
39
- const friction = 5
40
- const dt = 0
41
-
42
- expect(applyFriction({ velocity, friction }, dt)).toStrictEqual(velocity)
43
- })
@@ -1,78 +0,0 @@
1
- import { expect, test } from "vitest"
2
-
3
- import { v } from "../v.js"
4
- import { applyGravity } from "./gravity.js"
5
-
6
- test("it should apply gravity based on input parameters", () => {
7
- const params = {
8
- maxJump: 10,
9
- maxLeap: 10,
10
- maxSpeed: 10,
11
- vy: 10,
12
- position: v(0, 10, 0),
13
- }
14
-
15
- expect(applyGravity(params)).toStrictEqual({
16
- ay: -20,
17
- vy: -10,
18
- position: v(0, -10, 0),
19
- })
20
- })
21
-
22
- test("it should compute no gravity when max jump height is not set", () => {
23
- const params = {
24
- maxLeap: 10,
25
- maxSpeed: 10,
26
- vy: 10,
27
- position: v(0, 10, 0),
28
- }
29
-
30
- expect(applyGravity(params)).toStrictEqual({
31
- ay: -0,
32
- vy: 10,
33
- position: v(0, 20, 0),
34
- })
35
- })
36
-
37
- test("it should compute no gravity when max movement speed is not set", () => {
38
- const params = {
39
- maxJump: 10,
40
- maxLeap: 10,
41
- vy: 10,
42
- position: v(0, 10, 0),
43
- }
44
-
45
- expect(applyGravity(params)).toStrictEqual({
46
- ay: -0,
47
- vy: 10,
48
- position: v(0, 20, 0),
49
- })
50
- })
51
-
52
- test("it should apply no gravity when no time has passed", () => {
53
- const params = {
54
- maxJump: 10,
55
- maxLeap: 10,
56
- maxSpeed: 10,
57
- vy: 10,
58
- position: v(0, 10, 0),
59
- }
60
- const dt = 0
61
-
62
- expect(applyGravity(params, dt)).toStrictEqual({
63
- ay: -20,
64
- vy: 10,
65
- position: v(0, 10, 0),
66
- })
67
- })
68
-
69
- test("it should throw an error when max leap distance is not set", () => {
70
- const params = {
71
- maxJump: 10,
72
- maxSpeed: 10,
73
- vy: 10,
74
- position: v(0, 10, 0),
75
- }
76
-
77
- expect(() => applyGravity(params)).toThrow()
78
- })
package/src/v.test.js DELETED
@@ -1,48 +0,0 @@
1
- import { expect, test } from "vitest"
2
-
3
- import { ensureV, v } from "./v"
4
-
5
- test("ensureV() should convert a numeric array into a vector", () => {
6
- const array = [1, 2, 3]
7
- const result = ensureV(array)
8
- expect(result).toStrictEqual([1, 2, 3])
9
- expect(result.__isVector__).toBe(true)
10
- })
11
-
12
- test("ensureV() should return an existing vector unchanged", () => {
13
- const vector = v(4, 5, 6)
14
- const result = ensureV(vector)
15
- expect(result).toBe(vector)
16
- expect(result.__isVector__).toBe(true)
17
- })
18
-
19
- test("ensureV() should not convert an array with non-finite numbers", () => {
20
- const array = [1, Infinity, 3]
21
- const result = ensureV(array)
22
- expect(result).toBe(array)
23
- expect(result.__isVector__).toBeUndefined()
24
- })
25
-
26
- test("ensureV() should not convert an array with non-numeric values", () => {
27
- const array = [1, "a", 3]
28
- const result = ensureV(array)
29
- expect(result).toBe(array)
30
- expect(result.__isVector__).toBeUndefined()
31
- })
32
-
33
- test("ensureV() should return non-array values unchanged", () => {
34
- expect(ensureV(123)).toBe(123)
35
- expect(ensureV("hello")).toBe("hello")
36
- const obj = { a: 1 }
37
- expect(ensureV(obj)).toBe(obj)
38
- })
39
-
40
- test("a vector created with the v() function is just a number array", () => {
41
- const vector = v(1, 2, 3)
42
- expect(vector).toStrictEqual([1, 2, 3])
43
- })
44
-
45
- test("a vector created with the v() function has a __isVector__ property", () => {
46
- const vector = v(1, 2, 3)
47
- expect(vector.__isVector__).toBe(true)
48
- })