@inglorious/engine 0.2.0 → 0.4.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 (169) hide show
  1. package/README.md +76 -75
  2. package/package.json +14 -25
  3. package/src/{engine/ai → ai}/movement/dynamic/align.js +63 -63
  4. package/src/{engine/ai → ai}/movement/dynamic/arrive.js +42 -42
  5. package/src/{engine/ai → ai}/movement/dynamic/evade.js +38 -38
  6. package/src/{engine/ai → ai}/movement/dynamic/face.js +19 -19
  7. package/src/{engine/ai → ai}/movement/dynamic/flee.js +45 -45
  8. package/src/{engine/ai → ai}/movement/dynamic/look-where-youre-going.js +16 -16
  9. package/src/{engine/ai → ai}/movement/dynamic/match-velocity.js +51 -51
  10. package/src/{engine/ai → ai}/movement/dynamic/pursue.js +38 -38
  11. package/src/{engine/ai → ai}/movement/dynamic/seek.js +44 -44
  12. package/src/{engine/ai → ai}/movement/dynamic/wander.js +31 -31
  13. package/src/{engine/ai → ai}/movement/kinematic/align.js +37 -37
  14. package/src/{engine/ai → ai}/movement/kinematic/arrive.js +42 -42
  15. package/src/{engine/ai → ai}/movement/kinematic/face.js +19 -19
  16. package/src/{engine/ai → ai}/movement/kinematic/flee.js +26 -26
  17. package/src/{engine/ai → ai}/movement/kinematic/seek.js +26 -26
  18. package/src/{engine/ai → ai}/movement/kinematic/seek.test.js +42 -42
  19. package/src/{engine/ai → ai}/movement/kinematic/wander-as-seek.js +31 -31
  20. package/src/{engine/ai → ai}/movement/kinematic/wander.js +27 -27
  21. package/src/{engine/animation → animation}/sprite.js +101 -101
  22. package/src/{engine/animation → animation}/ticker.js +38 -38
  23. package/src/{engine/behaviors → behaviors}/camera.js +68 -68
  24. package/src/{engine/behaviors → behaviors}/controls/dynamic/modern.js +76 -76
  25. package/src/{engine/behaviors → behaviors}/controls/dynamic/shooter.js +84 -84
  26. package/src/{engine/behaviors → behaviors}/controls/dynamic/tank.js +69 -69
  27. package/src/{engine/behaviors → behaviors}/controls/event-handlers.js +17 -17
  28. package/src/{engine/behaviors → behaviors}/controls/kinematic/modern.js +76 -76
  29. package/src/{engine/behaviors → behaviors}/controls/kinematic/shooter.js +82 -82
  30. package/src/{engine/behaviors → behaviors}/controls/kinematic/tank.js +67 -67
  31. package/src/behaviors/debug/collision.js +29 -0
  32. package/src/{engine/behaviors → behaviors}/fps.js +29 -29
  33. package/src/{engine/behaviors → behaviors}/fsm.js +33 -33
  34. package/src/{engine/behaviors → behaviors}/fsm.test.js +49 -49
  35. package/src/{engine/behaviors → behaviors}/game.js +15 -15
  36. package/src/{engine/behaviors → behaviors}/input/controls.js +37 -37
  37. package/src/{engine/behaviors → behaviors}/input/gamepad.js +114 -114
  38. package/src/{engine/behaviors → behaviors}/input/input.js +48 -48
  39. package/src/{engine/behaviors → behaviors}/input/keyboard.js +64 -64
  40. package/src/{engine/behaviors → behaviors}/input/mouse.js +91 -91
  41. package/src/{engine/behaviors → behaviors}/physics/bouncy.js +25 -25
  42. package/src/{engine/behaviors → behaviors}/physics/clamped.js +36 -36
  43. package/src/{engine/behaviors → behaviors}/physics/collidable.js +20 -20
  44. package/src/{engine/behaviors → behaviors}/physics/jumpable.js +145 -145
  45. package/src/{engine/behaviors → behaviors}/ui/button.js +17 -17
  46. package/src/{engine/collision → collision}/detection.js +110 -110
  47. package/src/{engine/core → core}/dev-tools.js +135 -135
  48. package/src/{engine/core → core}/engine.js +119 -119
  49. package/src/{engine/core → core}/loop.js +15 -15
  50. package/src/{engine/core → core}/loops/animation-frame.js +25 -25
  51. package/src/{engine/core → core}/loops/elapsed.js +22 -22
  52. package/src/{engine/core → core}/loops/fixed.js +27 -27
  53. package/src/{engine/core → core}/loops/flash.js +13 -13
  54. package/src/{engine/core → core}/loops/lag.js +26 -26
  55. package/src/main.js +10 -10
  56. package/src/{engine/movement → movement}/dynamic/modern.js +21 -21
  57. package/src/{engine/movement → movement}/dynamic/tank.js +43 -43
  58. package/src/{engine/movement → movement}/kinematic/modern.js +16 -16
  59. package/src/{engine/movement → movement}/kinematic/modern.test.js +27 -27
  60. package/src/{engine/movement → movement}/kinematic/tank.js +27 -27
  61. package/src/{engine/physics → physics}/bounds.js +138 -138
  62. package/src/{engine/physics → physics}/position.js +43 -43
  63. package/src/{engine/physics → physics}/position.test.js +80 -80
  64. package/src/{engine/systems → systems}/sprite-animation.js +27 -27
  65. package/src/engine/behaviors/debug/collision.js +0 -35
  66. package/src/engine/core/api.js +0 -34
  67. package/src/engine/core/select.js +0 -26
  68. package/src/engine/core/store.js +0 -178
  69. package/src/engine/core/store.test.js +0 -110
  70. package/src/renderers/canvas/absolute-position.js +0 -18
  71. package/src/renderers/canvas/camera.js +0 -13
  72. package/src/renderers/canvas/canvas-renderer.js +0 -68
  73. package/src/renderers/canvas/character.js +0 -38
  74. package/src/renderers/canvas/form/button.js +0 -25
  75. package/src/renderers/canvas/fps.js +0 -18
  76. package/src/renderers/canvas/image/hitmask.js +0 -51
  77. package/src/renderers/canvas/image/image.js +0 -34
  78. package/src/renderers/canvas/image/sprite.js +0 -49
  79. package/src/renderers/canvas/image/tilemap.js +0 -66
  80. package/src/renderers/canvas/mouse.js +0 -37
  81. package/src/renderers/canvas/rendering-system.js +0 -79
  82. package/src/renderers/canvas/shapes/circle.js +0 -29
  83. package/src/renderers/canvas/shapes/rectangle.js +0 -27
  84. package/src/renderers/react/game/character/character.module.scss +0 -17
  85. package/src/renderers/react/game/character/index.jsx +0 -20
  86. package/src/renderers/react/game/cursor/cursor.module.scss +0 -47
  87. package/src/renderers/react/game/cursor/index.jsx +0 -20
  88. package/src/renderers/react/game/form/fields/field/field.module.scss +0 -5
  89. package/src/renderers/react/game/form/fields/field/index.jsx +0 -56
  90. package/src/renderers/react/game/form/fields/fields.module.scss +0 -48
  91. package/src/renderers/react/game/form/fields/index.jsx +0 -12
  92. package/src/renderers/react/game/form/form.module.scss +0 -18
  93. package/src/renderers/react/game/form/index.jsx +0 -22
  94. package/src/renderers/react/game/fps/index.jsx +0 -16
  95. package/src/renderers/react/game/game.jsx +0 -72
  96. package/src/renderers/react/game/index.jsx +0 -29
  97. package/src/renderers/react/game/platform/index.jsx +0 -30
  98. package/src/renderers/react/game/platform/platform.module.scss +0 -7
  99. package/src/renderers/react/game/scene/index.jsx +0 -27
  100. package/src/renderers/react/game/scene/scene.module.scss +0 -9
  101. package/src/renderers/react/game/sprite/index.jsx +0 -60
  102. package/src/renderers/react/game/sprite/sprite.module.css +0 -3
  103. package/src/renderers/react/game/stats/index.jsx +0 -22
  104. package/src/renderers/react/hocs/with-absolute-position/index.jsx +0 -20
  105. package/src/renderers/react/hocs/with-absolute-position/with-absolute-position.module.scss +0 -5
  106. package/src/renderers/react/index.jsx +0 -9
  107. package/src/utils/algorithms/decision-tree.js +0 -24
  108. package/src/utils/algorithms/decision-tree.test.js +0 -153
  109. package/src/utils/algorithms/path-finding.js +0 -155
  110. package/src/utils/algorithms/path-finding.test.js +0 -151
  111. package/src/utils/algorithms/types.d.ts +0 -28
  112. package/src/utils/data-structures/array.js +0 -83
  113. package/src/utils/data-structures/array.test.js +0 -173
  114. package/src/utils/data-structures/board.js +0 -159
  115. package/src/utils/data-structures/board.test.js +0 -242
  116. package/src/utils/data-structures/boolean.js +0 -9
  117. package/src/utils/data-structures/heap.js +0 -164
  118. package/src/utils/data-structures/heap.test.js +0 -103
  119. package/src/utils/data-structures/object.js +0 -138
  120. package/src/utils/data-structures/object.test.js +0 -218
  121. package/src/utils/data-structures/objects.js +0 -66
  122. package/src/utils/data-structures/objects.test.js +0 -99
  123. package/src/utils/data-structures/tree.js +0 -36
  124. package/src/utils/data-structures/tree.test.js +0 -33
  125. package/src/utils/data-structures/types.d.ts +0 -4
  126. package/src/utils/functions/functions.js +0 -19
  127. package/src/utils/functions/functions.test.js +0 -23
  128. package/src/utils/math/geometry/circle.js +0 -70
  129. package/src/utils/math/geometry/circle.test.js +0 -97
  130. package/src/utils/math/geometry/hitmask.js +0 -70
  131. package/src/utils/math/geometry/hitmask.test.js +0 -155
  132. package/src/utils/math/geometry/line.js +0 -35
  133. package/src/utils/math/geometry/line.test.js +0 -49
  134. package/src/utils/math/geometry/point.js +0 -78
  135. package/src/utils/math/geometry/point.test.js +0 -81
  136. package/src/utils/math/geometry/rectangle.js +0 -76
  137. package/src/utils/math/geometry/rectangle.test.js +0 -42
  138. package/src/utils/math/geometry/segment.js +0 -80
  139. package/src/utils/math/geometry/segment.test.js +0 -183
  140. package/src/utils/math/geometry/triangle.js +0 -15
  141. package/src/utils/math/geometry/triangle.test.js +0 -11
  142. package/src/utils/math/geometry/types.d.ts +0 -23
  143. package/src/utils/math/linear-algebra/2d.js +0 -28
  144. package/src/utils/math/linear-algebra/2d.test.js +0 -17
  145. package/src/utils/math/linear-algebra/quaternion.js +0 -22
  146. package/src/utils/math/linear-algebra/quaternion.test.js +0 -25
  147. package/src/utils/math/linear-algebra/quaternions.js +0 -20
  148. package/src/utils/math/linear-algebra/quaternions.test.js +0 -29
  149. package/src/utils/math/linear-algebra/types.d.ts +0 -4
  150. package/src/utils/math/linear-algebra/vector.js +0 -327
  151. package/src/utils/math/linear-algebra/vector.test.js +0 -265
  152. package/src/utils/math/linear-algebra/vectors.js +0 -122
  153. package/src/utils/math/linear-algebra/vectors.test.js +0 -65
  154. package/src/utils/math/linear-interpolation.js +0 -9
  155. package/src/utils/math/numbers.js +0 -90
  156. package/src/utils/math/numbers.test.js +0 -137
  157. package/src/utils/math/rng.js +0 -44
  158. package/src/utils/math/rng.test.js +0 -39
  159. package/src/utils/math/statistics.js +0 -43
  160. package/src/utils/math/statistics.test.js +0 -47
  161. package/src/utils/math/trigonometry.js +0 -89
  162. package/src/utils/math/trigonometry.test.js +0 -52
  163. package/src/utils/physics/acceleration.js +0 -61
  164. package/src/utils/physics/friction.js +0 -28
  165. package/src/utils/physics/friction.test.js +0 -42
  166. package/src/utils/physics/gravity.js +0 -69
  167. package/src/utils/physics/gravity.test.js +0 -77
  168. package/src/utils/physics/jump.js +0 -31
  169. package/src/utils/physics/velocity.js +0 -36
@@ -1,151 +0,0 @@
1
- import {
2
- createBoard,
3
- down,
4
- downRight,
5
- right,
6
- } from "@inglorious/utils/data-structures/board.js"
7
- import { expect, test } from "vitest"
8
-
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
- })
@@ -1,28 +0,0 @@
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
- }
@@ -1,83 +0,0 @@
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
- * Finds the maximum item in an array based on a comparator function.
27
- *
28
- * @param {Array} arr - The array to search.
29
- * @param {Function} [comparator=DEFAULT_COMPARATOR] - The comparator function to determine the maximum item.
30
- * @returns {*} - The maximum item in the array.
31
- */
32
- export function max(arr, comparator = DEFAULT_COMPARATOR) {
33
- return arr.reduce((acc, item) =>
34
- comparator(item, acc) < LESS_THAN_ZERO ? item : acc,
35
- )
36
- }
37
-
38
- /**
39
- * Finds the minimum item in an array based on a comparator function.
40
- *
41
- * @param {Array} arr - The array to search.
42
- * @param {Function} [comparator=DEFAULT_COMPARATOR] - The comparator function to determine the minimum item.
43
- * @returns {*} - The minimum item in the array.
44
- */
45
- export function min(arr, comparator = DEFAULT_COMPARATOR) {
46
- return arr.reduce((acc, item) =>
47
- comparator(item, acc) > GREATER_THAN_ZERO ? item : acc,
48
- )
49
- }
50
-
51
- /**
52
- * Removes the smallest item from an array, based on a comparator function.
53
- *
54
- * @param {Array} arr - The array to remove the item from.
55
- * @param {Function} [comparator=DEFAULT_COMPARATOR] - The comparator function to determine the smallest item.
56
- * @returns {*} - A new array with the item removed.
57
- */
58
- export function pop(arr, comparator = DEFAULT_COMPARATOR) {
59
- const item = min(arr, comparator)
60
- return remove(arr, item)
61
- }
62
-
63
- /**
64
- * Adds an item to an array in an immutable way.
65
- *
66
- * @param {Array} arr - The array to add the item to.
67
- * @param {*} item - The item to add.
68
- * @returns {Array} - A new array with the item added.
69
- */
70
- export function push(arr, item) {
71
- return [...arr, item]
72
- }
73
-
74
- /**
75
- * Removes a specific item from an array in an immutable way.
76
- *
77
- * @param {Array} arr - The array to remove the item from.
78
- * @param {*} item - The item to remove.
79
- * @returns {Array} - A new array with the item removed.
80
- */
81
- export function remove(arr, item) {
82
- return arr.filter((el) => el !== item)
83
- }
@@ -1,173 +0,0 @@
1
- import { expect, test } from "vitest"
2
-
3
- import { contains, max, min, pop, push, remove } from "./array.js"
4
-
5
- test("it should check if an array contains a value", () => {
6
- const arr = [3, 2, 6, 1, 7, 4, 5]
7
- const item = 1
8
- const expectedResult = true
9
-
10
- expect(contains(arr, item)).toBe(expectedResult)
11
- })
12
-
13
- test("it should check if an array contains an object", () => {
14
- const arr = [
15
- { value: 3 },
16
- { value: 2 },
17
- { value: 6 },
18
- { value: 1 },
19
- { value: 7 },
20
- { value: 4 },
21
- { value: 5 },
22
- ]
23
- const item = arr[3]
24
- const expectedResult = true
25
-
26
- expect(contains(arr, item)).toBe(expectedResult)
27
- })
28
-
29
- test("it should find the maximum value of an array", () => {
30
- const arr = [3, 2, 6, 1, 7, 4, 5]
31
- const expectedResult = 7
32
-
33
- expect(max(arr)).toBe(expectedResult)
34
- })
35
-
36
- test("it should find the maximum value in an array of objects", () => {
37
- const arr = [
38
- { value: 3 },
39
- { value: 2 },
40
- { value: 6 },
41
- { value: 1 },
42
- { value: 7 },
43
- { value: 4 },
44
- { value: 5 },
45
- ]
46
- const comparator = (a, b) => b.value - a.value
47
- const expectedResult = { value: 7 }
48
-
49
- expect(max(arr, comparator)).toStrictEqual(expectedResult)
50
- })
51
-
52
- test("it should find the minimum value of an array", () => {
53
- const arr = [3, 2, 6, 1, 7, 4, 5]
54
- const expectedResult = 1
55
-
56
- expect(min(arr)).toBe(expectedResult)
57
- })
58
-
59
- test("it should find the minimum value in an array of objects", () => {
60
- const arr = [
61
- { value: 3 },
62
- { value: 2 },
63
- { value: 6 },
64
- { value: 1 },
65
- { value: 7 },
66
- { value: 4 },
67
- { value: 5 },
68
- ]
69
- const comparator = (a, b) => b.value - a.value
70
- const expectedResult = { value: 1 }
71
-
72
- expect(min(arr, comparator)).toStrictEqual(expectedResult)
73
- })
74
-
75
- test("it should remove the minimum value from an array", () => {
76
- const arr = [3, 2, 6, 1, 7, 4, 5]
77
- const expectedResult = [3, 2, 6, 7, 4, 5]
78
-
79
- expect(pop(arr)).toStrictEqual(expectedResult)
80
- })
81
-
82
- test("it should remove the minimum object from an array", () => {
83
- const arr = [
84
- { value: 3 },
85
- { value: 2 },
86
- { value: 6 },
87
- { value: 1 },
88
- { value: 7 },
89
- { value: 4 },
90
- { value: 5 },
91
- ]
92
- const comparator = (a, b) => b.value - a.value
93
- const expectedResult = [
94
- { value: 3 },
95
- { value: 2 },
96
- { value: 6 },
97
- { value: 7 },
98
- { value: 4 },
99
- { value: 5 },
100
- ]
101
-
102
- expect(pop(arr, comparator)).toStrictEqual(expectedResult)
103
- })
104
-
105
- test("it should remove the maximum value from an array", () => {
106
- const arr = [3, 2, 6, 1, 7, 4, 5]
107
- const comparator = (a, b) => a - b
108
- const expectedResult = [3, 2, 6, 1, 4, 5]
109
-
110
- expect(pop(arr, comparator)).toStrictEqual(expectedResult)
111
- })
112
-
113
- test("it should remove the maximum object from an array", () => {
114
- const arr = [
115
- { value: 3 },
116
- { value: 2 },
117
- { value: 6 },
118
- { value: 1 },
119
- { value: 7 },
120
- { value: 4 },
121
- { value: 5 },
122
- ]
123
- const comparator = (a, b) => a.value - b.value
124
- const expectedResult = [
125
- { value: 3 },
126
- { value: 2 },
127
- { value: 6 },
128
- { value: 1 },
129
- { value: 4 },
130
- { value: 5 },
131
- ]
132
-
133
- expect(pop(arr, comparator)).toStrictEqual(expectedResult)
134
- })
135
-
136
- test("it should add a value at the end of the array", () => {
137
- const arr = [3, 2, 6, 1, 7, 4]
138
- const item = 5
139
- const expectedResult = [3, 2, 6, 1, 7, 4, 5]
140
-
141
- expect(push(arr, item)).toStrictEqual(expectedResult)
142
- })
143
-
144
- test("it should remove a primitive value from an array", () => {
145
- const arr = [3, 2, 6, 1, 7, 4, 5]
146
- const item = 1
147
- const expectedResult = [3, 2, 6, 7, 4, 5]
148
-
149
- expect(remove(arr, item)).toStrictEqual(expectedResult)
150
- })
151
-
152
- test("it should remove an object from an array", () => {
153
- const arr = [
154
- { value: 3 },
155
- { value: 2 },
156
- { value: 6 },
157
- { value: 1 },
158
- { value: 7 },
159
- { value: 4 },
160
- { value: 5 },
161
- ]
162
- const item = arr[3]
163
- const expectedResult = [
164
- { value: 3 },
165
- { value: 2 },
166
- { value: 6 },
167
- { value: 7 },
168
- { value: 4 },
169
- { value: 5 },
170
- ]
171
-
172
- expect(remove(arr, item)).toStrictEqual(expectedResult)
173
- })
@@ -1,159 +0,0 @@
1
- /**
2
- * Default filler function for board cells.
3
- * @returns {null} Default value for a cell.
4
- */
5
- const DEFAULT_FILLER = () => null
6
-
7
- /**
8
- * Default function to convert a cell to a string.
9
- * @param {*} cell - The cell value.
10
- * @returns {string} String representation of the cell.
11
- */
12
- const DEFAULT_CELL_TO_STRING = (cell) => `${cell}`
13
-
14
- const FIRST_ROW = 0
15
- const FIRST_COLUMN = 0
16
- const LAST_ROW_OFFSET = 1
17
- const LAST_COLUMN_OFFSET = 1
18
- const MOVE_DOWN = 1
19
- const MOVE_LEFT = -1
20
- const MOVE_RIGHT = 1
21
- const MOVE_UP = -1
22
-
23
- /**
24
- * Calculates the index in a one-dimensional array for given row and column.
25
- * @param {number} row - The row index.
26
- * @param {number} column - The column index.
27
- * @param {number} columns - The total number of columns.
28
- * @returns {number} The calculated index.
29
- */
30
- function getIndex(row, column, columns) {
31
- return row * columns + column
32
- }
33
-
34
- /**
35
- * Creates a 1D board with the specified dimensions and fills it using the provided filler function.
36
- * @param {[number, number]} dimensions - Array containing the number of rows and columns.
37
- * @param {Function} [filler=DEFAULT_FILLER] - Function to fill each cell of the board.
38
- * @returns {any[]} The created board.
39
- */
40
- export function createBoard([rows, columns], filler = DEFAULT_FILLER) {
41
- return new Array(rows * columns).fill(null).map((_, index) => {
42
- const row = Math.floor(index / columns)
43
- const column = index % columns
44
- return filler(row, column)
45
- })
46
- }
47
-
48
- /**
49
- * Moves the given coordinates one step down.
50
- * @param {[number, number]} coords - The current coordinates [row, column].
51
- * @param {[number]} size - Array containing the number of rows.
52
- * @returns {[number, number]} The new coordinates after moving down.
53
- * @throws {Error} If the movement goes out of bounds.
54
- */
55
- export function down([i, j], [rows]) {
56
- if (i === rows - LAST_ROW_OFFSET) {
57
- throw new Error()
58
- }
59
- return [i + MOVE_DOWN, j]
60
- }
61
-
62
- /**
63
- * Moves the given coordinates one step down and one step left.
64
- * @param {[number, number]} coords - The current coordinates [row, column].
65
- * @param {[number, number]} size - Array containing the number of rows and columns.
66
- * @returns {[number, number]} The new coordinates after moving down-left.
67
- */
68
- export function downLeft(coords, size) {
69
- return down(left(coords, size), size)
70
- }
71
-
72
- /**
73
- * Moves the given coordinates one step down and one step right.
74
- * @param {[number, number]} coords - The current coordinates [row, column].
75
- * @param {[number, number]} size - Array containing the number of rows and columns.
76
- * @returns {[number, number]} The new coordinates after moving down-right.
77
- */
78
- export function downRight(coords, size) {
79
- return down(right(coords, size), size)
80
- }
81
-
82
- /**
83
- * Moves the given coordinates one step left.
84
- * @param {[number, number]} coords - The current coordinates [row, column].
85
- * @returns {[number, number]} The new coordinates after moving left.
86
- * @throws {Error} If the movement goes out of bounds.
87
- */
88
- export function left([i, j]) {
89
- if (j === FIRST_COLUMN) {
90
- throw new Error()
91
- }
92
- return [i, j + MOVE_LEFT]
93
- }
94
-
95
- /**
96
- * Moves the given coordinates one step right.
97
- * @param {[number, number]} coords - The current coordinates [row, column].
98
- * @param {[number, number]} size - Array containing the number of rows and columns.
99
- * @returns {[number, number]} The new coordinates after moving right.
100
- * @throws {Error} If the movement goes out of bounds.
101
- */
102
- export function right([i, j], [, columns]) {
103
- if (j === columns - LAST_COLUMN_OFFSET) {
104
- throw new Error()
105
- }
106
- return [i, j + MOVE_RIGHT]
107
- }
108
-
109
- /**
110
- * Converts the board to a string representation.
111
- * @param {any[]} board - The 1D board to convert.
112
- * @param {[number, number]} dimensions - Array containing the number of rows and columns.
113
- * @param {Function} [cellToString=DEFAULT_CELL_TO_STRING] - Function to convert each cell to a string.
114
- * @returns {string} The string representation of the board.
115
- */
116
- export function toString(
117
- board,
118
- [rows, columns],
119
- cellToString = DEFAULT_CELL_TO_STRING,
120
- ) {
121
- return Array.from({ length: rows }, (_, i) =>
122
- Array.from({ length: columns }, (_, j) =>
123
- cellToString(board[getIndex(i, j, columns)], i, j),
124
- ).join(" "),
125
- ).join("\n")
126
- }
127
-
128
- /**
129
- * Moves the given coordinates one step up.
130
- * @param {[number, number]} coords - The current coordinates [row, column].
131
- * @returns {[number, number]} The new coordinates after moving up.
132
- * @throws {Error} If the movement goes out of bounds.
133
- */
134
- export function up([i, j]) {
135
- if (i === FIRST_ROW) {
136
- throw new Error()
137
- }
138
- return [i + MOVE_UP, j]
139
- }
140
-
141
- /**
142
- * Moves the given coordinates one step up and one step left.
143
- * @param {[number, number]} coords - The current coordinates [row, column].
144
- * @param {[number, number]} size - Array containing the number of rows and columns.
145
- * @returns {[number, number]} The new coordinates after moving up-left.
146
- */
147
- export function upLeft(coords, size) {
148
- return up(left(coords, size), size)
149
- }
150
-
151
- /**
152
- * Moves the given coordinates one step up and one step right.
153
- * @param {[number, number]} coords - The current coordinates [row, column].
154
- * @param {[number, number]} size - Array containing the number of rows and columns.
155
- * @returns {[number, number]} The new coordinates after moving up-right.
156
- */
157
- export function upRight(coords, size) {
158
- return up(right(coords, size), size)
159
- }