@inglorious/utils 1.0.0 → 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.
Files changed (67) hide show
  1. package/README.md +10 -11
  2. package/package.json +4 -4
  3. package/src/algorithms/decision-tree.js +24 -0
  4. package/src/algorithms/decision-tree.test.js +153 -0
  5. package/src/algorithms/path-finding.js +150 -0
  6. package/src/algorithms/path-finding.test.js +151 -0
  7. package/src/algorithms/types.d.ts +28 -0
  8. package/src/data-structures/array.js +93 -0
  9. package/src/data-structures/array.test.js +185 -0
  10. package/src/data-structures/board.js +159 -0
  11. package/src/data-structures/board.test.js +242 -0
  12. package/src/data-structures/boolean.js +9 -0
  13. package/src/data-structures/heap.js +164 -0
  14. package/src/data-structures/heap.test.js +103 -0
  15. package/src/data-structures/object.js +138 -0
  16. package/src/data-structures/object.test.js +218 -0
  17. package/src/data-structures/objects.js +66 -0
  18. package/src/data-structures/objects.test.js +99 -0
  19. package/src/data-structures/set.js +9 -0
  20. package/src/data-structures/set.test.js +15 -0
  21. package/src/data-structures/tree.js +36 -0
  22. package/src/data-structures/tree.test.js +33 -0
  23. package/src/data-structures/types.d.ts +4 -0
  24. package/src/functions/functions.js +19 -0
  25. package/src/functions/functions.test.js +23 -0
  26. package/src/math/geometry/circle.js +69 -0
  27. package/src/math/geometry/circle.test.js +97 -0
  28. package/src/math/geometry/hitmask.js +70 -0
  29. package/src/math/geometry/hitmask.test.js +155 -0
  30. package/src/math/geometry/line.js +34 -0
  31. package/src/math/geometry/line.test.js +49 -0
  32. package/src/math/geometry/point.js +78 -0
  33. package/src/math/geometry/point.test.js +81 -0
  34. package/src/math/geometry/rectangle.js +76 -0
  35. package/src/math/geometry/rectangle.test.js +42 -0
  36. package/src/math/geometry/segment.js +72 -0
  37. package/src/math/geometry/segment.test.js +183 -0
  38. package/src/math/geometry/triangle.js +15 -0
  39. package/src/math/geometry/triangle.test.js +11 -0
  40. package/src/math/geometry/types.d.ts +23 -0
  41. package/src/math/linear-algebra/2d.js +28 -0
  42. package/src/math/linear-algebra/2d.test.js +17 -0
  43. package/src/math/linear-algebra/quaternion.js +22 -0
  44. package/src/math/linear-algebra/quaternion.test.js +25 -0
  45. package/src/math/linear-algebra/quaternions.js +20 -0
  46. package/src/math/linear-algebra/quaternions.test.js +29 -0
  47. package/src/math/linear-algebra/types.d.ts +4 -0
  48. package/src/math/linear-algebra/vector.js +337 -0
  49. package/src/math/linear-algebra/vector.test.js +265 -0
  50. package/src/math/linear-algebra/vectors.js +122 -0
  51. package/src/math/linear-algebra/vectors.test.js +65 -0
  52. package/src/math/linear-interpolation.js +9 -0
  53. package/src/math/numbers.js +90 -0
  54. package/src/math/numbers.test.js +137 -0
  55. package/src/math/rng.js +44 -0
  56. package/src/math/rng.test.js +39 -0
  57. package/src/math/statistics.js +43 -0
  58. package/src/math/statistics.test.js +47 -0
  59. package/src/math/trigonometry.js +89 -0
  60. package/src/math/trigonometry.test.js +52 -0
  61. package/src/physics/acceleration.js +56 -0
  62. package/src/physics/friction.js +24 -0
  63. package/src/physics/friction.test.js +42 -0
  64. package/src/physics/gravity.js +69 -0
  65. package/src/physics/gravity.test.js +77 -0
  66. package/src/physics/jump.js +31 -0
  67. package/src/physics/velocity.js +32 -0
@@ -0,0 +1,122 @@
1
+ /**
2
+ * @typedef {import('./types').Vector2} Vector2
3
+ * @typedef {import('./types').Vector3} Vector3
4
+ * @typedef {import('./types').Vector7} Vector7
5
+ */
6
+
7
+ import { magnitude, shift } from "./vector.js"
8
+
9
+ /**
10
+ * Alias for the sum function.
11
+ * @type {typeof sum}
12
+ */
13
+ export const add = sum
14
+
15
+ /**
16
+ * Computes the cross product of multiple vectors.
17
+ * @param {...Vector3 | Vector7} vectors - The vectors to compute the cross product for.
18
+ * @returns {Vector3 | Vector7} The resulting vector after the cross product.
19
+ */
20
+ export function cross(...vectors) {
21
+ return vectors.reduce(crossMultiplyCoordinates)
22
+ }
23
+
24
+ /**
25
+ * Computes the distance between multiple vectors.
26
+ * @param {...Vector2 | Vector3} vectors - The vectors to compute the distance between.
27
+ * @returns {number} The distance between the vectors.
28
+ */
29
+ export function distance(...vectors) {
30
+ return magnitude(subtract(...vectors))
31
+ }
32
+
33
+ /**
34
+ * Computes the dot product of multiple vectors.
35
+ * @param {...Vector2 | Vector3} vectors - The vectors to compute the dot product for.
36
+ * @returns {number} The resulting scalar value of the dot product.
37
+ */
38
+ export function dot(...vectors) {
39
+ return vectors
40
+ .reduce(dotMultiplyCoordinates)
41
+ .reduce((coord1, coord2) => coord1 + coord2)
42
+ }
43
+
44
+ /**
45
+ * Alias for the dot product function.
46
+ * @type {typeof dot}
47
+ */
48
+ export const scalarProduct = dot
49
+
50
+ /**
51
+ * Subtracts multiple vectors.
52
+ * @param {...Vector2 | Vector3} vectors - The vectors to subtract.
53
+ * @returns {Vector2 | Vector3} The resulting vector after subtraction.
54
+ */
55
+ export function subtract(...vectors) {
56
+ return vectors.reduce(subtractCoordinates)
57
+ }
58
+
59
+ /**
60
+ * Sums multiple vectors.
61
+ * @param {...Vector2 | Vector3} vectors - The vectors to sum.
62
+ * @returns {Vector2 | Vector3} The resulting vector after summation.
63
+ */
64
+ export function sum(...vectors) {
65
+ return vectors.reduce(sumCoordinates)
66
+ }
67
+
68
+ /**
69
+ * Alias for the cross product function.
70
+ * @type {typeof cross}
71
+ */
72
+ export const vectorProduct = cross
73
+
74
+ /**
75
+ * Adds the coordinates of two vectors.
76
+ * @param {Vector2 | Vector3} vector1 - The first vector.
77
+ * @param {Vector2 | Vector3} vector2 - The second vector.
78
+ * @returns {Vector2 | Vector3} The resulting vector after addition.
79
+ */
80
+ function sumCoordinates(vector1, vector2) {
81
+ return vector1.map((coordinate, index) => coordinate + vector2[index])
82
+ }
83
+
84
+ /**
85
+ * Subtracts the coordinates of two vectors.
86
+ * @param {Vector2 | Vector3} vector1 - The first vector.
87
+ * @param {Vector2 | Vector3} vector2 - The second vector.
88
+ * @returns {Vector2 | Vector3} The resulting vector after subtraction.
89
+ */
90
+ function subtractCoordinates(vector1, vector2) {
91
+ return vector1.map((coordinate, index) => coordinate - vector2[index])
92
+ }
93
+
94
+ /**
95
+ * Computes the cross product of two vectors' coordinates.
96
+ * @param {Vector3} vector1 - The first vector.
97
+ * @param {Vector3} vector2 - The second vector.
98
+ * @returns {Vector3} The resulting vector after the cross product.
99
+ */
100
+ function crossMultiplyCoordinates(vector1, vector2) {
101
+ const indexes = Array(vector1.length)
102
+ .fill(null)
103
+ .map((_, index) => index)
104
+
105
+ return indexes.map((_, index) => {
106
+ const [index1, index2] = shift(
107
+ indexes.filter((_, i) => i !== index),
108
+ index,
109
+ )
110
+ return vector1[index1] * vector2[index2] - vector1[index2] * vector2[index1]
111
+ })
112
+ }
113
+
114
+ /**
115
+ * Multiplies the coordinates of two vectors.
116
+ * @param {Vector2 | Vector3} vector1 - The first vector.
117
+ * @param {Vector2 | Vector3} vector2 - The second vector.
118
+ * @returns {Vector2 | Vector3} The resulting vector containing multiplied coordinates.
119
+ */
120
+ function dotMultiplyCoordinates(vector1, vector2) {
121
+ return vector1.map((coordinate, index) => coordinate * vector2[index])
122
+ }
@@ -0,0 +1,65 @@
1
+ import { expect, test } from "vitest"
2
+
3
+ import { cross, distance, dot, subtract, sum } from "./vectors.js"
4
+
5
+ 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]
9
+
10
+ expect(cross(vector1, vector2)).toStrictEqual(expectedResult)
11
+ })
12
+
13
+ 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]
20
+
21
+ expect(cross(...vectors)).toStrictEqual(expectedResult)
22
+ })
23
+
24
+ test("it should compute the distance between multiple vectors", () => {
25
+ const vectors = [
26
+ [0, 0, 0],
27
+ [4, 0, 3],
28
+ ]
29
+ const expectedResult = 5
30
+
31
+ expect(distance(...vectors)).toStrictEqual(expectedResult)
32
+ })
33
+
34
+ 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
+ ]
40
+ const expectedResult = 270
41
+
42
+ expect(dot(...vectors)).toStrictEqual(expectedResult)
43
+ })
44
+
45
+ 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]
52
+
53
+ expect(subtract(...vectors)).toStrictEqual(expectedResult)
54
+ })
55
+
56
+ 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]
63
+
64
+ expect(sum(...vectors)).toStrictEqual(expectedResult)
65
+ })
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Performs linear interpolation between two numbers.
3
+ *
4
+ * @param {number} a - The start value.
5
+ * @param {number} b - The end value.
6
+ * @param {number} t - The interpolation factor, typically in the range [0, 1].
7
+ * @returns {number} The interpolated value.
8
+ */
9
+ export const lerp = (a, b, t) => a + (b - a) * t
@@ -0,0 +1,90 @@
1
+ const DEFAULT_PRECISION = 1 // Default precision used for snapping numbers.
2
+ const DEFAULT_TOLERANCE = 0.1 // Default tolerance used for comparing numbers.
3
+ const SQUARE_ROOT = 0.5 // Exponent used for calculating square roots.
4
+
5
+ /**
6
+ * Returns the absolute value of a number.
7
+ * @param {number} num - The number to get the absolute value of.
8
+ * @returns {number} The absolute value of the input number.
9
+ */
10
+ export function abs(num) {
11
+ return Math.abs(num)
12
+ }
13
+
14
+ /**
15
+ * Clamps a number within the inclusive range specified by min and max.
16
+ * @param {number} num - The number to clamp.
17
+ * @param {number} min - The minimum value.
18
+ * @param {number} max - The maximum value.
19
+ * @returns {number} The clamped value.
20
+ */
21
+ export function clamp(num, min, max) {
22
+ if (num < min) {
23
+ return min
24
+ }
25
+
26
+ if (num > max) {
27
+ return max
28
+ }
29
+
30
+ return num
31
+ }
32
+
33
+ /**
34
+ * Checks if two numbers are close to each other within a given tolerance.
35
+ * @param {number} num1 - The first number.
36
+ * @param {number} num2 - The second number.
37
+ * @param {number} [tolerance=DEFAULT_TOLERANCE] - The tolerance for comparison.
38
+ * @returns {boolean} True if the numbers are close, false otherwise.
39
+ */
40
+ export function isClose(num1, num2, tolerance = DEFAULT_TOLERANCE) {
41
+ return abs(num1 - num2) <= tolerance
42
+ }
43
+
44
+ /**
45
+ * Computes the modulus of two numbers, ensuring a positive result.
46
+ * @param {number} dividend - The number to be divided.
47
+ * @param {number} divisor - The number to divide by.
48
+ * @returns {number} The modulus result.
49
+ */
50
+ export function mod(dividend, divisor) {
51
+ return ((dividend % divisor) + divisor) % divisor
52
+ }
53
+
54
+ /**
55
+ * Snaps a number to the nearest multiple of a given precision.
56
+ * @param {number} num - The number to snap.
57
+ * @param {number} [precision=DEFAULT_PRECISION] - The precision to snap to.
58
+ * @returns {number} The snapped value.
59
+ */
60
+ export function snap(num, precision = DEFAULT_PRECISION) {
61
+ return Math.round(num / precision) * precision
62
+ }
63
+
64
+ /**
65
+ * Alias for the `mod` function.
66
+ * @type {typeof mod}
67
+ */
68
+ export const remainder = mod
69
+
70
+ /**
71
+ * Returns the sign of a number: 1 for positive, -1 for negative, and 0 for zero.
72
+ * @param {number} num - The number to get the sign of.
73
+ * @returns {number} The sign of the number.
74
+ */
75
+ export function sign(num) {
76
+ if (!num) {
77
+ return num
78
+ }
79
+
80
+ return num / abs(num)
81
+ }
82
+
83
+ /**
84
+ * Computes the square root of a number.
85
+ * @param {number} num - The number to compute the square root of.
86
+ * @returns {number} The square root of the number.
87
+ */
88
+ export function sqrt(num) {
89
+ return num ** SQUARE_ROOT
90
+ }
@@ -0,0 +1,137 @@
1
+ import { expect, test } from "vitest"
2
+
3
+ import { abs, clamp, isClose, mod, sign, snap, sqrt } from "./numbers.js"
4
+
5
+ test("it should return the same number if already positive", () => {
6
+ const num = 42
7
+ const expectedResult = 42
8
+
9
+ expect(abs(num)).toBe(expectedResult)
10
+ })
11
+
12
+ test("it should return the absolute value of a number", () => {
13
+ const num = -42
14
+ const expectedResult = 42
15
+
16
+ expect(abs(num)).toBe(expectedResult)
17
+ })
18
+
19
+ test("it should not clamp a number in the range", () => {
20
+ const num = 42
21
+ const min = 0
22
+ const max = 100
23
+ const expectedResult = 42
24
+
25
+ expect(clamp(num, min, max)).toBe(expectedResult)
26
+ })
27
+
28
+ test("it should clamp a number too small", () => {
29
+ const num = -42
30
+ const min = 0
31
+ const max = 100
32
+ const expectedResult = 0
33
+
34
+ expect(clamp(num, min, max)).toBe(expectedResult)
35
+ })
36
+
37
+ test("it should clamp a number too big", () => {
38
+ const num = 142
39
+ const min = 0
40
+ const max = 100
41
+ const expectedResult = 100
42
+
43
+ expect(clamp(num, min, max)).toBe(expectedResult)
44
+ })
45
+
46
+ test("it should check if two numbers are close given some tolerance", () => {
47
+ const num1 = 42
48
+ const num2 = 42.1
49
+ const tolerance = 0.2
50
+ const expectedResult = true
51
+
52
+ expect(isClose(num1, num2, tolerance)).toBe(expectedResult)
53
+ })
54
+
55
+ test("it should return zero if dividend equals divisor", () => {
56
+ const num = 2
57
+ const divisor = 2
58
+ const expectedResult = 0
59
+
60
+ expect(mod(num, divisor)).toBe(expectedResult)
61
+ })
62
+
63
+ test("it should return one if number is odd", () => {
64
+ const num = 3
65
+ const divisor = 2
66
+ const expectedResult = 1
67
+
68
+ expect(mod(num, divisor)).toBe(expectedResult)
69
+ })
70
+
71
+ test("it should return one if negative number turns to positive one", () => {
72
+ const num = -3
73
+ const divisor = 2
74
+ const expectedResult = 1
75
+
76
+ expect(mod(num, divisor)).toBe(expectedResult)
77
+ })
78
+
79
+ test("it should convert a float number greater than 1 to a range between -1 and 1", () => {
80
+ const num = 5 / 4
81
+ const divisor = 1
82
+ const expectedResult = 1 / 4
83
+
84
+ expect(mod(num, divisor)).toBe(expectedResult)
85
+ })
86
+
87
+ test("it should convert a float number greater than 2 to a range between -1 and 1", () => {
88
+ const num = 13 / 4
89
+ const divisor = 1
90
+ const expectedResult = 1 / 4
91
+
92
+ expect(mod(num, divisor)).toBe(expectedResult)
93
+ })
94
+
95
+ test("it should convert a negative float number to a range between -1 and 1", () => {
96
+ const num = -5 / 4
97
+ const divisor = 1
98
+ const expectedResult = 3 / 4
99
+
100
+ expect(mod(num, divisor)).toBe(expectedResult)
101
+ })
102
+
103
+ test("it should return one if number is positive", () => {
104
+ const num = 42
105
+ const expectedResult = 1
106
+
107
+ expect(sign(num)).toBe(expectedResult)
108
+ })
109
+
110
+ test("it should return minus one if number is negative", () => {
111
+ const num = -42
112
+ const expectedResult = -1
113
+
114
+ expect(sign(num)).toBe(expectedResult)
115
+ })
116
+
117
+ test("it should return the number itself if the sign is zero", () => {
118
+ const num = 0
119
+ const expectedResult = 0
120
+
121
+ expect(sign(num)).toBe(expectedResult)
122
+ })
123
+
124
+ test("it should snap the number to the given precision", () => {
125
+ const num = 42
126
+ const precision = 10
127
+ const expectedResult = 40
128
+
129
+ expect(snap(num, precision)).toBe(expectedResult)
130
+ })
131
+
132
+ test("it should compute the square root of the number", () => {
133
+ const num = 9
134
+ const expectedResult = 3
135
+
136
+ expect(sqrt(num)).toBe(expectedResult)
137
+ })
@@ -0,0 +1,44 @@
1
+ /* eslint-disable no-magic-numbers */
2
+
3
+ /**
4
+ * Generates a random number.
5
+ * - If no arguments are provided, returns a random float between 0 (inclusive) and 1 (exclusive).
6
+ * - If one argument is provided, returns a random integer between 0 and the given number (inclusive).
7
+ * - If two or more arguments are provided, returns a random integer within the specified range and step.
8
+ *
9
+ * @param {number} [to] - The upper bound (inclusive) if one argument is provided.
10
+ * @param {number} [from] - The lower bound (inclusive) if two arguments are provided.
11
+ * @param {number} [step=1] - The step size if two or more arguments are provided.
12
+ * @returns {number} A random number based on the provided arguments.
13
+ */
14
+ export function random(...args) {
15
+ let step, from, to
16
+
17
+ if (!args.length) {
18
+ return Math.random()
19
+ }
20
+
21
+ if (args.length === 1) {
22
+ step = 1
23
+ from = 0
24
+ to = args[0] + 1
25
+ }
26
+
27
+ if (args.length > 1) {
28
+ step = args[2] ?? 1
29
+ from = args[0] / step
30
+ to = (args[1] + 1) / step
31
+ }
32
+
33
+ return Math.floor(Math.random() * (to - from) + from) * step
34
+ }
35
+
36
+ /**
37
+ * Generates a random binomial value.
38
+ * The result is the difference between two random numbers.
39
+ *
40
+ * @returns {number} A random binomial value.
41
+ */
42
+ export function randomBinomial() {
43
+ return random() - random()
44
+ }
@@ -0,0 +1,39 @@
1
+ import { expect, test } from "vitest"
2
+
3
+ import { random, randomBinomial } from "./rng.js"
4
+
5
+ test("it should create a random number in the range [0, 1)", () => {
6
+ const randomNumber = random()
7
+
8
+ expect(randomNumber).toBeGreaterThanOrEqual(0)
9
+ expect(randomNumber).toBeLessThan(1)
10
+ })
11
+
12
+ test("it should create a random integer in the range [0, 6]", () => {
13
+ const randomNumber = random(6)
14
+
15
+ expect(randomNumber).toBeGreaterThanOrEqual(0)
16
+ expect(randomNumber).toBeLessThanOrEqual(6)
17
+ })
18
+
19
+ test("it should create a random integer in the range [1, 6]", () => {
20
+ const randomNumber = random(1, 6)
21
+
22
+ expect(randomNumber).toBeGreaterThanOrEqual(1)
23
+ expect(randomNumber).toBeLessThanOrEqual(6)
24
+ })
25
+
26
+ test("it should create a random even number in the range [2, 6]", () => {
27
+ const randomNumber = random(2, 6, 2)
28
+
29
+ expect(randomNumber).toBeGreaterThanOrEqual(1)
30
+ expect(randomNumber).toBeLessThanOrEqual(6)
31
+ expect(randomNumber % 2).toBe(0)
32
+ })
33
+
34
+ test("it should create a random number in the range (-1, 1)", () => {
35
+ const randomNumber = randomBinomial()
36
+
37
+ expect(randomNumber).toBeGreaterThan(-1)
38
+ expect(randomNumber).toBeLessThan(1)
39
+ })
@@ -0,0 +1,43 @@
1
+ const HALF = 2
2
+ const AROUND_INDEX = 1
3
+ const NO_OCCURRENCES = 0
4
+
5
+ export const average = mean
6
+
7
+ export function mean(...numbers) {
8
+ return numbers.reduce((acc, num) => acc + num) / numbers.length
9
+ }
10
+
11
+ export function median(...numbers) {
12
+ const sortedNumbers = numbers.toSorted()
13
+ const middleIndex = numbers.length / HALF
14
+
15
+ if (Number.isInteger(middleIndex)) {
16
+ return mean(
17
+ ...sortedNumbers.slice(
18
+ middleIndex - AROUND_INDEX,
19
+ middleIndex + AROUND_INDEX,
20
+ ),
21
+ )
22
+ }
23
+
24
+ return sortedNumbers[Math.floor(middleIndex)]
25
+ }
26
+
27
+ export function mode(...values) {
28
+ const occurrences = values.reduce((acc, value) => {
29
+ acc[value] = acc[value] ?? NO_OCCURRENCES
30
+ acc[value]++
31
+ return acc
32
+ }, {})
33
+
34
+ return Object.entries(occurrences).reduce(
35
+ (acc, [value, occurrences]) => {
36
+ if (occurrences > acc.occurrences) {
37
+ acc = { value, occurrences }
38
+ }
39
+ return acc
40
+ },
41
+ { occurrences: 0 },
42
+ ).value
43
+ }
@@ -0,0 +1,47 @@
1
+ import { expect, test } from "vitest"
2
+
3
+ import { mean, median, mode } from "./statistics.js"
4
+
5
+ test("it should return the average of the given numbers", () => {
6
+ const numbers = [1, 3, 2]
7
+ const expectedResult = 2
8
+
9
+ expect(mean(...numbers)).toBeCloseTo(expectedResult)
10
+ })
11
+
12
+ test("it should return the median value from an odd list of numbers", () => {
13
+ const numbers = [1, 3, 2]
14
+ const expectedResult = 2
15
+
16
+ expect(median(...numbers)).toBe(expectedResult)
17
+ })
18
+
19
+ test("it should return the median value from an even list of numbers", () => {
20
+ const numbers = [1, 3, 2, 4]
21
+ const expectedResult = 2.5
22
+
23
+ expect(median(...numbers)).toBe(expectedResult)
24
+ })
25
+
26
+ test("it should find the mode of a list of values", () => {
27
+ const values = [
28
+ "rarely",
29
+ "seldom",
30
+ "seldom",
31
+ "sometimes",
32
+ "sometimes",
33
+ "sometimes",
34
+ "often",
35
+ "often",
36
+ "often",
37
+ "often",
38
+ "always",
39
+ "always",
40
+ "always",
41
+ "always",
42
+ "always",
43
+ ]
44
+ const expectedResult = "always"
45
+
46
+ expect(mode(...values)).toBe(expectedResult)
47
+ })
@@ -0,0 +1,89 @@
1
+ import { mod } from "./numbers.js"
2
+
3
+ const HALF_CIRCLE_IN_DEGRESS = 180 // Half-circle in degrees (180°)
4
+ const FULL_CIRCLE = 2 // Full circle multiplier for radians (2π)
5
+
6
+ /**
7
+ * Calculates the arctangent of the quotient of its arguments.
8
+ * @param {number} y - The y-coordinate.
9
+ * @param {number} x - The x-coordinate.
10
+ * @returns {number} The angle in radians.
11
+ */
12
+ export function atan2(y, x) {
13
+ return Math.atan2(y, x)
14
+ }
15
+
16
+ /**
17
+ * Calculates the cosine of an angle.
18
+ * @param {number} angle - The angle in radians.
19
+ * @returns {number} The cosine of the angle.
20
+ */
21
+ export function cos(angle) {
22
+ return Math.cos(angle)
23
+ }
24
+
25
+ /**
26
+ * Alias for the `cos` function.
27
+ * @type {typeof cos}.
28
+ */
29
+ export const cosine = cos
30
+
31
+ /**
32
+ * Returns the value of π (pi).
33
+ * @returns {number} The value of π.
34
+ */
35
+ export function pi() {
36
+ return Math.PI
37
+ }
38
+
39
+ /**
40
+ * Calculates the sine of an angle.
41
+ * @param {number} angle - The angle in radians.
42
+ * @returns {number} The sine of the angle.
43
+ */
44
+ export function sin(angle) {
45
+ return Math.sin(angle)
46
+ }
47
+
48
+ /**
49
+ * Alias for the `sin` function.
50
+ * @type {typeof sin}.
51
+ */
52
+ export const sine = sin
53
+
54
+ /**
55
+ * Converts an angle from radians to degrees.
56
+ * @param {number} radians - The angle in radians.
57
+ * @returns {number} The angle in degrees.
58
+ */
59
+ export function toDegrees(radians) {
60
+ return (radians * HALF_CIRCLE_IN_DEGRESS) / pi()
61
+ }
62
+
63
+ /**
64
+ * Converts an angle from degrees to radians.
65
+ * @param {number} degrees - The angle in degrees.
66
+ * @returns {number} The angle in radians.
67
+ */
68
+ export function toRadians(degrees) {
69
+ return (degrees * pi()) / HALF_CIRCLE_IN_DEGRESS
70
+ }
71
+
72
+ /**
73
+ * Normalizes an angle to the range [-π, π].
74
+ * @param {number} angle - The angle in radians.
75
+ * @returns {number} The normalized angle in radians.
76
+ */
77
+ export function toRange(angle) {
78
+ if (angle > -pi() && angle < pi()) {
79
+ return angle
80
+ }
81
+
82
+ angle = mod(angle, FULL_CIRCLE * pi())
83
+
84
+ if (angle > pi()) {
85
+ angle -= FULL_CIRCLE * pi()
86
+ }
87
+
88
+ return angle
89
+ }