@inglorious/utils 3.8.0 → 5.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 (73) hide show
  1. package/README.md +83 -3
  2. package/package.json +104 -12
  3. package/src/algorithms/decision-tree.js +3 -3
  4. package/src/algorithms/path-finding.js +6 -6
  5. package/src/data-structures/array.js +12 -0
  6. package/src/data-structures/board.js +97 -15
  7. package/src/data-structures/index.js +1 -2
  8. package/src/data-structures/object.js +164 -2
  9. package/src/data-structures/string.js +77 -0
  10. package/src/data-structures/tree.js +1 -1
  11. package/src/functions/function.js +190 -0
  12. package/src/functions/index.js +1 -4
  13. package/src/math/circle.js +5 -7
  14. package/src/math/hitmask.js +1 -1
  15. package/src/math/index.js +2 -6
  16. package/src/math/line.js +4 -4
  17. package/src/math/point.js +31 -9
  18. package/src/math/quaternion.js +19 -2
  19. package/src/math/rectangle.js +16 -4
  20. package/src/math/rng.js +5 -2
  21. package/src/math/segment.js +11 -5
  22. package/src/math/trigonometry.js +1 -1
  23. package/src/math/vector.js +288 -37
  24. package/src/physics/acceleration.js +2 -3
  25. package/src/physics/friction.js +1 -1
  26. package/src/physics/gravity.js +1 -1
  27. package/src/physics/jump.js +0 -4
  28. package/src/physics/velocity.js +2 -3
  29. package/src/v.js +1 -1
  30. package/types/algorithms/decision-tree.d.ts +27 -0
  31. package/types/algorithms/index.d.ts +2 -0
  32. package/types/algorithms/path-finding.d.ts +69 -0
  33. package/types/data-structures/array.d.ts +61 -0
  34. package/types/data-structures/board.d.ts +121 -0
  35. package/types/data-structures/boolean.d.ts +4 -0
  36. package/types/data-structures/heap.d.ts +69 -0
  37. package/types/data-structures/index.d.ts +8 -0
  38. package/types/data-structures/object.d.ts +237 -0
  39. package/types/data-structures/set.d.ts +4 -0
  40. package/types/data-structures/tree.d.ts +21 -0
  41. package/types/functions/function.d.ts +57 -0
  42. package/types/functions/index.d.ts +1 -0
  43. package/types/index.d.ts +6 -4
  44. package/types/math/circle.d.ts +22 -0
  45. package/types/math/hitmask.d.ts +20 -0
  46. package/types/math/index.d.ts +14 -0
  47. package/types/math/line.d.ts +11 -0
  48. package/types/math/linear-interpolation.d.ts +2 -0
  49. package/types/math/number.d.ts +26 -0
  50. package/types/math/point.d.ts +50 -0
  51. package/types/math/quaternion.d.ts +10 -0
  52. package/types/math/rectangle.d.ts +57 -0
  53. package/types/math/rng.d.ts +14 -0
  54. package/types/math/segment.d.ts +23 -0
  55. package/types/math/statistics.d.ts +11 -0
  56. package/types/math/triangle.d.ts +5 -0
  57. package/types/math/trigonometry.d.ts +26 -0
  58. package/types/math/vector.d.ts +219 -0
  59. package/types/physics/acceleration.d.ts +24 -0
  60. package/types/physics/friction.d.ts +11 -0
  61. package/types/physics/gravity.d.ts +20 -0
  62. package/types/physics/index.d.ts +5 -0
  63. package/types/physics/jump.d.ts +10 -0
  64. package/types/physics/velocity.d.ts +20 -0
  65. package/types/v.d.ts +7 -3
  66. package/src/data-structures/objects.js +0 -113
  67. package/src/functions/functions.js +0 -31
  68. package/src/math/quaternions.js +0 -23
  69. package/src/math/vectors.js +0 -212
  70. package/types/algorithms.d.ts +0 -26
  71. package/types/data-structures.d.ts +0 -53
  72. package/types/math.d.ts +0 -90
  73. /package/src/math/{numbers.js → number.js} +0 -0
@@ -1,113 +0,0 @@
1
- import { isFunction } from "../functions/function.js"
2
- import { isArray } from "./array.js"
3
- import { isObject } from "./object.js"
4
-
5
- /**
6
- * Creates a new object by deeply merging a target object with one or more source objects.
7
- * This function is immutable; it does not modify the original `target` or `sources`.
8
- * Nested objects are merged recursively.
9
- * Arrays from a source object will overwrite arrays in the target object.
10
- *
11
- * @param {Object} target - The base object.
12
- * @param {...Object} sources - The source objects to merge into the target object.
13
- * @returns {Object} - A new object containing the merged properties.
14
- * @see {@link merge} for a mutable version.
15
- */
16
- export function extend(target, ...sources) {
17
- return extendWith(undefined, target, ...sources)
18
- }
19
-
20
- /**
21
- * Merges multiple source objects into a target object.
22
- * This function is mutable; it modifies the `target` object in place.
23
- * Nested objects are merged recursively.
24
- * Arrays from a source object will overwrite arrays in the target object.
25
- *
26
- * @param {Object} target - The target object to merge into.
27
- * @param {...Object} sources - The source objects to merge from.
28
- * @returns {Object} - The merged target object.
29
- * @see {@link extend} for an immutable version.
30
- */
31
- export function merge(target, ...sources) {
32
- return mergeWith(undefined, target, ...sources)
33
- }
34
-
35
- /**
36
- * Creates a new object by deeply merging a target object with one or more source objects,
37
- * using a custom merger function to handle specific properties.
38
- * This function is immutable.
39
- *
40
- * @param {Function} [merger] - A function to customize merging behavior. It receives `(targetValue, sourceValue)`. If it returns `undefined`, the default merge logic is used.
41
- * @param {Object} target - The base object.
42
- * @param {...Object} sources - The source objects to merge.
43
- * @returns {Object} A new object with the merged properties.
44
- */
45
- export function extendWith(merger, target, ...sources) {
46
- return mergeWith(merger, {}, target, ...sources)
47
- }
48
-
49
- /**
50
- * Merges multiple source objects into a target object, using a custom merger function.
51
- * This function is mutable; it modifies the `target` object in place.
52
- *
53
- * @param {Function} [merger] - A function to customize merging behavior. It receives `(targetValue, sourceValue)`. If it returns `undefined`, the default merge logic is used.
54
- * @param {Object} target - The target object to merge into.
55
- * @param {...Object} sources - The source objects to merge from.
56
- * @returns {Object} The merged target object.
57
- */
58
- export function mergeWith(merger, target, ...sources) {
59
- return sources
60
- .filter((source) => source != null)
61
- .reduce((acc, source) => deepMerge(acc, source, merger), target)
62
- }
63
-
64
- /**
65
- * Recursively merges properties from a source object into a target object.
66
- * This is a helper function for `merge` and `extend`.
67
- *
68
- * @param {Object} target - The target object to merge into.
69
- * @param {Object} source - The source object to merge from.
70
- * @param {Function} [merger] - An optional function to customize merging behavior for specific keys.
71
- * @returns {Object} - The modified target object.
72
- */
73
- function deepMerge(target, source, merger) {
74
- for (const [key, value] of Object.entries(source)) {
75
- if (isFunction(merger)) {
76
- const mergedValue = merger(target[key], value)
77
- if (mergedValue !== undefined) {
78
- target[key] = mergedValue
79
- continue
80
- }
81
- }
82
-
83
- if (isArray(value)) {
84
- target[key] = value
85
- } else if (isObject(value)) {
86
- if (!isObject(target[key])) {
87
- target[key] = {}
88
- }
89
- target[key] = deepMerge(target[key], value, merger)
90
- } else {
91
- target[key] = value
92
- }
93
- }
94
- return target
95
- }
96
-
97
- /**
98
- * Assigns default properties to a target object from a source object.
99
- * For each key in `defaultProps`, if `target[key]` is `null` or `undefined`,
100
- * it is set to `defaultProps[key]`.
101
- *
102
- * This function modifies the target object in place.
103
- *
104
- * @param {Object} target The object to apply defaults to.
105
- * @param {Object} defaultProps The object containing the default properties.
106
- * @returns {Object} The modified target object.
107
- */
108
- export function defaults(target, defaultProps) {
109
- for (const key in defaultProps) {
110
- target[key] ??= defaultProps[key]
111
- }
112
- return target
113
- }
@@ -1,31 +0,0 @@
1
- /**
2
- * Composes multiple functions from right to left, as if every function wraps the next one.
3
- * The first function (rightmost) can take multiple arguments; the remaining functions must be unary.
4
- *
5
- * @param {...Function} fns - Functions to compose.
6
- * @returns {Function} A function that takes the initial arguments and applies the composed functions.
7
- */
8
- export function compose(...fns) {
9
- if (!fns.length) {
10
- return (x) => x
11
- }
12
-
13
- const [last, ...rest] = fns.reverse()
14
- return (...args) => rest.reduce((acc, fn) => fn(acc), last(...args))
15
- }
16
-
17
- /**
18
- * Pipes multiple functions from left to right, as if the functions are applied one by one.
19
- * The first function (leftmost) can take multiple arguments; the remaining functions must be unary.
20
- *
21
- * @param {...Function} fns - Functions to pipe.
22
- * @returns {Function} A function that takes the initial arguments and applies the piped functions.
23
- */
24
- export function pipe(...fns) {
25
- if (!fns.length) {
26
- return (x) => x
27
- }
28
-
29
- const [first, ...rest] = fns
30
- return (...args) => rest.reduce((acc, fn) => fn(acc), first(...args))
31
- }
@@ -1,23 +0,0 @@
1
- /**
2
- * @typedef {import("../../types/math").Quaternion} Quaternion
3
- */
4
-
5
- import { v } from "../v.js"
6
- import { times } from "./vector.js"
7
- import { cross, dot, sum } from "./vectors.js"
8
-
9
- /**
10
- * Combines two quaternions using the Hamilton product.
11
- * @param {Quaternion} q1 - The first quaternion.
12
- * @param {Quaternion} q2 - The second quaternion.
13
- * @returns {Quaternion} - The resulting quaternion after combining q1 and q2.
14
- */
15
- export function combine(q1, q2) {
16
- const [s1, ...v1] = q1
17
- const [s2, ...v2] = q2
18
-
19
- return v(
20
- s1 * s2 - dot(v1, v2),
21
- ...sum(times(v2, s1), times(v1, s2), cross(v1, v2)),
22
- )
23
- }
@@ -1,212 +0,0 @@
1
- /**
2
- * @typedef {import("../../types/math").Vector2} Vector2
3
- * @typedef {import("../../types/math").Vector3} Vector3
4
- * @typedef {import("../../types/math").Vector7} Vector7
5
- */
6
-
7
- import { v } from "../v.js"
8
- import { mod as nMod } from "./numbers.js"
9
- import { magnitude, shift } from "./vector.js"
10
-
11
- /**
12
- * Alias for the sum function.
13
- * @type {typeof sum}
14
- */
15
- export const add = sum
16
-
17
- /**
18
- * Computes the cross product of multiple vectors.
19
- * @param {...Vector3 | Vector7} vectors - The vectors to compute the cross product for.
20
- * @returns {Vector3 | Vector7} The resulting vector after the cross product.
21
- */
22
- export function cross(...vectors) {
23
- return v(...vectors.reduce(crossMultiplyCoordinates))
24
- }
25
-
26
- /**
27
- * Computes the distance between multiple vectors.
28
- * @param {...Vector2 | Vector3} vectors - The vectors to compute the distance between.
29
- * @returns {number} The distance between the vectors.
30
- */
31
- export function distance(...vectors) {
32
- return magnitude(subtract(...vectors))
33
- }
34
-
35
- /**
36
- * Computes the component-wise division of multiple vectors.
37
- * @param {...Vector2 | Vector3} vectors - The vectors to divide.
38
- * @returns {Vector2 | Vector3} The resulting vector.
39
- */
40
- export function divide(...vectors) {
41
- return v(...vectors.reduce(divideCoordinates))
42
- }
43
-
44
- /**
45
- * Computes the dot product of multiple vectors.
46
- * @param {...Vector2 | Vector3} vectors - The vectors to compute the dot product for.
47
- * @returns {number} The resulting scalar value of the dot product.
48
- */
49
- export function dot(...vectors) {
50
- return vectors
51
- .reduce(dotMultiplyCoordinates)
52
- .reduce((coord1, coord2) => coord1 + coord2)
53
- }
54
-
55
- /**
56
- * Alias for the multiply function.
57
- * @type {typeof multiply}
58
- */
59
- export const hadamard = multiply
60
-
61
- /**
62
- * Computes the component-wise modulus of multiple vectors.
63
- * @param {...Vector2 | Vector3} vectors - The vectors to compute the modulus for.
64
- * @returns {Vector2 | Vector3} The resulting vector after the modulus.
65
- */
66
- export function mod(...vectors) {
67
- return v(...vectors.reduce(modCoordinates))
68
- }
69
-
70
- /**
71
- * Computes the component-wise multiplication of multiple vectors (Hadamard product).
72
- * @param {...Vector2 | Vector3} vectors - The vectors to multiply.
73
- * @returns {Vector2 | Vector3} The resulting vector.
74
- */
75
- export function multiply(...vectors) {
76
- return v(...vectors.reduce(multiplyCoordinates))
77
- }
78
-
79
- /**
80
- * Computes the component-wise power of multiple vectors.
81
- * @param {...Vector2 | Vector3} vectors - The vectors to compute the power for.
82
- * @returns {Vector2 | Vector3} The resulting vector after the power.
83
- */
84
- export function power(...vectors) {
85
- return v(...vectors.reduce(powerCoordinates))
86
- }
87
-
88
- /**
89
- * Alias for the dot product function.
90
- * @type {typeof dot}
91
- */
92
- export const scalarProduct = dot
93
-
94
- /**
95
- * Subtracts multiple vectors.
96
- * @param {...Vector2 | Vector3} vectors - The vectors to subtract.
97
- * @returns {Vector2 | Vector3} The resulting vector after subtraction.
98
- */
99
- export function subtract(...vectors) {
100
- return v(...vectors.reduce(subtractCoordinates))
101
- }
102
-
103
- /**
104
- * Sums multiple vectors.
105
- * @param {...Vector2 | Vector3} vectors - The vectors to sum.
106
- * @returns {Vector2 | Vector3} The resulting vector after summation.
107
- */
108
- export function sum(...vectors) {
109
- return v(...vectors.reduce(sumCoordinates))
110
- }
111
-
112
- /**
113
- * Alias for the cross product function.
114
- * @type {typeof cross}
115
- */
116
- export const vectorProduct = cross
117
-
118
- /**
119
- * Adds the coordinates of two vectors.
120
- * @param {Vector2 | Vector3} vector1 - The first vector.
121
- * @param {Vector2 | Vector3} vector2 - The second vector.
122
- * @returns {Vector2 | Vector3} The resulting vector after addition.
123
- */
124
- function sumCoordinates(vector1, vector2) {
125
- return v(...vector1.map((coordinate, index) => coordinate + vector2[index]))
126
- }
127
-
128
- /**
129
- * Subtracts the coordinates of two vectors.
130
- * @param {Vector2 | Vector3} vector1 - The first vector.
131
- * @param {Vector2 | Vector3} vector2 - The second vector.
132
- * @returns {Vector2 | Vector3} The resulting vector after subtraction.
133
- */
134
- function subtractCoordinates(vector1, vector2) {
135
- return v(...vector1.map((coordinate, index) => coordinate - vector2[index]))
136
- }
137
-
138
- /**
139
- * Divides the coordinates of two vectors component-wise.
140
- * @param {Vector2 | Vector3} vector1 - The dividend vector.
141
- * @param {Vector2 | Vector3} vector2 - The divisor vector.
142
- * @returns {Vector2 | Vector3} The resulting vector.
143
- */
144
- function divideCoordinates(vector1, vector2) {
145
- return v(...vector1.map((coordinate, index) => coordinate / vector2[index]))
146
- }
147
-
148
- /**
149
- * Computes the component-wise modulus of two vectors.
150
- * @param {Vector2 | Vector3} vector1 - The first vector.
151
- * @param {Vector2 | Vector3} vector2 - The second vector.
152
- * @returns {Vector2 | Vector3} The resulting vector after the modulus.
153
- */
154
- function modCoordinates(vector1, vector2) {
155
- return v(
156
- ...vector1.map((coordinate, index) => nMod(coordinate, vector2[index])),
157
- )
158
- }
159
-
160
- /**
161
- * Multiplies the coordinates of two vectors component-wise.
162
- * @param {Vector2 | Vector3} vector1 - The first vector.
163
- * @param {Vector2 | Vector3} vector2 - The second vector.
164
- * @returns {Vector2 | Vector3} The resulting vector.
165
- */
166
- function multiplyCoordinates(vector1, vector2) {
167
- return v(...vector1.map((coordinate, index) => coordinate * vector2[index]))
168
- }
169
-
170
- /**
171
- * Raises the coordinates of the first vector to the power of the second vector's coordinates.
172
- * @param {Vector2 | Vector3} vector1 - The base vector.
173
- * @param {Vector2 | Vector3} vector2 - The exponent vector.
174
- * @returns {Vector2 | Vector3} The resulting vector.
175
- */
176
- function powerCoordinates(vector1, vector2) {
177
- return v(...vector1.map((coordinate, index) => coordinate ** vector2[index]))
178
- }
179
-
180
- /**
181
- * Computes the cross product of two vectors' coordinates.
182
- * @param {Vector3} vector1 - The first vector.
183
- * @param {Vector3} vector2 - The second vector.
184
- * @returns {Vector3} The resulting vector after the cross product.
185
- */
186
- function crossMultiplyCoordinates(vector1, vector2) {
187
- const indexes = Array(vector1.length)
188
- .fill(null)
189
- .map((_, index) => index)
190
-
191
- return v(
192
- ...indexes.map((_, index) => {
193
- const [index1, index2] = shift(
194
- indexes.filter((_, i) => i !== index),
195
- index,
196
- )
197
- return (
198
- vector1[index1] * vector2[index2] - vector1[index2] * vector2[index1]
199
- )
200
- }),
201
- )
202
- }
203
-
204
- /**
205
- * Multiplies the coordinates of two vectors.
206
- * @param {Vector2 | Vector3} vector1 - The first vector.
207
- * @param {Vector2 | Vector3} vector2 - The second vector.
208
- * @returns {Vector2 | Vector3} The resulting vector containing multiplied coordinates.
209
- */
210
- function dotMultiplyCoordinates(vector1, vector2) {
211
- return v(...vector1.map((coordinate, index) => coordinate * vector2[index]))
212
- }
@@ -1,26 +0,0 @@
1
- /**
2
- * Represents the input conditions to be evaluated by a decision tree node.
3
- * The `unknown` type is used to allow for flexible condition structures.
4
- */
5
- export type Conditions = unknown
6
-
7
- /**
8
- * Represents the result of a decision tree's `test` function, which is used
9
- * as a key to determine the next branch.
10
- */
11
- export type Outcome = string
12
-
13
- /**
14
- * Represents a node in a decision tree. It consists of:
15
- * - A `test` function that evaluates conditions and returns an outcome.
16
- * - A map of outcomes to subsequent actions, which can be either a final outcome
17
- * or another `DecisionTree` node.
18
- */
19
- export type DecisionTree = {
20
- /**
21
- * A function that takes a set of conditions and returns an outcome string.
22
- * @param conditions - The conditions to evaluate.
23
- * @returns The outcome of the test.
24
- */
25
- test: (conditions: Conditions) => Outcome
26
- } & Record<Outcome, () => Outcome | DecisionTree>
@@ -1,53 +0,0 @@
1
- /**
2
- * Represents a generic tree data structure.
3
- * Each node in the tree has a value and an array of child nodes.
4
- * @template T - The type of the value stored in the tree nodes. Defaults to `any`.
5
- */
6
- export interface Tree<T = any> {
7
- /** The value of the current node. */
8
- value: T
9
- /** An array of child `Tree` nodes. */
10
- children: Tree<T>[]
11
- }
12
-
13
- /**
14
- * A map where keys are node identifiers and values are their [x, y] coordinates.
15
- * Used for efficient node lookups by ID.
16
- */
17
- export interface NodeMap {
18
- [id: string]: [number, number] // [x, y]
19
- }
20
-
21
- /**
22
- * Represents a node in a graph.
23
- */
24
- export interface Node {
25
- /** A unique identifier for the node. */
26
- id: string
27
- /** The [x, y] coordinates of the node. */
28
- position: [number, number] // [x, y]
29
- /** An optional cost associated with traversing to this node. */
30
- cost?: number
31
- }
32
-
33
- /**
34
- * Represents a directed edge (or arc) between two nodes in a graph.
35
- */
36
- export interface Arc {
37
- /** The ID of the starting node. */
38
- from: string
39
- /** The ID of the ending node. */
40
- to: string
41
- /** An optional cost associated with traversing this arc. */
42
- cost?: number
43
- }
44
-
45
- /**
46
- * Represents a graph structure consisting of nodes and arcs.
47
- */
48
- export interface Graph {
49
- /** The nodes of the graph, as either an array or a map for efficient lookups. */
50
- nodes: NodeMap | Node[]
51
- /** An array of arcs connecting the nodes. */
52
- arcs: Arc[]
53
- }
package/types/math.d.ts DELETED
@@ -1,90 +0,0 @@
1
- // geometry
2
-
3
- /**
4
- * Represents a point in 3D space using a readonly tuple of numbers.
5
- * @public
6
- */
7
- export type Point = readonly [number, number, number] // [x, y, z]
8
-
9
- /**
10
- * Represents the dimensions of an object in 3D space.
11
- * @public
12
- */
13
- export type Size = readonly [number, number, number] // [width, height, depth]
14
-
15
- /**
16
- * Represents a line in 2D space in the form `ax + by + c = 0`.
17
- * @public
18
- */
19
- export type Line = readonly [number, number, number] // [a, b, c]
20
-
21
- /**
22
- * Represents a circle with a position and radius.
23
- * @public
24
- */
25
- export interface Circle {
26
- /** The center point of the circle. */
27
- position: Point
28
- /** The radius of the circle. */
29
- radius: number
30
- }
31
-
32
- /**
33
- * Represents a rectangle with a position and size.
34
- * @public
35
- */
36
- export interface Rectangle {
37
- /** The top-left corner point of the rectangle. */
38
- position: Point
39
- /** The dimensions of the rectangle. */
40
- size: Size
41
- }
42
-
43
- /**
44
- * Represents a platform, typically for physics or collision detection.
45
- * @public
46
- */
47
- export interface Platform {
48
- /** The position of the platform. */
49
- position: Point
50
- /** The dimensions of the platform. */
51
- size: Size
52
- }
53
-
54
- /**
55
- * Represents a line segment defined by two points.
56
- * @public
57
- */
58
- export interface Segment {
59
- /** The starting point of the segment. */
60
- from: Point
61
- /** The ending point of the segment. */
62
- to: Point
63
- }
64
-
65
- // linear algebra
66
-
67
- /**
68
- * A nominal type brand to identify vector types.
69
- * This prevents accidental assignment of a plain number array to a vector.
70
- * @internal
71
- */
72
- type IsVector = {
73
- readonly __isVector__: true
74
- }
75
-
76
- /** A generic, dynamically-sized vector. */
77
- export type Vector = IsVector & readonly number[]
78
-
79
- /** A 2-dimensional vector. */
80
- export type Vector2 = IsVector & readonly [number, number]
81
-
82
- /** A 3-dimensional vector. */
83
- export type Vector3 = IsVector & readonly [number, number, number]
84
-
85
- /** A 4-dimensional vector, often used for quaternions. */
86
- export type Quaternion = IsVector & readonly [number, number, number, number]
87
-
88
- /** A 7-dimensional vector. */
89
- export type Vector7 = IsVector &
90
- readonly [number, number, number, number, number, number, number]
File without changes