@inglorious/utils 1.1.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.
- package/README.md +10 -11
- package/package.json +4 -4
- package/src/data-structures/array.js +10 -0
- package/src/data-structures/array.test.js +13 -1
- package/src/data-structures/set.js +9 -0
- package/src/data-structures/set.test.js +15 -0
- package/src/math/geometry/segment.test.js +6 -6
- package/src/math/linear-algebra/vector.js +11 -0
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Inglorious Utils
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/@inglorious/utils)
|
|
4
4
|
[](https://opensource.org/licenses/MIT)
|
|
@@ -24,7 +24,7 @@ Here are a few examples of how you can use the utilities in this package.
|
|
|
24
24
|
Compose functions together in a readable, left-to-right sequence.
|
|
25
25
|
|
|
26
26
|
```javascript
|
|
27
|
-
import { pipe } from "@inglorious/utils/
|
|
27
|
+
import { pipe } from "@inglorious/utils/functions/functions"
|
|
28
28
|
|
|
29
29
|
const add = (a) => (b) => a + b
|
|
30
30
|
const multiply = (a) => (b) => a * b
|
|
@@ -43,24 +43,23 @@ const result = calculate(10) // 40
|
|
|
43
43
|
Perform vector operations on plain objects.
|
|
44
44
|
|
|
45
45
|
```javascript
|
|
46
|
-
import { add } from "@inglorious/utils/vector"
|
|
46
|
+
import { add } from "@inglorious/utils/math/linear-algebra/vector"
|
|
47
47
|
|
|
48
|
-
const position =
|
|
49
|
-
const velocity =
|
|
48
|
+
const position = [10, 20]
|
|
49
|
+
const velocity = [2, -1]
|
|
50
50
|
|
|
51
51
|
const newPosition = add(position, velocity)
|
|
52
|
-
// =>
|
|
52
|
+
// => [12, 19]
|
|
53
53
|
```
|
|
54
54
|
|
|
55
55
|
## API
|
|
56
56
|
|
|
57
57
|
This package is designed to be used with subpath imports, which helps with tree-shaking.
|
|
58
58
|
|
|
59
|
-
- **`@inglorious/utils/
|
|
60
|
-
- **`@inglorious/utils/
|
|
61
|
-
- **`@inglorious/utils/
|
|
62
|
-
|
|
63
|
-
_(This is a sample API. Please update it to reflect the actual exported functions.)_
|
|
59
|
+
- **`@inglorious/utils/algorithms`**: Utilities for AI algorithms, like decision trees and A\*.
|
|
60
|
+
- **`@inglorious/utils/functions`**: Utilities for function composition.
|
|
61
|
+
- **`@inglorious/utils/math`**: Utilities for math operations.
|
|
62
|
+
- **`@inglorious/utils/physics`**: Utilities for calculations on friction, acceleration, and gravity.
|
|
64
63
|
|
|
65
64
|
## Contributing
|
|
66
65
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.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",
|
|
@@ -20,14 +20,14 @@
|
|
|
20
20
|
"utils"
|
|
21
21
|
],
|
|
22
22
|
"type": "module",
|
|
23
|
+
"exports": {
|
|
24
|
+
"./*": "./src/*"
|
|
25
|
+
},
|
|
23
26
|
"files": [
|
|
24
27
|
"src",
|
|
25
28
|
"README.md",
|
|
26
29
|
"LICENSE"
|
|
27
30
|
],
|
|
28
|
-
"exports": {
|
|
29
|
-
"./*": "./src/*"
|
|
30
|
-
},
|
|
31
31
|
"publishConfig": {
|
|
32
32
|
"access": "public"
|
|
33
33
|
},
|
|
@@ -22,6 +22,16 @@ export function contains(arr, item) {
|
|
|
22
22
|
return arr.includes(item)
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
+
/**
|
|
26
|
+
* Ensures that the given value is an array. If it's not an array, it wraps it in one.
|
|
27
|
+
*
|
|
28
|
+
* @param {*} arr - The value to check.
|
|
29
|
+
* @returns {Array} - The original array or the value wrapped in a new array.
|
|
30
|
+
*/
|
|
31
|
+
export function ensureArray(arr) {
|
|
32
|
+
return Array.isArray(arr) ? arr : [arr]
|
|
33
|
+
}
|
|
34
|
+
|
|
25
35
|
/**
|
|
26
36
|
* Finds the maximum item in an array based on a comparator function.
|
|
27
37
|
*
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { expect, test } from "vitest"
|
|
2
2
|
|
|
3
|
-
import { contains, max, min, pop, push, remove } from "./array.js"
|
|
3
|
+
import { contains, ensureArray, max, min, pop, push, remove } from "./array.js"
|
|
4
4
|
|
|
5
5
|
test("it should check if an array contains a value", () => {
|
|
6
6
|
const arr = [3, 2, 6, 1, 7, 4, 5]
|
|
@@ -26,6 +26,18 @@ test("it should check if an array contains an object", () => {
|
|
|
26
26
|
expect(contains(arr, item)).toBe(expectedResult)
|
|
27
27
|
})
|
|
28
28
|
|
|
29
|
+
test("it should return the same array if the value is already an array", () => {
|
|
30
|
+
const arr = [1, "two", { three: 3 }]
|
|
31
|
+
|
|
32
|
+
expect(ensureArray(arr)).toBe(arr)
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
test("it should wrap the value in an array if it is not an array", () => {
|
|
36
|
+
const str = "hello"
|
|
37
|
+
|
|
38
|
+
expect(ensureArray(str)).toStrictEqual([str])
|
|
39
|
+
})
|
|
40
|
+
|
|
29
41
|
test("it should find the maximum value of an array", () => {
|
|
30
42
|
const arr = [3, 2, 6, 1, 7, 4, 5]
|
|
31
43
|
const expectedResult = 7
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ensures that the given value is a Set. If it's not a Set, it creates one from the value.
|
|
3
|
+
*
|
|
4
|
+
* @param {*} value - The value to check.
|
|
5
|
+
* @returns {Set} - The original Set or a new Set created from the value.
|
|
6
|
+
*/
|
|
7
|
+
export function ensureSet(value) {
|
|
8
|
+
return value instanceof Set ? value : new Set(value)
|
|
9
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { expect, test } from "vitest"
|
|
2
|
+
|
|
3
|
+
import { ensureSet } from "./set.js"
|
|
4
|
+
|
|
5
|
+
test("it should return the same array if the value is already an array", () => {
|
|
6
|
+
const set = new Set([1, "two", { three: 3 }])
|
|
7
|
+
|
|
8
|
+
expect(ensureSet(set)).toBe(set)
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
test("it should wrap the value in an array if it is not an array", () => {
|
|
12
|
+
const str = "hello"
|
|
13
|
+
|
|
14
|
+
expect(ensureSet(str)).toStrictEqual(new Set(["h", "e", "l", "o"]))
|
|
15
|
+
})
|
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
} from "./segment.js"
|
|
10
10
|
|
|
11
11
|
expect.extend({
|
|
12
|
-
|
|
12
|
+
toStrictEqualVector(received, expected) {
|
|
13
13
|
const { isNot } = this
|
|
14
14
|
return {
|
|
15
15
|
pass: received.every((coord, index) => isClose(coord, expected[index])),
|
|
@@ -26,7 +26,7 @@ test("it should retrieve values of *a*, *b*, and *c* from a segment, so it looks
|
|
|
26
26
|
}
|
|
27
27
|
const expectedResult = [-2, 1, 0]
|
|
28
28
|
|
|
29
|
-
expect(coefficients(segment)).
|
|
29
|
+
expect(coefficients(segment)).toStrictEqual(expectedResult)
|
|
30
30
|
})
|
|
31
31
|
|
|
32
32
|
test("it should find the closest point in a segment to a point projectable on it", () => {
|
|
@@ -37,7 +37,7 @@ test("it should find the closest point in a segment to a point projectable on it
|
|
|
37
37
|
const point = [2, 0, 0]
|
|
38
38
|
const expectedResult = [1, 0, 1]
|
|
39
39
|
|
|
40
|
-
expect(closestPoint(segment, point)).
|
|
40
|
+
expect(closestPoint(segment, point)).toStrictEqualVector(expectedResult)
|
|
41
41
|
})
|
|
42
42
|
|
|
43
43
|
test("it should find the closest point as the point itself when resting on the segment", () => {
|
|
@@ -48,7 +48,7 @@ test("it should find the closest point as the point itself when resting on the s
|
|
|
48
48
|
const point = [1, 0, 1]
|
|
49
49
|
const expectedResult = [1, 0, 1]
|
|
50
50
|
|
|
51
|
-
expect(closestPoint(segment, point)).
|
|
51
|
+
expect(closestPoint(segment, point)).toStrictEqualVector(expectedResult)
|
|
52
52
|
})
|
|
53
53
|
|
|
54
54
|
test("it return the start of a segment when the point is close to its start", () => {
|
|
@@ -59,7 +59,7 @@ test("it return the start of a segment when the point is close to its start", ()
|
|
|
59
59
|
const point = [-2, 0, 0]
|
|
60
60
|
const expectedResult = [0, 0, 0]
|
|
61
61
|
|
|
62
|
-
expect(closestPoint(segment, point)).
|
|
62
|
+
expect(closestPoint(segment, point)).toStrictEqualVector(expectedResult)
|
|
63
63
|
})
|
|
64
64
|
|
|
65
65
|
test("it should return the end of a segment when the point is close to its end", () => {
|
|
@@ -70,7 +70,7 @@ test("it should return the end of a segment when the point is close to its end",
|
|
|
70
70
|
const point = [5, 0, 4]
|
|
71
71
|
const expectedResult = [3, 0, 4]
|
|
72
72
|
|
|
73
|
-
expect(closestPoint(segment, point)).
|
|
73
|
+
expect(closestPoint(segment, point)).toStrictEqualVector(expectedResult)
|
|
74
74
|
})
|
|
75
75
|
|
|
76
76
|
test("it should compute the distance between a segment and a point projectable on it", () => {
|
|
@@ -169,6 +169,14 @@ export function rotate(vector, angle) {
|
|
|
169
169
|
return is2D ? to2D(result) : result
|
|
170
170
|
}
|
|
171
171
|
|
|
172
|
+
/**
|
|
173
|
+
* Scales the vector by the given scalar. Alias of `multiply`.
|
|
174
|
+
* @param {Vector3} vector - The input vector.
|
|
175
|
+
* @param {number} scalar - The scalar value.
|
|
176
|
+
* @returns {Vector3} The resulting vector.
|
|
177
|
+
*/
|
|
178
|
+
export const scale = multiply
|
|
179
|
+
|
|
172
180
|
/**
|
|
173
181
|
* Sets the angle of the vector while maintaining its magnitude.
|
|
174
182
|
* @param {Vector3} vector - The input vector.
|
|
@@ -181,6 +189,9 @@ export function setAngle(vector, angle) {
|
|
|
181
189
|
return [x, NO_Y, z]
|
|
182
190
|
}
|
|
183
191
|
|
|
192
|
+
/**
|
|
193
|
+
* Sets the length of the vector while maintaining its direction. Alias of `setMagnitude`.
|
|
194
|
+
*/
|
|
184
195
|
export const setLength = setMagnitude
|
|
185
196
|
|
|
186
197
|
/**
|