@inglorious/utils 2.1.0 → 2.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inglorious/utils",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "A set of general-purpose utility functions designed with functional programming principles in mind.",
5
5
  "author": "IceOnFire <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
6
6
  "license": "MIT",
@@ -25,21 +25,21 @@ export function contains(arr, item) {
25
25
  /**
26
26
  * Ensures that the given value is an array. If it's not an array, it wraps it in one.
27
27
  *
28
- * @param {*} arr - The value to check.
28
+ * @param {*} value - The value to check.
29
29
  * @returns {Array} - The original array or the value wrapped in a new array.
30
30
  */
31
- export function ensureArray(arr) {
32
- return isArray(arr) ? arr : [arr]
31
+ export function ensureArray(value) {
32
+ return isArray(value) ? value : [value]
33
33
  }
34
34
 
35
35
  /**
36
36
  * Checks if a value is an array.
37
- * @param {*} arr - The value to check.
38
37
  *
38
+ * @param {*} value - The value to check.
39
39
  * @returns {boolean} True if the value is an array, false otherwise.
40
40
  */
41
- export function isArray(arr) {
42
- return Array.isArray(arr)
41
+ export function isArray(value) {
42
+ return Array.isArray(value)
43
43
  }
44
44
 
45
45
  /**
@@ -44,11 +44,11 @@ export function find(obj, callback) {
44
44
  /**
45
45
  * Checks if a value is a plain object.
46
46
  *
47
- * @param {*} obj - The value to check.
47
+ * @param {*} value - The value to check.
48
48
  * @returns {boolean} True if the value is a plain object, false otherwise.
49
49
  */
50
- export function isObject(obj) {
51
- return obj != null && obj.constructor === Object
50
+ export function isObject(value) {
51
+ return value != null && value.constructor === Object
52
52
  }
53
53
 
54
54
  /**
@@ -8,6 +8,7 @@
8
8
  const SQUARED = 2
9
9
  const HALF = 2
10
10
 
11
+ import { isVector } from "../linear-algebra/vector.js"
11
12
  import { distanceFromPoint } from "./line.js"
12
13
 
13
14
  /**
@@ -27,8 +28,8 @@ export function getDistanceFromLine(point, line) {
27
28
  * @returns {boolean} True if the points intersect, false otherwise.
28
29
  */
29
30
  export function intersectsPoint(point1, point2) {
30
- const [x1, y1, z1] = point1
31
- const [x2, y2, z2] = point2
31
+ const [x1, y1, z1] = ensurePoint(point1)
32
+ const [x2, y2, z2] = ensurePoint(point2)
32
33
  return x1 === x2 && y1 === y2 && z1 === z2
33
34
  }
34
35
 
@@ -39,7 +40,7 @@ export function intersectsPoint(point1, point2) {
39
40
  * @returns {boolean} True if the point intersects the circle, false otherwise.
40
41
  */
41
42
  export function intersectsCircle(point, circle) {
42
- const [x, y, z] = point
43
+ const [x, y, z] = ensurePoint(point)
43
44
  const [cx, cy, cz] = circle.position
44
45
  const radius = circle.radius
45
46
 
@@ -56,7 +57,7 @@ export function intersectsCircle(point, circle) {
56
57
  * @returns {boolean} True if the point intersects the rectangle, false otherwise.
57
58
  */
58
59
  export function intersectsRectangle(point, rectangle) {
59
- const [x, y, z] = point
60
+ const [x, y, z] = ensurePoint(point)
60
61
  const [rectX, rectY, rectZ] = rectangle.position
61
62
  const [width, height, depth] = rectangle.size
62
63
 
@@ -76,3 +77,11 @@ export function intersectsRectangle(point, rectangle) {
76
77
  z <= front
77
78
  )
78
79
  }
80
+
81
+ function ensurePoint(value) {
82
+ if (!isVector(value)) {
83
+ return value.position
84
+ }
85
+
86
+ return value
87
+ }
@@ -79,3 +79,15 @@ test("it should prove that a point outside of a rectangle does not intersect wit
79
79
 
80
80
  expect(intersectsRectangle(point, rectangle)).toBe(false)
81
81
  })
82
+
83
+ test("it should work even if the point is an object with a position", () => {
84
+ const point = {
85
+ position: [1.5, 1.5, 0],
86
+ }
87
+ const rectangle = {
88
+ position: [1, 1, 0],
89
+ size: [2, 2, 0],
90
+ }
91
+
92
+ expect(intersectsRectangle(point, rectangle)).toBe(true)
93
+ })
@@ -3,6 +3,7 @@
3
3
  * @typedef {import('./types').Vector3} Vector3
4
4
  */
5
5
 
6
+ import { isArray } from "../../data-structures/array.js"
6
7
  import { hypothenuse } from "../geometry/triangle.js"
7
8
  import {
8
9
  abs as nAbs,
@@ -112,6 +113,19 @@ export function fromAngle(angle) {
112
113
 
113
114
  export const length = magnitude
114
115
 
116
+ /**
117
+ * Checks if a value is a vector.
118
+ *
119
+ * @param {*} value - The value to check.
120
+ * @returns {boolean} True if the value is a vector, false otherwise.
121
+ */
122
+ export function isVector(value) {
123
+ return (
124
+ isArray(value) &&
125
+ value.every((coordinate) => typeof coordinate === "number")
126
+ )
127
+ }
128
+
115
129
  /**
116
130
  * Calculates the magnitude of the vector.
117
131
  * @param {Vector3} vector - The input vector.