@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.
- package/README.md +10 -11
- package/package.json +4 -4
- package/src/algorithms/decision-tree.js +24 -0
- package/src/algorithms/decision-tree.test.js +153 -0
- package/src/algorithms/path-finding.js +150 -0
- package/src/algorithms/path-finding.test.js +151 -0
- package/src/algorithms/types.d.ts +28 -0
- package/src/data-structures/array.js +93 -0
- package/src/data-structures/array.test.js +185 -0
- package/src/data-structures/board.js +159 -0
- package/src/data-structures/board.test.js +242 -0
- package/src/data-structures/boolean.js +9 -0
- package/src/data-structures/heap.js +164 -0
- package/src/data-structures/heap.test.js +103 -0
- package/src/data-structures/object.js +138 -0
- package/src/data-structures/object.test.js +218 -0
- package/src/data-structures/objects.js +66 -0
- package/src/data-structures/objects.test.js +99 -0
- package/src/data-structures/set.js +9 -0
- package/src/data-structures/set.test.js +15 -0
- package/src/data-structures/tree.js +36 -0
- package/src/data-structures/tree.test.js +33 -0
- package/src/data-structures/types.d.ts +4 -0
- package/src/functions/functions.js +19 -0
- package/src/functions/functions.test.js +23 -0
- package/src/math/geometry/circle.js +69 -0
- package/src/math/geometry/circle.test.js +97 -0
- package/src/math/geometry/hitmask.js +70 -0
- package/src/math/geometry/hitmask.test.js +155 -0
- package/src/math/geometry/line.js +34 -0
- package/src/math/geometry/line.test.js +49 -0
- package/src/math/geometry/point.js +78 -0
- package/src/math/geometry/point.test.js +81 -0
- package/src/math/geometry/rectangle.js +76 -0
- package/src/math/geometry/rectangle.test.js +42 -0
- package/src/math/geometry/segment.js +72 -0
- package/src/math/geometry/segment.test.js +183 -0
- package/src/math/geometry/triangle.js +15 -0
- package/src/math/geometry/triangle.test.js +11 -0
- package/src/math/geometry/types.d.ts +23 -0
- package/src/math/linear-algebra/2d.js +28 -0
- package/src/math/linear-algebra/2d.test.js +17 -0
- package/src/math/linear-algebra/quaternion.js +22 -0
- package/src/math/linear-algebra/quaternion.test.js +25 -0
- package/src/math/linear-algebra/quaternions.js +20 -0
- package/src/math/linear-algebra/quaternions.test.js +29 -0
- package/src/math/linear-algebra/types.d.ts +4 -0
- package/src/math/linear-algebra/vector.js +337 -0
- package/src/math/linear-algebra/vector.test.js +265 -0
- package/src/math/linear-algebra/vectors.js +122 -0
- package/src/math/linear-algebra/vectors.test.js +65 -0
- package/src/math/linear-interpolation.js +9 -0
- package/src/math/numbers.js +90 -0
- package/src/math/numbers.test.js +137 -0
- package/src/math/rng.js +44 -0
- package/src/math/rng.test.js +39 -0
- package/src/math/statistics.js +43 -0
- package/src/math/statistics.test.js +47 -0
- package/src/math/trigonometry.js +89 -0
- package/src/math/trigonometry.test.js +52 -0
- package/src/physics/acceleration.js +56 -0
- package/src/physics/friction.js +24 -0
- package/src/physics/friction.test.js +42 -0
- package/src/physics/gravity.js +69 -0
- package/src/physics/gravity.test.js +77 -0
- package/src/physics/jump.js +31 -0
- package/src/physics/velocity.js +32 -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
|
-
"./*": "./*"
|
|
30
|
-
},
|
|
31
31
|
"publishConfig": {
|
|
32
32
|
"access": "public"
|
|
33
33
|
},
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {import("./types").Conditions} Conditions
|
|
3
|
+
* @typedef {import("./types").Outcome} Outcome
|
|
4
|
+
* @typedef {import("./types").DecisionTree} DecisionTree
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Traverses a decision tree recursively based on the provided conditions and returns the resulting node.
|
|
9
|
+
*
|
|
10
|
+
* @param {DecisionTree} tree - The decision tree to traverse. Each node contains:
|
|
11
|
+
* - `test` {Function}: A function that evaluates the conditions and determines the next branch.
|
|
12
|
+
* - Branches keyed by the possible outcomes of the `test` function.
|
|
13
|
+
* @param {Conditions} conditions - The conditions object passed to the `test` function at each node.
|
|
14
|
+
* @returns {Outcome | DecisionTree} - The resulting outcome or decision tree itself after traversing the decision tree.
|
|
15
|
+
*/
|
|
16
|
+
export function decide(tree, conditions) {
|
|
17
|
+
if (!tree.test) {
|
|
18
|
+
return tree
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const value = tree.test(conditions)
|
|
22
|
+
|
|
23
|
+
return tree[value] && decide(tree[value](conditions), conditions)
|
|
24
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { expect, test } from "vitest"
|
|
2
|
+
|
|
3
|
+
import { length } from "../math/linear-algebra/vector.js"
|
|
4
|
+
import { subtract } from "../math/linear-algebra/vectors.js"
|
|
5
|
+
import { decide } from "./decision-tree.js"
|
|
6
|
+
|
|
7
|
+
test("it should make a decision based on a binary decision tree", () => {
|
|
8
|
+
const entity = { state: "sleeping", position: [0, 0, 0] }
|
|
9
|
+
const target = { position: [10, 0, 0] }
|
|
10
|
+
const tree = {
|
|
11
|
+
test({ entity }) {
|
|
12
|
+
return entity.state === "idle"
|
|
13
|
+
},
|
|
14
|
+
true() {
|
|
15
|
+
return {
|
|
16
|
+
test({ entity, target }) {
|
|
17
|
+
const distance = length(subtract(target.position, entity.position))
|
|
18
|
+
return distance < 250
|
|
19
|
+
},
|
|
20
|
+
true() {
|
|
21
|
+
return "aware"
|
|
22
|
+
},
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
false() {
|
|
26
|
+
return {
|
|
27
|
+
test({ entity }) {
|
|
28
|
+
return entity.state === "chasing"
|
|
29
|
+
},
|
|
30
|
+
true() {
|
|
31
|
+
return {
|
|
32
|
+
test({ entity, target }) {
|
|
33
|
+
const distance = length(
|
|
34
|
+
subtract(target.position, entity.position),
|
|
35
|
+
)
|
|
36
|
+
return distance >= 250
|
|
37
|
+
},
|
|
38
|
+
true() {
|
|
39
|
+
return "idle"
|
|
40
|
+
},
|
|
41
|
+
false() {
|
|
42
|
+
return {
|
|
43
|
+
test({ entity, target }) {
|
|
44
|
+
const distance = length(
|
|
45
|
+
subtract(target.position, entity.position),
|
|
46
|
+
)
|
|
47
|
+
return distance < 10
|
|
48
|
+
},
|
|
49
|
+
true() {
|
|
50
|
+
return "sleepy"
|
|
51
|
+
},
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
false() {
|
|
57
|
+
return {
|
|
58
|
+
test({ entity }) {
|
|
59
|
+
return ["sleepy", "sleeping"].includes(entity.state)
|
|
60
|
+
},
|
|
61
|
+
true() {
|
|
62
|
+
return {
|
|
63
|
+
test({ entity, target }) {
|
|
64
|
+
const distance = length(
|
|
65
|
+
subtract(target.position, entity.position),
|
|
66
|
+
)
|
|
67
|
+
return distance >= 10
|
|
68
|
+
},
|
|
69
|
+
true() {
|
|
70
|
+
return "aware"
|
|
71
|
+
},
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const state = decide(tree, { entity, target })
|
|
81
|
+
|
|
82
|
+
expect(state).toBe("aware")
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
test("it should make a decision on a multi-child tree", () => {
|
|
86
|
+
const entity = { state: "sleeping", position: [0, 0, 0] }
|
|
87
|
+
const target = { position: [10, 0, 0] }
|
|
88
|
+
const wakeUp = () => ({
|
|
89
|
+
test({ entity, target }) {
|
|
90
|
+
const distance = length(subtract(target.position, entity.position))
|
|
91
|
+
return distance >= 10
|
|
92
|
+
},
|
|
93
|
+
true() {
|
|
94
|
+
return "aware"
|
|
95
|
+
},
|
|
96
|
+
false({ entity }) {
|
|
97
|
+
return entity.state
|
|
98
|
+
},
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
const tree = {
|
|
102
|
+
test({ entity }) {
|
|
103
|
+
return entity.state
|
|
104
|
+
},
|
|
105
|
+
idle() {
|
|
106
|
+
return {
|
|
107
|
+
test({ entity, target }) {
|
|
108
|
+
const distance = length(subtract(target.position, entity.position))
|
|
109
|
+
return distance < 250
|
|
110
|
+
},
|
|
111
|
+
true() {
|
|
112
|
+
return "aware"
|
|
113
|
+
},
|
|
114
|
+
false({ entity }) {
|
|
115
|
+
return entity.state
|
|
116
|
+
},
|
|
117
|
+
}
|
|
118
|
+
},
|
|
119
|
+
chasing() {
|
|
120
|
+
return {
|
|
121
|
+
test({ entity, target }) {
|
|
122
|
+
const distance = length(subtract(target.position, entity.position))
|
|
123
|
+
return distance >= 250
|
|
124
|
+
},
|
|
125
|
+
true() {
|
|
126
|
+
return "idle"
|
|
127
|
+
},
|
|
128
|
+
false() {
|
|
129
|
+
return {
|
|
130
|
+
test({ entity, target }) {
|
|
131
|
+
const distance = length(
|
|
132
|
+
subtract(target.position, entity.position),
|
|
133
|
+
)
|
|
134
|
+
return distance < 10
|
|
135
|
+
},
|
|
136
|
+
true() {
|
|
137
|
+
return "sleepy"
|
|
138
|
+
},
|
|
139
|
+
false({ entity }) {
|
|
140
|
+
return entity.state
|
|
141
|
+
},
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
}
|
|
145
|
+
},
|
|
146
|
+
sleepy: wakeUp,
|
|
147
|
+
sleeping: wakeUp,
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const state = decide(tree, { entity, target })
|
|
151
|
+
|
|
152
|
+
expect(state).toBe("aware")
|
|
153
|
+
})
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {import("./types").Node} Node
|
|
3
|
+
* @typedef {import("./types").Graph} Graph
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { contains, push, remove, root } from "../data-structures/heap.js"
|
|
7
|
+
import { abs, magnitude } from "../math/linear-algebra/vector.js"
|
|
8
|
+
import { subtract } from "../math/linear-algebra/vectors.js"
|
|
9
|
+
|
|
10
|
+
const NO_COST = 0
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Calculates the cost of a path using Dijkstra's algorithm.
|
|
14
|
+
* In this simplified form the cost is simply 0.
|
|
15
|
+
*
|
|
16
|
+
* @returns {number} - The cost of the path (which is 0).
|
|
17
|
+
*/
|
|
18
|
+
export const dijkstra = () => NO_COST
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Calculates the Euclidean distance between two nodes.
|
|
22
|
+
*
|
|
23
|
+
* @param {Node} a - The first node.
|
|
24
|
+
* @param {Node} b - The second node.
|
|
25
|
+
* @returns {number} - The Euclidean distance between the two nodes.
|
|
26
|
+
*/
|
|
27
|
+
export const eucledianDistance = (a, b) =>
|
|
28
|
+
magnitude(subtract(a.position, b.position))
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Calculates the Manhattan distance between two nodes.
|
|
32
|
+
*
|
|
33
|
+
* @param {Node} a - The first node.
|
|
34
|
+
* @param {Node} b - The second node.
|
|
35
|
+
* @returns {number} - The Manhattan distance between the two nodes.
|
|
36
|
+
*/
|
|
37
|
+
export const manhattanDistance = (a, b) => abs(subtract(a.position, b.position))
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Compares the cost of two nodes.
|
|
41
|
+
*
|
|
42
|
+
* @param {Node} a - The first node.
|
|
43
|
+
* @param {Node} b - The second node.
|
|
44
|
+
* @returns {number} - The difference in cost between the two nodes.
|
|
45
|
+
*/
|
|
46
|
+
const compareCost = (a, b) => b.cost - a.cost
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Compares the total cost of two nodes.
|
|
50
|
+
*
|
|
51
|
+
* @param {Node} a - The first node.
|
|
52
|
+
* @param {Node} b - The second node.
|
|
53
|
+
* @returns {number} - The difference in total cost between the two nodes.
|
|
54
|
+
*/
|
|
55
|
+
const compareTotalCost = (a, b) => b.totalCost - a.totalCost
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Finds the shortest path in a graph from a start node to an end node using a heuristic.
|
|
59
|
+
*
|
|
60
|
+
* @param {Graph} graph - The graph containing nodes and arcs.
|
|
61
|
+
* @param {string} start - The ID of the start node.
|
|
62
|
+
* @param {string} end - The ID of the end node.
|
|
63
|
+
* @param {(a: Node, b: Node) => number} [heuristic=eucledianDistance] - The heuristic function to estimate the cost.
|
|
64
|
+
* @returns {string[]} - An array of node IDs representing the shortest path.
|
|
65
|
+
*/
|
|
66
|
+
export function findPath(graph, start, end, heuristic = eucledianDistance) {
|
|
67
|
+
const { nodes, arcs } = adaptGraph(graph)
|
|
68
|
+
const findNode = createFindNode(nodes)
|
|
69
|
+
|
|
70
|
+
const startNode = findNode(start)
|
|
71
|
+
const endNode = findNode(end)
|
|
72
|
+
let discoveredNodes = [startNode]
|
|
73
|
+
let evaluatedNodes = []
|
|
74
|
+
let path = []
|
|
75
|
+
|
|
76
|
+
while (discoveredNodes.length) {
|
|
77
|
+
let current = root(discoveredNodes, compareTotalCost)
|
|
78
|
+
|
|
79
|
+
if (current.id === end) {
|
|
80
|
+
while (current != null) {
|
|
81
|
+
path = [current.id, ...path]
|
|
82
|
+
current = current.previous
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return path
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
discoveredNodes = remove(discoveredNodes, current)
|
|
89
|
+
evaluatedNodes = push(evaluatedNodes, current)
|
|
90
|
+
|
|
91
|
+
const outgoingArcs = arcs.filter(({ from }) => from === current.id)
|
|
92
|
+
const destinations = outgoingArcs.map(({ to }) => findNode(to))
|
|
93
|
+
|
|
94
|
+
for (const destination of destinations) {
|
|
95
|
+
const totalCost =
|
|
96
|
+
current.cost +
|
|
97
|
+
destination.cost +
|
|
98
|
+
heuristic(current, destination) +
|
|
99
|
+
outgoingArcs.find(({ to }) => to === destination.id)?.cost
|
|
100
|
+
|
|
101
|
+
let isNewPathFound = false
|
|
102
|
+
if (contains(discoveredNodes, destination)) {
|
|
103
|
+
if (totalCost < destination.cost) {
|
|
104
|
+
destination.cost = totalCost
|
|
105
|
+
isNewPathFound = true
|
|
106
|
+
}
|
|
107
|
+
} else {
|
|
108
|
+
destination.cost = totalCost
|
|
109
|
+
isNewPathFound = true
|
|
110
|
+
discoveredNodes = push(discoveredNodes, destination, compareCost)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (isNewPathFound) {
|
|
114
|
+
destination.totalCost =
|
|
115
|
+
destination.cost + heuristic(destination, endNode)
|
|
116
|
+
destination.previous = current
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Adapts a graph to ensure all nodes and arcs have a cost property.
|
|
124
|
+
*
|
|
125
|
+
* @param {Graph} graph - The graph to adapt.
|
|
126
|
+
* @returns {Graph} - The adapted graph with cost properties added.
|
|
127
|
+
*/
|
|
128
|
+
function adaptGraph(graph) {
|
|
129
|
+
return {
|
|
130
|
+
...graph,
|
|
131
|
+
nodes: Array.isArray(graph.nodes)
|
|
132
|
+
? graph.nodes.map((node) => ({ ...node, cost: node.cost ?? NO_COST }))
|
|
133
|
+
: Object.entries(graph.nodes).map(([key, value]) => ({
|
|
134
|
+
id: key,
|
|
135
|
+
position: value,
|
|
136
|
+
cost: 0,
|
|
137
|
+
})),
|
|
138
|
+
arcs: graph.arcs.map((arc) => ({ ...arc, cost: arc.cost ?? NO_COST })),
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Creates a function that finds a node by its ID.
|
|
144
|
+
*
|
|
145
|
+
* @param {Node[]} nodes - The array of nodes.
|
|
146
|
+
* @returns {(id: string) => Node} - A function that finds a node by its ID.
|
|
147
|
+
*/
|
|
148
|
+
function createFindNode(nodes) {
|
|
149
|
+
return (id) => nodes.find((node) => node.id === id)
|
|
150
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { expect, test } from "vitest"
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
createBoard,
|
|
5
|
+
down,
|
|
6
|
+
downRight,
|
|
7
|
+
right,
|
|
8
|
+
} from "../data-structures/board.js"
|
|
9
|
+
import { findPath } from "./path-finding.js"
|
|
10
|
+
|
|
11
|
+
test("it should find the shortest path in a trivial example", () => {
|
|
12
|
+
const graph = {
|
|
13
|
+
nodes: {
|
|
14
|
+
A: [0, 0],
|
|
15
|
+
B: [2, 0],
|
|
16
|
+
},
|
|
17
|
+
arcs: [{ from: "A", to: "B", cost: 2 }],
|
|
18
|
+
}
|
|
19
|
+
const start = "A"
|
|
20
|
+
const end = "B"
|
|
21
|
+
const expectedResult = ["A", "B"]
|
|
22
|
+
|
|
23
|
+
expect(findPath(graph, start, end)).toStrictEqual(expectedResult)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
test("it should find the shortest path between two nodes with default heuristic", () => {
|
|
27
|
+
const graph = {
|
|
28
|
+
nodes: {
|
|
29
|
+
A: [0, 0],
|
|
30
|
+
B: [2, 0],
|
|
31
|
+
C: [3, 2],
|
|
32
|
+
D: [4, 4],
|
|
33
|
+
E: [1, 1],
|
|
34
|
+
F: [2, 3],
|
|
35
|
+
G: [0, 4],
|
|
36
|
+
},
|
|
37
|
+
arcs: [
|
|
38
|
+
{ from: "A", to: "B" },
|
|
39
|
+
{ from: "B", to: "C" },
|
|
40
|
+
{ from: "C", to: "D" },
|
|
41
|
+
{ from: "A", to: "E" },
|
|
42
|
+
{ from: "E", to: "F" },
|
|
43
|
+
{ from: "F", to: "G" },
|
|
44
|
+
{ from: "G", to: "D" },
|
|
45
|
+
],
|
|
46
|
+
}
|
|
47
|
+
const start = "A"
|
|
48
|
+
const end = "D"
|
|
49
|
+
const expectedResult = ["A", "B", "C", "D"]
|
|
50
|
+
|
|
51
|
+
expect(findPath(graph, start, end)).toStrictEqual(expectedResult)
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
test("it should find the shortest path between two nodes with node costs", () => {
|
|
55
|
+
const graph = {
|
|
56
|
+
nodes: [
|
|
57
|
+
{ id: "A", position: [0, 0], cost: 0 },
|
|
58
|
+
{ id: "B", position: [2, 0], cost: 7 },
|
|
59
|
+
{ id: "C", position: [3, 2], cost: 3 },
|
|
60
|
+
{ id: "D", position: [4, 4], cost: 2 },
|
|
61
|
+
{ id: "E", position: [1, 1], cost: 1.5 },
|
|
62
|
+
{ id: "F", position: [2, 3], cost: 2 },
|
|
63
|
+
{ id: "G", position: [0, 4], cost: 3 },
|
|
64
|
+
],
|
|
65
|
+
arcs: [
|
|
66
|
+
{ from: "A", to: "B" },
|
|
67
|
+
{ from: "B", to: "C" },
|
|
68
|
+
{ from: "C", to: "D" },
|
|
69
|
+
{ from: "A", to: "E" },
|
|
70
|
+
{ from: "E", to: "F" },
|
|
71
|
+
{ from: "F", to: "G" },
|
|
72
|
+
{ from: "G", to: "D" },
|
|
73
|
+
],
|
|
74
|
+
}
|
|
75
|
+
const start = "A"
|
|
76
|
+
const end = "D"
|
|
77
|
+
const expectedResult = ["A", "E", "F", "G", "D"]
|
|
78
|
+
|
|
79
|
+
expect(findPath(graph, start, end)).toStrictEqual(expectedResult)
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
test("it should find the shortest path between two nodes with arc costs", () => {
|
|
83
|
+
const graph = {
|
|
84
|
+
nodes: {
|
|
85
|
+
A: [0, 0],
|
|
86
|
+
B: [2, 0],
|
|
87
|
+
C: [3, 2],
|
|
88
|
+
D: [4, 4],
|
|
89
|
+
E: [1, 1],
|
|
90
|
+
F: [2, 3],
|
|
91
|
+
G: [0, 4],
|
|
92
|
+
},
|
|
93
|
+
arcs: [
|
|
94
|
+
{ from: "A", to: "B", cost: 7 },
|
|
95
|
+
{ from: "B", to: "C", cost: 3 },
|
|
96
|
+
{ from: "C", to: "D", cost: 2 },
|
|
97
|
+
{ from: "A", to: "E", cost: 1.5 },
|
|
98
|
+
{ from: "E", to: "F", cost: 2 },
|
|
99
|
+
{ from: "F", to: "G", cost: 3 },
|
|
100
|
+
{ from: "G", to: "D", cost: 4 },
|
|
101
|
+
],
|
|
102
|
+
}
|
|
103
|
+
const start = "A"
|
|
104
|
+
const end = "D"
|
|
105
|
+
const expectedResult = ["A", "E", "F", "G", "D"]
|
|
106
|
+
|
|
107
|
+
expect(findPath(graph, start, end)).toStrictEqual(expectedResult)
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
test("it should find the best path in a board graph", () => {
|
|
111
|
+
const rows = 5
|
|
112
|
+
const columns = 5
|
|
113
|
+
const size = [rows, columns]
|
|
114
|
+
const filler = (i, j) => ({ id: `${i}${j}`, position: [i, j] })
|
|
115
|
+
const board = createBoard(size, filler)
|
|
116
|
+
|
|
117
|
+
const graph = {
|
|
118
|
+
nodes: board,
|
|
119
|
+
arcs: [],
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
for (let i = 0; i < rows; i++) {
|
|
123
|
+
for (let j = 0; j < columns; j++) {
|
|
124
|
+
const current = `${i}${j}`
|
|
125
|
+
if (i + 1 < rows) {
|
|
126
|
+
graph.arcs.push({
|
|
127
|
+
from: current,
|
|
128
|
+
to: down([i, j], size).join(""),
|
|
129
|
+
})
|
|
130
|
+
}
|
|
131
|
+
if (j + 1 < columns) {
|
|
132
|
+
graph.arcs.push({
|
|
133
|
+
from: current,
|
|
134
|
+
to: right([i, j], size).join(""),
|
|
135
|
+
})
|
|
136
|
+
}
|
|
137
|
+
if (i + 1 < rows && j + 1 < columns) {
|
|
138
|
+
graph.arcs.push({
|
|
139
|
+
from: current,
|
|
140
|
+
to: downRight([i, j], size).join(""),
|
|
141
|
+
})
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const start = "00"
|
|
147
|
+
const end = "44"
|
|
148
|
+
const expectedResult = ["00", "11", "22", "33", "44"]
|
|
149
|
+
|
|
150
|
+
expect(findPath(graph, start, end)).toStrictEqual(expectedResult)
|
|
151
|
+
})
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export type Conditions = unknown
|
|
2
|
+
export type Outcome = string | boolean
|
|
3
|
+
|
|
4
|
+
export interface DecisionTree {
|
|
5
|
+
test: (conditions: Conditions) => Outcome
|
|
6
|
+
[outcome: Outcome]: () => Outcome | DecisionTree
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
interface NodeMap {
|
|
10
|
+
[id: string]: [number, number] // [x, y]
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface Node {
|
|
14
|
+
id: string
|
|
15
|
+
position: [number, number] // [x, y]
|
|
16
|
+
cost?: number
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface Arc {
|
|
20
|
+
from: string
|
|
21
|
+
to: string
|
|
22
|
+
cost?: number
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface Graph {
|
|
26
|
+
nodes: NodeMap | Node[]
|
|
27
|
+
arcs: Arc[]
|
|
28
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A default comparator function that compares two numbers.
|
|
3
|
+
*
|
|
4
|
+
* @param {number} a - The first number.
|
|
5
|
+
* @param {number} b - The second number.
|
|
6
|
+
* @returns {number} - A positive number if `a` is less than `b`, a negative number if `a` is greater than `b`, or 0 if they are equal.
|
|
7
|
+
*/
|
|
8
|
+
const DEFAULT_COMPARATOR = (a, b) => b - a
|
|
9
|
+
|
|
10
|
+
// Threshold constants for comparison results
|
|
11
|
+
const LESS_THAN_ZERO = 0
|
|
12
|
+
const GREATER_THAN_ZERO = 0
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Checks if an array contains a specific item.
|
|
16
|
+
*
|
|
17
|
+
* @param {Array} arr - The array to search.
|
|
18
|
+
* @param {*} item - The item to check for.
|
|
19
|
+
* @returns {boolean} - `true` if the item is found, otherwise `false`.
|
|
20
|
+
*/
|
|
21
|
+
export function contains(arr, item) {
|
|
22
|
+
return arr.includes(item)
|
|
23
|
+
}
|
|
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
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Finds the maximum item in an array based on a comparator function.
|
|
37
|
+
*
|
|
38
|
+
* @param {Array} arr - The array to search.
|
|
39
|
+
* @param {Function} [comparator=DEFAULT_COMPARATOR] - The comparator function to determine the maximum item.
|
|
40
|
+
* @returns {*} - The maximum item in the array.
|
|
41
|
+
*/
|
|
42
|
+
export function max(arr, comparator = DEFAULT_COMPARATOR) {
|
|
43
|
+
return arr.reduce((acc, item) =>
|
|
44
|
+
comparator(item, acc) < LESS_THAN_ZERO ? item : acc,
|
|
45
|
+
)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Finds the minimum item in an array based on a comparator function.
|
|
50
|
+
*
|
|
51
|
+
* @param {Array} arr - The array to search.
|
|
52
|
+
* @param {Function} [comparator=DEFAULT_COMPARATOR] - The comparator function to determine the minimum item.
|
|
53
|
+
* @returns {*} - The minimum item in the array.
|
|
54
|
+
*/
|
|
55
|
+
export function min(arr, comparator = DEFAULT_COMPARATOR) {
|
|
56
|
+
return arr.reduce((acc, item) =>
|
|
57
|
+
comparator(item, acc) > GREATER_THAN_ZERO ? item : acc,
|
|
58
|
+
)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Removes the smallest item from an array, based on a comparator function.
|
|
63
|
+
*
|
|
64
|
+
* @param {Array} arr - The array to remove the item from.
|
|
65
|
+
* @param {Function} [comparator=DEFAULT_COMPARATOR] - The comparator function to determine the smallest item.
|
|
66
|
+
* @returns {*} - A new array with the item removed.
|
|
67
|
+
*/
|
|
68
|
+
export function pop(arr, comparator = DEFAULT_COMPARATOR) {
|
|
69
|
+
const item = min(arr, comparator)
|
|
70
|
+
return remove(arr, item)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Adds an item to an array in an immutable way.
|
|
75
|
+
*
|
|
76
|
+
* @param {Array} arr - The array to add the item to.
|
|
77
|
+
* @param {*} item - The item to add.
|
|
78
|
+
* @returns {Array} - A new array with the item added.
|
|
79
|
+
*/
|
|
80
|
+
export function push(arr, item) {
|
|
81
|
+
return [...arr, item]
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Removes a specific item from an array in an immutable way.
|
|
86
|
+
*
|
|
87
|
+
* @param {Array} arr - The array to remove the item from.
|
|
88
|
+
* @param {*} item - The item to remove.
|
|
89
|
+
* @returns {Array} - A new array with the item removed.
|
|
90
|
+
*/
|
|
91
|
+
export function remove(arr, item) {
|
|
92
|
+
return arr.filter((el) => el !== item)
|
|
93
|
+
}
|