@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.
- package/README.md +39 -36
- package/package.json +10 -22
- package/src/engine/ai/movement/dynamic/align.js +63 -63
- package/src/engine/ai/movement/dynamic/arrive.js +42 -43
- package/src/engine/ai/movement/dynamic/evade.js +38 -38
- package/src/engine/ai/movement/dynamic/face.js +19 -20
- package/src/engine/ai/movement/dynamic/flee.js +45 -45
- package/src/engine/ai/movement/dynamic/look-where-youre-going.js +16 -17
- package/src/engine/ai/movement/dynamic/match-velocity.js +51 -50
- package/src/engine/ai/movement/dynamic/pursue.js +38 -38
- package/src/engine/ai/movement/dynamic/seek.js +44 -44
- package/src/engine/ai/movement/dynamic/wander.js +31 -32
- package/src/engine/ai/movement/kinematic/align.js +37 -37
- package/src/engine/ai/movement/kinematic/arrive.js +42 -42
- package/src/engine/ai/movement/kinematic/face.js +19 -20
- package/src/engine/ai/movement/kinematic/flee.js +26 -26
- package/src/engine/ai/movement/kinematic/seek.js +26 -26
- package/src/engine/ai/movement/kinematic/seek.test.js +42 -42
- package/src/engine/ai/movement/kinematic/wander-as-seek.js +31 -31
- package/src/engine/ai/movement/kinematic/wander.js +27 -27
- package/src/engine/animation/sprite.js +101 -0
- package/src/engine/animation/ticker.js +38 -0
- package/src/engine/behaviors/camera.js +68 -0
- package/src/engine/behaviors/controls/dynamic/modern.js +76 -0
- package/src/engine/behaviors/controls/dynamic/shooter.js +84 -0
- package/src/engine/behaviors/controls/dynamic/tank.js +69 -0
- package/src/engine/behaviors/controls/event-handlers.js +17 -0
- package/src/engine/behaviors/controls/kinematic/modern.js +76 -0
- package/src/engine/behaviors/controls/kinematic/shooter.js +82 -0
- package/src/engine/behaviors/controls/kinematic/tank.js +67 -0
- package/src/engine/behaviors/debug/collision.js +35 -0
- package/src/engine/behaviors/fps.js +29 -0
- package/src/engine/behaviors/fsm.js +33 -0
- package/src/{game/decorators → engine/behaviors}/fsm.test.js +49 -56
- package/src/engine/behaviors/game.js +15 -0
- package/src/engine/behaviors/input/controls.js +37 -0
- package/src/engine/behaviors/input/gamepad.js +114 -0
- package/src/engine/behaviors/input/input.js +48 -0
- package/src/engine/behaviors/input/keyboard.js +64 -0
- package/src/engine/behaviors/input/mouse.js +91 -0
- package/src/engine/behaviors/physics/bouncy.js +25 -0
- package/src/engine/behaviors/physics/clamped.js +36 -0
- package/src/{game/decorators/collisions.js → engine/behaviors/physics/collidable.js} +20 -24
- package/src/engine/behaviors/physics/jumpable.js +145 -0
- package/src/engine/behaviors/ui/button.js +17 -0
- package/src/engine/collision/detection.js +110 -115
- package/src/engine/core/api.js +34 -0
- package/src/engine/core/dev-tools.js +135 -0
- package/src/engine/core/engine.js +119 -0
- package/src/engine/core/loop.js +15 -0
- package/src/engine/{loop → core/loops}/animation-frame.js +25 -26
- package/src/engine/{loop → core/loops}/elapsed.js +22 -23
- package/src/engine/{loop → core/loops}/fixed.js +27 -28
- package/src/engine/{loop → core/loops}/flash.js +13 -14
- package/src/engine/{loop → core/loops}/lag.js +26 -27
- package/src/engine/core/select.js +26 -0
- package/src/engine/core/store.js +178 -0
- package/src/engine/core/store.test.js +110 -0
- package/src/engine/movement/dynamic/modern.js +21 -24
- package/src/engine/movement/dynamic/tank.js +43 -43
- package/src/engine/movement/kinematic/modern.js +16 -16
- package/src/engine/movement/kinematic/modern.test.js +27 -27
- package/src/engine/movement/kinematic/tank.js +27 -27
- package/src/engine/physics/bounds.js +138 -0
- package/src/engine/physics/position.js +43 -0
- package/src/engine/physics/position.test.js +80 -0
- package/src/engine/systems/sprite-animation.js +27 -0
- package/src/main.js +10 -5
- package/src/renderers/canvas/absolute-position.js +18 -0
- package/src/renderers/canvas/camera.js +13 -0
- package/src/renderers/canvas/canvas-renderer.js +68 -0
- package/src/{ui → renderers}/canvas/character.js +38 -35
- package/src/{ui → renderers}/canvas/form/button.js +25 -25
- package/src/{ui → renderers}/canvas/fps.js +18 -18
- package/src/renderers/canvas/image/hitmask.js +51 -0
- package/src/{ui → renderers}/canvas/image/image.js +34 -37
- package/src/{ui → renderers}/canvas/image/sprite.js +49 -49
- package/src/{ui → renderers}/canvas/image/tilemap.js +66 -64
- package/src/{ui → renderers}/canvas/mouse.js +37 -37
- package/src/renderers/canvas/rendering-system.js +79 -0
- package/src/{ui → renderers}/canvas/shapes/circle.js +29 -31
- package/src/{ui → renderers}/canvas/shapes/rectangle.js +27 -31
- package/src/renderers/react/game/character/index.jsx +20 -0
- package/src/{ui → renderers}/react/game/cursor/index.jsx +20 -20
- package/src/{ui → renderers}/react/game/form/fields/field/index.jsx +56 -56
- package/src/{ui → renderers}/react/game/form/fields/index.jsx +12 -12
- package/src/{ui → renderers}/react/game/form/index.jsx +22 -22
- package/src/{ui → renderers}/react/game/fps/index.jsx +16 -16
- package/src/{ui → renderers}/react/game/game.jsx +72 -71
- package/src/{ui → renderers}/react/game/index.jsx +29 -29
- package/src/{ui → renderers}/react/game/platform/index.jsx +30 -30
- package/src/{ui → renderers}/react/game/scene/index.jsx +27 -25
- package/src/{ui → renderers}/react/game/sprite/index.jsx +60 -58
- package/src/{ui → renderers}/react/game/stats/index.jsx +22 -22
- package/src/{ui → renderers}/react/hocs/with-absolute-position/index.jsx +20 -20
- package/src/{ui → renderers}/react/index.jsx +9 -9
- package/src/utils/algorithms/decision-tree.js +24 -24
- package/src/utils/algorithms/decision-tree.test.js +153 -102
- package/src/utils/algorithms/path-finding.js +155 -155
- package/src/utils/algorithms/path-finding.test.js +151 -151
- package/src/utils/data-structures/array.js +83 -83
- package/src/utils/data-structures/array.test.js +173 -173
- package/src/utils/data-structures/board.js +159 -159
- package/src/utils/data-structures/board.test.js +242 -242
- package/src/utils/data-structures/boolean.js +9 -9
- package/src/utils/data-structures/heap.js +164 -164
- package/src/utils/data-structures/heap.test.js +103 -103
- package/src/utils/data-structures/object.js +138 -102
- package/src/utils/data-structures/object.test.js +218 -121
- package/src/utils/data-structures/objects.js +66 -48
- package/src/utils/data-structures/objects.test.js +99 -99
- package/src/utils/data-structures/tree.js +36 -36
- package/src/utils/data-structures/tree.test.js +33 -33
- package/src/utils/functions/functions.js +19 -19
- package/src/utils/functions/functions.test.js +23 -23
- package/src/utils/math/geometry/circle.js +70 -117
- package/src/utils/math/geometry/circle.test.js +97 -97
- package/src/utils/math/geometry/hitmask.js +70 -39
- package/src/utils/math/geometry/hitmask.test.js +155 -84
- package/src/utils/math/geometry/line.js +35 -35
- package/src/utils/math/geometry/line.test.js +49 -49
- package/src/utils/math/geometry/point.js +78 -71
- package/src/utils/math/geometry/point.test.js +81 -81
- package/src/utils/math/geometry/rectangle.js +76 -45
- package/src/utils/math/geometry/rectangle.test.js +42 -42
- package/src/utils/math/geometry/segment.js +80 -80
- package/src/utils/math/geometry/segment.test.js +183 -183
- package/src/utils/math/geometry/triangle.js +15 -15
- package/src/utils/math/geometry/triangle.test.js +11 -11
- package/src/utils/math/linear-algebra/2d.js +28 -28
- package/src/utils/math/linear-algebra/2d.test.js +17 -17
- package/src/utils/math/linear-algebra/quaternion.js +22 -22
- package/src/utils/math/linear-algebra/quaternion.test.js +25 -25
- package/src/utils/math/linear-algebra/quaternions.js +20 -20
- package/src/utils/math/linear-algebra/quaternions.test.js +29 -29
- package/src/utils/math/linear-algebra/vector.js +327 -302
- package/src/utils/math/linear-algebra/vector.test.js +265 -257
- package/src/utils/math/linear-algebra/vectors.js +122 -122
- package/src/utils/math/linear-algebra/vectors.test.js +65 -65
- package/src/utils/math/linear-interpolation.js +9 -0
- package/src/utils/math/numbers.js +90 -90
- package/src/utils/math/numbers.test.js +137 -137
- package/src/utils/math/rng.js +44 -44
- package/src/utils/math/rng.test.js +39 -39
- package/src/utils/math/statistics.js +43 -43
- package/src/utils/math/statistics.test.js +47 -47
- package/src/utils/math/trigonometry.js +89 -89
- package/src/utils/math/trigonometry.test.js +52 -52
- package/src/utils/physics/acceleration.js +61 -63
- package/src/utils/physics/friction.js +28 -30
- package/src/utils/physics/friction.test.js +42 -44
- package/src/utils/physics/gravity.js +69 -71
- package/src/utils/physics/gravity.test.js +77 -80
- package/src/utils/physics/jump.js +31 -41
- package/src/utils/physics/velocity.js +36 -38
- package/src/engine/loop.js +0 -15
- package/src/engine/store.js +0 -174
- package/src/engine/store.test.js +0 -256
- package/src/engine.js +0 -74
- package/src/game/animation.js +0 -26
- package/src/game/bounds.js +0 -66
- package/src/game/decorators/character.js +0 -5
- package/src/game/decorators/clamp-to-bounds.js +0 -15
- package/src/game/decorators/controls/dynamic/modern.js +0 -48
- package/src/game/decorators/controls/dynamic/shooter.js +0 -47
- package/src/game/decorators/controls/dynamic/tank.js +0 -55
- package/src/game/decorators/controls/kinematic/modern.js +0 -49
- package/src/game/decorators/controls/kinematic/shooter.js +0 -45
- package/src/game/decorators/controls/kinematic/tank.js +0 -52
- package/src/game/decorators/debug/collisions.js +0 -32
- package/src/game/decorators/double-jump.js +0 -70
- package/src/game/decorators/fps.js +0 -30
- package/src/game/decorators/fsm.js +0 -27
- package/src/game/decorators/game.js +0 -11
- package/src/game/decorators/image/image.js +0 -5
- package/src/game/decorators/image/sprite.js +0 -5
- package/src/game/decorators/image/tilemap.js +0 -5
- package/src/game/decorators/input/controls.js +0 -27
- package/src/game/decorators/input/gamepad.js +0 -74
- package/src/game/decorators/input/input.js +0 -41
- package/src/game/decorators/input/keyboard.js +0 -49
- package/src/game/decorators/input/mouse.js +0 -65
- package/src/game/decorators/jump.js +0 -72
- package/src/game/decorators/platform.js +0 -5
- package/src/game/decorators/ui/button.js +0 -21
- package/src/game/sprite.js +0 -119
- package/src/ui/canvas/absolute-position.js +0 -17
- package/src/ui/canvas/image/hitmask.js +0 -37
- package/src/ui/canvas.js +0 -81
- package/src/ui/react/game/character/index.jsx +0 -30
- package/src/utils/math/geometry/platform.js +0 -42
- package/src/utils/math/geometry/platform.test.js +0 -133
- /package/src/{ui → renderers}/react/game/character/character.module.scss +0 -0
- /package/src/{ui → renderers}/react/game/cursor/cursor.module.scss +0 -0
- /package/src/{ui → renderers}/react/game/form/fields/field/field.module.scss +0 -0
- /package/src/{ui → renderers}/react/game/form/fields/fields.module.scss +0 -0
- /package/src/{ui → renderers}/react/game/form/form.module.scss +0 -0
- /package/src/{ui → renderers}/react/game/platform/platform.module.scss +0 -0
- /package/src/{ui → renderers}/react/game/scene/scene.module.scss +0 -0
- /package/src/{ui → renderers}/react/game/sprite/sprite.module.css +0 -0
- /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
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
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 {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
expect(
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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
|
+
})
|