@inglorious/engine 0.1.1 → 0.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.
Files changed (201) hide show
  1. package/README.md +39 -36
  2. package/package.json +10 -22
  3. package/src/engine/ai/movement/dynamic/align.js +63 -63
  4. package/src/engine/ai/movement/dynamic/arrive.js +42 -43
  5. package/src/engine/ai/movement/dynamic/evade.js +38 -38
  6. package/src/engine/ai/movement/dynamic/face.js +19 -20
  7. package/src/engine/ai/movement/dynamic/flee.js +45 -45
  8. package/src/engine/ai/movement/dynamic/look-where-youre-going.js +16 -17
  9. package/src/engine/ai/movement/dynamic/match-velocity.js +51 -50
  10. package/src/engine/ai/movement/dynamic/pursue.js +38 -38
  11. package/src/engine/ai/movement/dynamic/seek.js +44 -44
  12. package/src/engine/ai/movement/dynamic/wander.js +31 -32
  13. package/src/engine/ai/movement/kinematic/align.js +37 -37
  14. package/src/engine/ai/movement/kinematic/arrive.js +42 -42
  15. package/src/engine/ai/movement/kinematic/face.js +19 -20
  16. package/src/engine/ai/movement/kinematic/flee.js +26 -26
  17. package/src/engine/ai/movement/kinematic/seek.js +26 -26
  18. package/src/engine/ai/movement/kinematic/seek.test.js +42 -42
  19. package/src/engine/ai/movement/kinematic/wander-as-seek.js +31 -31
  20. package/src/engine/ai/movement/kinematic/wander.js +27 -27
  21. package/src/engine/animation/sprite.js +101 -0
  22. package/src/engine/animation/ticker.js +38 -0
  23. package/src/engine/behaviors/camera.js +68 -0
  24. package/src/engine/behaviors/controls/dynamic/modern.js +76 -0
  25. package/src/engine/behaviors/controls/dynamic/shooter.js +84 -0
  26. package/src/engine/behaviors/controls/dynamic/tank.js +69 -0
  27. package/src/engine/behaviors/controls/event-handlers.js +17 -0
  28. package/src/engine/behaviors/controls/kinematic/modern.js +76 -0
  29. package/src/engine/behaviors/controls/kinematic/shooter.js +82 -0
  30. package/src/engine/behaviors/controls/kinematic/tank.js +67 -0
  31. package/src/engine/behaviors/debug/collision.js +35 -0
  32. package/src/engine/behaviors/fps.js +29 -0
  33. package/src/engine/behaviors/fsm.js +33 -0
  34. package/src/{game/decorators → engine/behaviors}/fsm.test.js +49 -56
  35. package/src/engine/behaviors/game.js +15 -0
  36. package/src/engine/behaviors/input/controls.js +37 -0
  37. package/src/engine/behaviors/input/gamepad.js +114 -0
  38. package/src/engine/behaviors/input/input.js +48 -0
  39. package/src/engine/behaviors/input/keyboard.js +64 -0
  40. package/src/engine/behaviors/input/mouse.js +91 -0
  41. package/src/engine/behaviors/physics/bouncy.js +25 -0
  42. package/src/engine/behaviors/physics/clamped.js +36 -0
  43. package/src/{game/decorators/collisions.js → engine/behaviors/physics/collidable.js} +20 -24
  44. package/src/engine/behaviors/physics/jumpable.js +145 -0
  45. package/src/engine/behaviors/ui/button.js +17 -0
  46. package/src/engine/collision/detection.js +110 -115
  47. package/src/engine/core/api.js +34 -0
  48. package/src/engine/core/dev-tools.js +135 -0
  49. package/src/engine/core/engine.js +119 -0
  50. package/src/engine/core/loop.js +15 -0
  51. package/src/engine/{loop → core/loops}/animation-frame.js +25 -26
  52. package/src/engine/{loop → core/loops}/elapsed.js +22 -23
  53. package/src/engine/{loop → core/loops}/fixed.js +27 -28
  54. package/src/engine/{loop → core/loops}/flash.js +13 -14
  55. package/src/engine/{loop → core/loops}/lag.js +26 -27
  56. package/src/engine/core/select.js +26 -0
  57. package/src/engine/core/store.js +178 -0
  58. package/src/engine/core/store.test.js +110 -0
  59. package/src/engine/movement/dynamic/modern.js +21 -24
  60. package/src/engine/movement/dynamic/tank.js +43 -43
  61. package/src/engine/movement/kinematic/modern.js +16 -16
  62. package/src/engine/movement/kinematic/modern.test.js +27 -27
  63. package/src/engine/movement/kinematic/tank.js +27 -27
  64. package/src/engine/physics/bounds.js +138 -0
  65. package/src/engine/physics/position.js +43 -0
  66. package/src/engine/physics/position.test.js +80 -0
  67. package/src/engine/systems/sprite-animation.js +27 -0
  68. package/src/main.js +10 -5
  69. package/src/renderers/canvas/absolute-position.js +18 -0
  70. package/src/renderers/canvas/camera.js +13 -0
  71. package/src/renderers/canvas/canvas-renderer.js +68 -0
  72. package/src/{ui → renderers}/canvas/character.js +38 -35
  73. package/src/{ui → renderers}/canvas/form/button.js +25 -25
  74. package/src/{ui → renderers}/canvas/fps.js +18 -18
  75. package/src/renderers/canvas/image/hitmask.js +51 -0
  76. package/src/{ui → renderers}/canvas/image/image.js +34 -37
  77. package/src/{ui → renderers}/canvas/image/sprite.js +49 -49
  78. package/src/{ui → renderers}/canvas/image/tilemap.js +66 -64
  79. package/src/{ui → renderers}/canvas/mouse.js +37 -37
  80. package/src/renderers/canvas/rendering-system.js +79 -0
  81. package/src/{ui → renderers}/canvas/shapes/circle.js +29 -31
  82. package/src/{ui → renderers}/canvas/shapes/rectangle.js +27 -31
  83. package/src/renderers/react/game/character/index.jsx +20 -0
  84. package/src/{ui → renderers}/react/game/cursor/index.jsx +20 -20
  85. package/src/{ui → renderers}/react/game/form/fields/field/index.jsx +56 -56
  86. package/src/{ui → renderers}/react/game/form/fields/index.jsx +12 -12
  87. package/src/{ui → renderers}/react/game/form/index.jsx +22 -22
  88. package/src/{ui → renderers}/react/game/fps/index.jsx +16 -16
  89. package/src/{ui → renderers}/react/game/game.jsx +72 -71
  90. package/src/{ui → renderers}/react/game/index.jsx +29 -29
  91. package/src/{ui → renderers}/react/game/platform/index.jsx +30 -30
  92. package/src/{ui → renderers}/react/game/scene/index.jsx +27 -25
  93. package/src/{ui → renderers}/react/game/sprite/index.jsx +60 -58
  94. package/src/{ui → renderers}/react/game/stats/index.jsx +22 -22
  95. package/src/{ui → renderers}/react/hocs/with-absolute-position/index.jsx +20 -20
  96. package/src/{ui → renderers}/react/index.jsx +9 -9
  97. package/src/utils/algorithms/decision-tree.js +24 -24
  98. package/src/utils/algorithms/decision-tree.test.js +153 -102
  99. package/src/utils/algorithms/path-finding.js +155 -155
  100. package/src/utils/algorithms/path-finding.test.js +151 -151
  101. package/src/utils/data-structures/array.js +83 -83
  102. package/src/utils/data-structures/array.test.js +173 -173
  103. package/src/utils/data-structures/board.js +159 -159
  104. package/src/utils/data-structures/board.test.js +242 -242
  105. package/src/utils/data-structures/boolean.js +9 -9
  106. package/src/utils/data-structures/heap.js +164 -164
  107. package/src/utils/data-structures/heap.test.js +103 -103
  108. package/src/utils/data-structures/object.js +138 -102
  109. package/src/utils/data-structures/object.test.js +218 -121
  110. package/src/utils/data-structures/objects.js +66 -48
  111. package/src/utils/data-structures/objects.test.js +99 -99
  112. package/src/utils/data-structures/tree.js +36 -36
  113. package/src/utils/data-structures/tree.test.js +33 -33
  114. package/src/utils/functions/functions.js +19 -19
  115. package/src/utils/functions/functions.test.js +23 -23
  116. package/src/utils/math/geometry/circle.js +70 -117
  117. package/src/utils/math/geometry/circle.test.js +97 -97
  118. package/src/utils/math/geometry/hitmask.js +70 -39
  119. package/src/utils/math/geometry/hitmask.test.js +155 -84
  120. package/src/utils/math/geometry/line.js +35 -35
  121. package/src/utils/math/geometry/line.test.js +49 -49
  122. package/src/utils/math/geometry/point.js +78 -71
  123. package/src/utils/math/geometry/point.test.js +81 -81
  124. package/src/utils/math/geometry/rectangle.js +76 -45
  125. package/src/utils/math/geometry/rectangle.test.js +42 -42
  126. package/src/utils/math/geometry/segment.js +80 -80
  127. package/src/utils/math/geometry/segment.test.js +183 -183
  128. package/src/utils/math/geometry/triangle.js +15 -15
  129. package/src/utils/math/geometry/triangle.test.js +11 -11
  130. package/src/utils/math/linear-algebra/2d.js +28 -28
  131. package/src/utils/math/linear-algebra/2d.test.js +17 -17
  132. package/src/utils/math/linear-algebra/quaternion.js +22 -22
  133. package/src/utils/math/linear-algebra/quaternion.test.js +25 -25
  134. package/src/utils/math/linear-algebra/quaternions.js +20 -20
  135. package/src/utils/math/linear-algebra/quaternions.test.js +29 -29
  136. package/src/utils/math/linear-algebra/vector.js +327 -302
  137. package/src/utils/math/linear-algebra/vector.test.js +265 -257
  138. package/src/utils/math/linear-algebra/vectors.js +122 -122
  139. package/src/utils/math/linear-algebra/vectors.test.js +65 -65
  140. package/src/utils/math/linear-interpolation.js +9 -0
  141. package/src/utils/math/numbers.js +90 -90
  142. package/src/utils/math/numbers.test.js +137 -137
  143. package/src/utils/math/rng.js +44 -44
  144. package/src/utils/math/rng.test.js +39 -39
  145. package/src/utils/math/statistics.js +43 -43
  146. package/src/utils/math/statistics.test.js +47 -47
  147. package/src/utils/math/trigonometry.js +89 -89
  148. package/src/utils/math/trigonometry.test.js +52 -52
  149. package/src/utils/physics/acceleration.js +61 -63
  150. package/src/utils/physics/friction.js +28 -30
  151. package/src/utils/physics/friction.test.js +42 -44
  152. package/src/utils/physics/gravity.js +69 -71
  153. package/src/utils/physics/gravity.test.js +77 -80
  154. package/src/utils/physics/jump.js +31 -41
  155. package/src/utils/physics/velocity.js +36 -38
  156. package/src/engine/loop.js +0 -15
  157. package/src/engine/store.js +0 -174
  158. package/src/engine/store.test.js +0 -256
  159. package/src/engine.js +0 -74
  160. package/src/game/animation.js +0 -26
  161. package/src/game/bounds.js +0 -66
  162. package/src/game/decorators/character.js +0 -5
  163. package/src/game/decorators/clamp-to-bounds.js +0 -15
  164. package/src/game/decorators/controls/dynamic/modern.js +0 -48
  165. package/src/game/decorators/controls/dynamic/shooter.js +0 -47
  166. package/src/game/decorators/controls/dynamic/tank.js +0 -55
  167. package/src/game/decorators/controls/kinematic/modern.js +0 -49
  168. package/src/game/decorators/controls/kinematic/shooter.js +0 -45
  169. package/src/game/decorators/controls/kinematic/tank.js +0 -52
  170. package/src/game/decorators/debug/collisions.js +0 -32
  171. package/src/game/decorators/double-jump.js +0 -70
  172. package/src/game/decorators/fps.js +0 -30
  173. package/src/game/decorators/fsm.js +0 -27
  174. package/src/game/decorators/game.js +0 -11
  175. package/src/game/decorators/image/image.js +0 -5
  176. package/src/game/decorators/image/sprite.js +0 -5
  177. package/src/game/decorators/image/tilemap.js +0 -5
  178. package/src/game/decorators/input/controls.js +0 -27
  179. package/src/game/decorators/input/gamepad.js +0 -74
  180. package/src/game/decorators/input/input.js +0 -41
  181. package/src/game/decorators/input/keyboard.js +0 -49
  182. package/src/game/decorators/input/mouse.js +0 -65
  183. package/src/game/decorators/jump.js +0 -72
  184. package/src/game/decorators/platform.js +0 -5
  185. package/src/game/decorators/ui/button.js +0 -21
  186. package/src/game/sprite.js +0 -119
  187. package/src/ui/canvas/absolute-position.js +0 -17
  188. package/src/ui/canvas/image/hitmask.js +0 -37
  189. package/src/ui/canvas.js +0 -81
  190. package/src/ui/react/game/character/index.jsx +0 -30
  191. package/src/utils/math/geometry/platform.js +0 -42
  192. package/src/utils/math/geometry/platform.test.js +0 -133
  193. /package/src/{ui → renderers}/react/game/character/character.module.scss +0 -0
  194. /package/src/{ui → renderers}/react/game/cursor/cursor.module.scss +0 -0
  195. /package/src/{ui → renderers}/react/game/form/fields/field/field.module.scss +0 -0
  196. /package/src/{ui → renderers}/react/game/form/fields/fields.module.scss +0 -0
  197. /package/src/{ui → renderers}/react/game/form/form.module.scss +0 -0
  198. /package/src/{ui → renderers}/react/game/platform/platform.module.scss +0 -0
  199. /package/src/{ui → renderers}/react/game/scene/scene.module.scss +0 -0
  200. /package/src/{ui → renderers}/react/game/sprite/sprite.module.css +0 -0
  201. /package/src/{ui → renderers}/react/hocs/with-absolute-position/with-absolute-position.module.scss +0 -0
@@ -1,102 +1,138 @@
1
- const INITIAL_LEVEL = 0
2
- const NEXT_LEVEL = 2
3
-
4
- /**
5
- * Creates a deep clone of the given object.
6
- * @param {Object} obj - The object to clone.
7
- * @returns {Object} A deep clone of the input object.
8
- */
9
- export function clone(obj) {
10
- return JSON.parse(JSON.stringify(obj))
11
- }
12
-
13
- /**
14
- * Filters the properties of an object based on a callback function.
15
- * @param {Object} obj - The object to filter.
16
- * @param {Function} callback - A function that determines whether a property should be included.
17
- * Receives (key, value, obj) as arguments.
18
- * @returns {Object} A new object with the filtered properties.
19
- */
20
- export function filter(obj, callback) {
21
- return Object.fromEntries(
22
- Object.entries(obj).filter(([key, value], obj) =>
23
- callback(key, value, obj),
24
- ),
25
- )
26
- }
27
-
28
- /**
29
- * Finds the first property in an object that satisfies the callback function.
30
- * @param {Object} obj - The object to search.
31
- * @param {Function} callback - A function that determines whether a property matches.
32
- * Receives (key, value, obj) as arguments.
33
- * @returns {Object} An object containing the first matching property, or an empty object if none match.
34
- */
35
- export function find(obj, callback) {
36
- return Object.fromEntries([
37
- Object.entries(obj).find(([key, value], obj) => callback(key, value, obj)),
38
- ])
39
- }
40
-
41
- /**
42
- * Checks if a value is a plain object.
43
- * @param {*} obj - The value to check.
44
- * @returns {boolean} True if the value is a plain object, false otherwise.
45
- */
46
- export function isObject(obj) {
47
- return obj != null && obj.constructor === Object
48
- }
49
-
50
- /**
51
- * Maps the properties of an object using a callback function.
52
- * @param {Object} obj - The object to map.
53
- * @param {Function} callback - A function that transforms each property.
54
- * Receives (key, value, obj) as arguments.
55
- * @returns {Object} A new object with the mapped properties.
56
- */
57
- export function map(obj, callback) {
58
- return Object.entries(obj).reduce((acc, [key, value]) => {
59
- acc[key] = callback(key, value, obj)
60
- return acc
61
- }, {})
62
- }
63
-
64
- /**
65
- * Converts an object or array to a formatted string representation.
66
- * @param {*} obj - The object or array to convert.
67
- * @param {number} [indentationLevel=INITIAL_LEVEL] - The current indentation level (used for nested structures).
68
- * @returns {string} A string representation of the input object or array.
69
- */
70
- export function toString(obj, indentationLevel = INITIAL_LEVEL) {
71
- if (Array.isArray(obj)) {
72
- return `[
73
- ${obj
74
- .map(
75
- (item) =>
76
- " ".repeat(indentationLevel + NEXT_LEVEL) +
77
- toString(item, indentationLevel + NEXT_LEVEL),
78
- )
79
- .join(",\n")}
80
- ${" ".repeat(indentationLevel)}]`
81
- }
82
-
83
- if (typeof obj === "object" && obj != null) {
84
- return `{
85
- ${Object.entries(obj)
86
- .map(
87
- ([key, value]) =>
88
- `${" ".repeat(indentationLevel + NEXT_LEVEL)}${key}: ${toString(
89
- value,
90
- indentationLevel + NEXT_LEVEL,
91
- )}`,
92
- )
93
- .join(",\n")}
94
- ${" ".repeat(indentationLevel)}}`
95
- }
96
-
97
- if (typeof obj === "string") {
98
- return `"${obj}"`
99
- }
100
-
101
- return obj
102
- }
1
+ const INITIAL_LEVEL = 0
2
+ const NEXT_LEVEL = 2
3
+
4
+ /**
5
+ * Creates a deep clone of the given object.
6
+ * @param {Object} obj - The object to clone.
7
+ * @returns {Object} A deep clone of the input object.
8
+ */
9
+ export function clone(obj) {
10
+ return JSON.parse(JSON.stringify(obj))
11
+ }
12
+
13
+ /**
14
+ * Filters the properties of an object based on a callback function.
15
+ * @param {Object} obj - The object to filter.
16
+ * @param {Function} callback - A function that determines whether a property should be included.
17
+ * Receives (key, value, obj) as arguments.
18
+ * @returns {Object} A new object with the filtered properties.
19
+ */
20
+ export function filter(obj, callback) {
21
+ return Object.fromEntries(
22
+ Object.entries(obj).filter(([key, value], obj) =>
23
+ callback(key, value, obj),
24
+ ),
25
+ )
26
+ }
27
+
28
+ /**
29
+ * Finds the first property in an object that satisfies the callback function.
30
+ * @param {Object} obj - The object to search.
31
+ * @param {Function} callback - A function that determines whether a property matches.
32
+ * Receives (key, value, obj) as arguments.
33
+ * @returns {Object} An object containing the first matching property, or an empty object if none match.
34
+ */
35
+ export function find(obj, callback) {
36
+ return Object.fromEntries([
37
+ Object.entries(obj).find(([key, value], obj) => callback(key, value, obj)),
38
+ ])
39
+ }
40
+
41
+ /**
42
+ * Checks if a value is a plain object.
43
+ * @param {*} obj - The value to check.
44
+ * @returns {boolean} True if the value is a plain object, false otherwise.
45
+ */
46
+ export function isObject(obj) {
47
+ return obj != null && obj.constructor === Object
48
+ }
49
+
50
+ /**
51
+ * Maps the properties of an object using a callback function.
52
+ * @param {Object} obj - The object to map.
53
+ * @param {Function} callback - A function that transforms each property.
54
+ * Receives (key, value, obj) as arguments.
55
+ * @returns {Object} A new object with the mapped properties.
56
+ */
57
+ export function map(obj, callback) {
58
+ return Object.entries(obj).reduce((acc, [key, value]) => {
59
+ acc[key] = callback(key, value, obj)
60
+ return acc
61
+ }, {})
62
+ }
63
+
64
+ /**
65
+ * A utility function inspired by Immer's `produce` API. It provides a convenient
66
+ * way to work with immutable data structures by allowing "mutations" on a
67
+ * temporary draft.
68
+ *
69
+ * **Important:** Unlike Immer, which uses structural sharing via proxies for
70
+ * high performance, this implementation performs a full deep clone of the base
71
+ * state on every call using `JSON.parse(JSON.stringify())`. This can be
72
+ * inefficient for large or complex states. It is intended for simple use cases
73
+ * where the convenience of the API outweighs the performance cost.
74
+ *
75
+ * The recipe function receives a draft copy of the state. It can either
76
+ * mutate the draft and return nothing (`undefined`), or it can return a
77
+ * completely new value, which will become the next state.
78
+ *
79
+ * Can be called in two ways:
80
+ * - **Standard:** `produce(baseState, recipe, ...args)`
81
+ * - **Curried:** `produce(recipe)` returns a new function `(baseState, ...args) => newState`
82
+ *
83
+ * @template T
84
+ * @param {T|function(T, ...*): (T|void)} baseState The initial state, or a recipe for currying.
85
+ * @param {function(T, ...*): (T|void)} [recipe] The recipe function.
86
+ * @param {...*} args Additional arguments to pass to the recipe.
87
+ * @returns {T | function(T, ...*): T} A new state, or a producer function if curried.
88
+ */
89
+ export function produce(baseState, recipe, ...args) {
90
+ if (typeof baseState === "function" && recipe === undefined) {
91
+ const recipeFn = baseState
92
+ return (state, ...recipeArgs) => produce(state, recipeFn, ...recipeArgs)
93
+ }
94
+
95
+ const draft = clone(baseState)
96
+ const result = recipe(draft, ...args)
97
+ return result === undefined ? draft : result
98
+ }
99
+
100
+ /**
101
+ * Converts an object or array to a formatted string representation.
102
+ * @param {*} obj - The object or array to convert.
103
+ * @param {number} [indentationLevel=INITIAL_LEVEL] - The current indentation level (used for nested structures).
104
+ * @returns {string} A string representation of the input object or array.
105
+ */
106
+ export function toString(obj, indentationLevel = INITIAL_LEVEL) {
107
+ if (Array.isArray(obj)) {
108
+ return `[
109
+ ${obj
110
+ .map(
111
+ (item) =>
112
+ " ".repeat(indentationLevel + NEXT_LEVEL) +
113
+ toString(item, indentationLevel + NEXT_LEVEL),
114
+ )
115
+ .join(",\n")}
116
+ ${" ".repeat(indentationLevel)}]`
117
+ }
118
+
119
+ if (typeof obj === "object" && obj != null) {
120
+ return `{
121
+ ${Object.entries(obj)
122
+ .map(
123
+ ([key, value]) =>
124
+ `${" ".repeat(indentationLevel + NEXT_LEVEL)}${key}: ${toString(
125
+ value,
126
+ indentationLevel + NEXT_LEVEL,
127
+ )}`,
128
+ )
129
+ .join(",\n")}
130
+ ${" ".repeat(indentationLevel)}}`
131
+ }
132
+
133
+ if (typeof obj === "string") {
134
+ return `"${obj}"`
135
+ }
136
+
137
+ return obj
138
+ }
@@ -1,121 +1,218 @@
1
- import { expect, test } from "vitest"
2
-
3
- import { clone, filter, find, isObject, map, toString } from "./object.js"
4
-
5
- test("it should deep clone an object", () => {
6
- const obj = {
7
- primitive: 1,
8
- array: [2, 3],
9
- object: { a: 1, b: 2 },
10
- }
11
- const expectedResult = {
12
- primitive: 1,
13
- array: [2, 3],
14
- object: { a: 1, b: 2 },
15
- }
16
-
17
- const result = clone(obj)
18
-
19
- expect(result).toStrictEqual(expectedResult)
20
- expect(result).not.toBe(expectedResult)
21
- expect(result.primitive).toBe(expectedResult.primitive)
22
- expect(result.array).not.toBe(expectedResult.array)
23
- expect(result.object).not.toBe(expectedResult.object)
24
- })
25
-
26
- test("it should behave like Array.prototype.filter, but on an object", () => {
27
- const obj = {
28
- key1: "value1",
29
- key2: "value2",
30
- key3: "value3",
31
- }
32
- const callback = (key) => ["key2", "key3"].includes(key)
33
- const expectedResult = {
34
- key2: "value2",
35
- key3: "value3",
36
- }
37
-
38
- expect(filter(obj, callback)).toStrictEqual(expectedResult)
39
- })
40
-
41
- test("it should behave like Array.prototype.find, but on an object", () => {
42
- const obj = {
43
- key1: "value1",
44
- key2: "value2",
45
- key3: "value3",
46
- }
47
- const callback = (key) => ["key2", "key3"].includes(key)
48
- const expectedResult = {
49
- key2: "value2",
50
- }
51
-
52
- expect(find(obj, callback)).toStrictEqual(expectedResult)
53
- })
54
-
55
- test("it correctly should check if a value is an object", () => {
56
- expect(isObject(1)).toBe(false)
57
- expect(isObject("a")).toBe(false)
58
- expect(isObject([])).toBe(false)
59
- expect(isObject(null)).toBe(false)
60
- expect(isObject(new Date())).toBe(false)
61
- expect(isObject({})).toBe(true)
62
- })
63
-
64
- test("it should behave like Array.prototype.map, but on an object", () => {
65
- const obj = {
66
- key1: "value1",
67
- key2: "value2",
68
- key3: "value3",
69
- }
70
- const callback = (key, value) => value.toUpperCase()
71
- const expectedResult = {
72
- key1: "VALUE1",
73
- key2: "VALUE2",
74
- key3: "VALUE3",
75
- }
76
-
77
- expect(map(obj, callback)).toStrictEqual(expectedResult)
78
- })
79
-
80
- test("it should return a string representation of a shallow object", () => {
81
- const obj = {
82
- key1: "value1",
83
- key2: "value2",
84
- key3: "value3",
85
- }
86
-
87
- expect(toString(obj)).toBe(`{
88
- key1: "value1",
89
- key2: "value2",
90
- key3: "value3"
91
- }`)
92
- })
93
-
94
- test("it should return a string representation of a nested object", () => {
95
- const obj = {
96
- a: 1,
97
- b: [7, 3],
98
- c: { d: 4, h: 8 },
99
- e: [{ f: 5, i: 9 }],
100
- g: 6,
101
- }
102
-
103
- expect(toString(obj)).toBe(`{
104
- a: 1,
105
- b: [
106
- 7,
107
- 3
108
- ],
109
- c: {
110
- d: 4,
111
- h: 8
112
- },
113
- e: [
114
- {
115
- f: 5,
116
- i: 9
117
- }
118
- ],
119
- g: 6
120
- }`)
121
- })
1
+ import { expect, test } from "vitest"
2
+
3
+ import {
4
+ clone,
5
+ filter,
6
+ find,
7
+ isObject,
8
+ map,
9
+ produce,
10
+ toString,
11
+ } from "./object.js"
12
+
13
+ test("it should deep clone an object", () => {
14
+ const obj = {
15
+ primitive: 1,
16
+ array: [2, 3],
17
+ object: { a: 1, b: 2 },
18
+ }
19
+ const expectedResult = {
20
+ primitive: 1,
21
+ array: [2, 3],
22
+ object: { a: 1, b: 2 },
23
+ }
24
+
25
+ const result = clone(obj)
26
+
27
+ expect(result).toStrictEqual(expectedResult)
28
+ expect(result).not.toBe(expectedResult)
29
+ expect(result.primitive).toBe(expectedResult.primitive)
30
+ expect(result.array).not.toBe(expectedResult.array)
31
+ expect(result.object).not.toBe(expectedResult.object)
32
+ })
33
+
34
+ test("it should behave like Array.prototype.filter, but on an object", () => {
35
+ const obj = {
36
+ key1: "value1",
37
+ key2: "value2",
38
+ key3: "value3",
39
+ }
40
+ const callback = (key) => ["key2", "key3"].includes(key)
41
+ const expectedResult = {
42
+ key2: "value2",
43
+ key3: "value3",
44
+ }
45
+
46
+ expect(filter(obj, callback)).toStrictEqual(expectedResult)
47
+ })
48
+
49
+ test("it should behave like Array.prototype.find, but on an object", () => {
50
+ const obj = {
51
+ key1: "value1",
52
+ key2: "value2",
53
+ key3: "value3",
54
+ }
55
+ const callback = (key) => ["key2", "key3"].includes(key)
56
+ const expectedResult = {
57
+ key2: "value2",
58
+ }
59
+
60
+ expect(find(obj, callback)).toStrictEqual(expectedResult)
61
+ })
62
+
63
+ test("it correctly should check if a value is an object", () => {
64
+ expect(isObject(1)).toBe(false)
65
+ expect(isObject("a")).toBe(false)
66
+ expect(isObject([])).toBe(false)
67
+ expect(isObject(null)).toBe(false)
68
+ expect(isObject(new Date())).toBe(false)
69
+ expect(isObject({})).toBe(true)
70
+ })
71
+
72
+ test("it should behave like Array.prototype.map, but on an object", () => {
73
+ const obj = {
74
+ key1: "value1",
75
+ key2: "value2",
76
+ key3: "value3",
77
+ }
78
+ const callback = (key, value) => value.toUpperCase()
79
+ const expectedResult = {
80
+ key1: "VALUE1",
81
+ key2: "VALUE2",
82
+ key3: "VALUE3",
83
+ }
84
+
85
+ expect(map(obj, callback)).toStrictEqual(expectedResult)
86
+ })
87
+
88
+ test("it should produce a new state without mutating the original", () => {
89
+ const baseState = {
90
+ a: 1,
91
+ b: {
92
+ c: [2, 3],
93
+ d: { e: 4 },
94
+ },
95
+ f: 5,
96
+ }
97
+
98
+ const recipe = (draft) => {
99
+ draft.a = 10
100
+ draft.b.c.push(4)
101
+ draft.b.d.e = 40
102
+ draft.g = 6
103
+ }
104
+
105
+ const expectedState = {
106
+ a: 10,
107
+ b: {
108
+ c: [2, 3, 4],
109
+ d: { e: 40 },
110
+ },
111
+ f: 5,
112
+ g: 6,
113
+ }
114
+
115
+ const originalBaseState = JSON.parse(JSON.stringify(baseState))
116
+
117
+ const nextState = produce(baseState, recipe)
118
+
119
+ expect(nextState).toStrictEqual(expectedState)
120
+ expect(nextState).not.toBe(baseState)
121
+ expect(baseState).toStrictEqual(originalBaseState)
122
+ })
123
+
124
+ test("it should support currying to produce a new state", () => {
125
+ const baseState = {
126
+ a: 1,
127
+ b: {
128
+ c: [2, 3],
129
+ d: { e: 4 },
130
+ },
131
+ f: 5,
132
+ }
133
+ const originalStateCopy = clone(baseState)
134
+
135
+ const recipe = (draft) => {
136
+ draft.a = 10
137
+ draft.b.c.push(4)
138
+ draft.g = 6
139
+ }
140
+
141
+ const expectedState = {
142
+ a: 10,
143
+ b: {
144
+ c: [2, 3, 4],
145
+ d: { e: 4 },
146
+ },
147
+ f: 5,
148
+ g: 6,
149
+ }
150
+
151
+ const producer = produce(recipe)
152
+ const nextState = producer(baseState)
153
+
154
+ expect(nextState).toStrictEqual(expectedState)
155
+ expect(nextState).not.toBe(baseState)
156
+ expect(baseState).toStrictEqual(originalStateCopy)
157
+ })
158
+
159
+ test("it should pass extra arguments to the recipe", () => {
160
+ const baseState = { value: 1 }
161
+ const recipe = (draft, increment, multiplier) => {
162
+ draft.value = (draft.value + increment) * multiplier
163
+ }
164
+
165
+ // Test non-curried version
166
+ const nextStateUncurried = produce(baseState, recipe, 2, 3) // (1 + 2) * 3 = 9
167
+ expect(nextStateUncurried.value).toBe(9)
168
+
169
+ // Test curried version
170
+ const producer = produce(recipe)
171
+ const nextStateCurried = producer(baseState, 4, 5) // (1 + 4) * 5 = 25
172
+ expect(nextStateCurried.value).toBe(25)
173
+
174
+ expect(baseState.value).toBe(1)
175
+ })
176
+
177
+ test("it should return a string representation of a shallow object", () => {
178
+ const obj = {
179
+ key1: "value1",
180
+ key2: "value2",
181
+ key3: "value3",
182
+ }
183
+
184
+ expect(toString(obj)).toBe(`{
185
+ key1: "value1",
186
+ key2: "value2",
187
+ key3: "value3"
188
+ }`)
189
+ })
190
+
191
+ test("it should return a string representation of a nested object", () => {
192
+ const obj = {
193
+ a: 1,
194
+ b: [7, 3],
195
+ c: { d: 4, h: 8 },
196
+ e: [{ f: 5, i: 9 }],
197
+ g: 6,
198
+ }
199
+
200
+ expect(toString(obj)).toBe(`{
201
+ a: 1,
202
+ b: [
203
+ 7,
204
+ 3
205
+ ],
206
+ c: {
207
+ d: 4,
208
+ h: 8
209
+ },
210
+ e: [
211
+ {
212
+ f: 5,
213
+ i: 9
214
+ }
215
+ ],
216
+ g: 6
217
+ }`)
218
+ })