@inglorious/engine 0.2.0 → 0.3.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 +75 -75
- package/package.json +13 -23
- package/src/{engine/ai → ai}/movement/dynamic/align.js +63 -63
- package/src/{engine/ai → ai}/movement/dynamic/arrive.js +42 -42
- package/src/{engine/ai → ai}/movement/dynamic/evade.js +38 -38
- package/src/{engine/ai → ai}/movement/dynamic/face.js +19 -19
- package/src/{engine/ai → ai}/movement/dynamic/flee.js +45 -45
- package/src/{engine/ai → ai}/movement/dynamic/look-where-youre-going.js +16 -16
- package/src/{engine/ai → ai}/movement/dynamic/match-velocity.js +51 -51
- package/src/{engine/ai → ai}/movement/dynamic/pursue.js +38 -38
- package/src/{engine/ai → ai}/movement/dynamic/seek.js +44 -44
- package/src/{engine/ai → ai}/movement/dynamic/wander.js +31 -31
- package/src/{engine/ai → ai}/movement/kinematic/align.js +37 -37
- package/src/{engine/ai → ai}/movement/kinematic/arrive.js +42 -42
- package/src/{engine/ai → ai}/movement/kinematic/face.js +19 -19
- package/src/{engine/ai → ai}/movement/kinematic/flee.js +26 -26
- package/src/{engine/ai → ai}/movement/kinematic/seek.js +26 -26
- package/src/{engine/ai → ai}/movement/kinematic/seek.test.js +42 -42
- package/src/{engine/ai → ai}/movement/kinematic/wander-as-seek.js +31 -31
- package/src/{engine/ai → ai}/movement/kinematic/wander.js +27 -27
- package/src/{engine/animation → animation}/sprite.js +101 -101
- package/src/{engine/animation → animation}/ticker.js +38 -38
- package/src/{engine/behaviors → behaviors}/camera.js +68 -68
- package/src/{engine/behaviors → behaviors}/controls/dynamic/modern.js +76 -76
- package/src/{engine/behaviors → behaviors}/controls/dynamic/shooter.js +84 -84
- package/src/{engine/behaviors → behaviors}/controls/dynamic/tank.js +69 -69
- package/src/{engine/behaviors → behaviors}/controls/event-handlers.js +17 -17
- package/src/{engine/behaviors → behaviors}/controls/kinematic/modern.js +76 -76
- package/src/{engine/behaviors → behaviors}/controls/kinematic/shooter.js +82 -82
- package/src/{engine/behaviors → behaviors}/controls/kinematic/tank.js +67 -67
- package/src/behaviors/debug/collision.js +29 -0
- package/src/{engine/behaviors → behaviors}/fps.js +29 -29
- package/src/{engine/behaviors → behaviors}/fsm.js +33 -33
- package/src/{engine/behaviors → behaviors}/fsm.test.js +49 -49
- package/src/{engine/behaviors → behaviors}/game.js +15 -15
- package/src/{engine/behaviors → behaviors}/input/controls.js +37 -37
- package/src/{engine/behaviors → behaviors}/input/gamepad.js +114 -114
- package/src/{engine/behaviors → behaviors}/input/input.js +48 -48
- package/src/{engine/behaviors → behaviors}/input/keyboard.js +64 -64
- package/src/{engine/behaviors → behaviors}/input/mouse.js +91 -91
- package/src/{engine/behaviors → behaviors}/physics/bouncy.js +25 -25
- package/src/{engine/behaviors → behaviors}/physics/clamped.js +36 -36
- package/src/{engine/behaviors → behaviors}/physics/collidable.js +20 -20
- package/src/{engine/behaviors → behaviors}/physics/jumpable.js +145 -145
- package/src/{engine/behaviors → behaviors}/ui/button.js +17 -17
- package/src/{engine/collision → collision}/detection.js +110 -110
- package/src/{engine/core → core}/api.js +34 -34
- package/src/{engine/core → core}/dev-tools.js +135 -135
- package/src/{engine/core → core}/engine.js +119 -119
- package/src/{engine/core → core}/loop.js +15 -15
- package/src/{engine/core → core}/loops/animation-frame.js +25 -25
- package/src/{engine/core → core}/loops/elapsed.js +22 -22
- package/src/{engine/core → core}/loops/fixed.js +27 -27
- package/src/{engine/core → core}/loops/flash.js +13 -13
- package/src/{engine/core → core}/loops/lag.js +26 -26
- package/src/{engine/core → core}/select.js +26 -26
- package/src/{engine/core → core}/store.js +178 -178
- package/src/{engine/core → core}/store.test.js +110 -110
- package/src/main.js +10 -10
- package/src/{engine/movement → movement}/dynamic/modern.js +21 -21
- package/src/{engine/movement → movement}/dynamic/tank.js +43 -43
- package/src/{engine/movement → movement}/kinematic/modern.js +16 -16
- package/src/{engine/movement → movement}/kinematic/modern.test.js +27 -27
- package/src/{engine/movement → movement}/kinematic/tank.js +27 -27
- package/src/{engine/physics → physics}/bounds.js +138 -138
- package/src/{engine/physics → physics}/position.js +43 -43
- package/src/{engine/physics → physics}/position.test.js +80 -80
- package/src/{engine/systems → systems}/sprite-animation.js +27 -27
- package/src/engine/behaviors/debug/collision.js +0 -35
- package/src/renderers/canvas/absolute-position.js +0 -18
- package/src/renderers/canvas/camera.js +0 -13
- package/src/renderers/canvas/canvas-renderer.js +0 -68
- package/src/renderers/canvas/character.js +0 -38
- package/src/renderers/canvas/form/button.js +0 -25
- package/src/renderers/canvas/fps.js +0 -18
- package/src/renderers/canvas/image/hitmask.js +0 -51
- package/src/renderers/canvas/image/image.js +0 -34
- package/src/renderers/canvas/image/sprite.js +0 -49
- package/src/renderers/canvas/image/tilemap.js +0 -66
- package/src/renderers/canvas/mouse.js +0 -37
- package/src/renderers/canvas/rendering-system.js +0 -79
- package/src/renderers/canvas/shapes/circle.js +0 -29
- package/src/renderers/canvas/shapes/rectangle.js +0 -27
- package/src/renderers/react/game/character/character.module.scss +0 -17
- package/src/renderers/react/game/character/index.jsx +0 -20
- package/src/renderers/react/game/cursor/cursor.module.scss +0 -47
- package/src/renderers/react/game/cursor/index.jsx +0 -20
- package/src/renderers/react/game/form/fields/field/field.module.scss +0 -5
- package/src/renderers/react/game/form/fields/field/index.jsx +0 -56
- package/src/renderers/react/game/form/fields/fields.module.scss +0 -48
- package/src/renderers/react/game/form/fields/index.jsx +0 -12
- package/src/renderers/react/game/form/form.module.scss +0 -18
- package/src/renderers/react/game/form/index.jsx +0 -22
- package/src/renderers/react/game/fps/index.jsx +0 -16
- package/src/renderers/react/game/game.jsx +0 -72
- package/src/renderers/react/game/index.jsx +0 -29
- package/src/renderers/react/game/platform/index.jsx +0 -30
- package/src/renderers/react/game/platform/platform.module.scss +0 -7
- package/src/renderers/react/game/scene/index.jsx +0 -27
- package/src/renderers/react/game/scene/scene.module.scss +0 -9
- package/src/renderers/react/game/sprite/index.jsx +0 -60
- package/src/renderers/react/game/sprite/sprite.module.css +0 -3
- package/src/renderers/react/game/stats/index.jsx +0 -22
- package/src/renderers/react/hocs/with-absolute-position/index.jsx +0 -20
- package/src/renderers/react/hocs/with-absolute-position/with-absolute-position.module.scss +0 -5
- package/src/renderers/react/index.jsx +0 -9
- package/src/utils/algorithms/decision-tree.js +0 -24
- package/src/utils/algorithms/decision-tree.test.js +0 -153
- package/src/utils/algorithms/path-finding.js +0 -155
- package/src/utils/algorithms/path-finding.test.js +0 -151
- package/src/utils/algorithms/types.d.ts +0 -28
- package/src/utils/data-structures/array.js +0 -83
- package/src/utils/data-structures/array.test.js +0 -173
- package/src/utils/data-structures/board.js +0 -159
- package/src/utils/data-structures/board.test.js +0 -242
- package/src/utils/data-structures/boolean.js +0 -9
- package/src/utils/data-structures/heap.js +0 -164
- package/src/utils/data-structures/heap.test.js +0 -103
- package/src/utils/data-structures/object.js +0 -138
- package/src/utils/data-structures/object.test.js +0 -218
- package/src/utils/data-structures/objects.js +0 -66
- package/src/utils/data-structures/objects.test.js +0 -99
- package/src/utils/data-structures/tree.js +0 -36
- package/src/utils/data-structures/tree.test.js +0 -33
- package/src/utils/data-structures/types.d.ts +0 -4
- package/src/utils/functions/functions.js +0 -19
- package/src/utils/functions/functions.test.js +0 -23
- package/src/utils/math/geometry/circle.js +0 -70
- package/src/utils/math/geometry/circle.test.js +0 -97
- package/src/utils/math/geometry/hitmask.js +0 -70
- package/src/utils/math/geometry/hitmask.test.js +0 -155
- package/src/utils/math/geometry/line.js +0 -35
- package/src/utils/math/geometry/line.test.js +0 -49
- package/src/utils/math/geometry/point.js +0 -78
- package/src/utils/math/geometry/point.test.js +0 -81
- package/src/utils/math/geometry/rectangle.js +0 -76
- package/src/utils/math/geometry/rectangle.test.js +0 -42
- package/src/utils/math/geometry/segment.js +0 -80
- package/src/utils/math/geometry/segment.test.js +0 -183
- package/src/utils/math/geometry/triangle.js +0 -15
- package/src/utils/math/geometry/triangle.test.js +0 -11
- package/src/utils/math/geometry/types.d.ts +0 -23
- package/src/utils/math/linear-algebra/2d.js +0 -28
- package/src/utils/math/linear-algebra/2d.test.js +0 -17
- package/src/utils/math/linear-algebra/quaternion.js +0 -22
- package/src/utils/math/linear-algebra/quaternion.test.js +0 -25
- package/src/utils/math/linear-algebra/quaternions.js +0 -20
- package/src/utils/math/linear-algebra/quaternions.test.js +0 -29
- package/src/utils/math/linear-algebra/types.d.ts +0 -4
- package/src/utils/math/linear-algebra/vector.js +0 -327
- package/src/utils/math/linear-algebra/vector.test.js +0 -265
- package/src/utils/math/linear-algebra/vectors.js +0 -122
- package/src/utils/math/linear-algebra/vectors.test.js +0 -65
- package/src/utils/math/linear-interpolation.js +0 -9
- package/src/utils/math/numbers.js +0 -90
- package/src/utils/math/numbers.test.js +0 -137
- package/src/utils/math/rng.js +0 -44
- package/src/utils/math/rng.test.js +0 -39
- package/src/utils/math/statistics.js +0 -43
- package/src/utils/math/statistics.test.js +0 -47
- package/src/utils/math/trigonometry.js +0 -89
- package/src/utils/math/trigonometry.test.js +0 -52
- package/src/utils/physics/acceleration.js +0 -61
- package/src/utils/physics/friction.js +0 -28
- package/src/utils/physics/friction.test.js +0 -42
- package/src/utils/physics/gravity.js +0 -69
- package/src/utils/physics/gravity.test.js +0 -77
- package/src/utils/physics/jump.js +0 -31
- package/src/utils/physics/velocity.js +0 -36
|
@@ -1,138 +0,0 @@
|
|
|
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,218 +0,0 @@
|
|
|
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
|
-
})
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { isObject } from "./object.js"
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Extends a target object by merging it with one or more source objects.
|
|
5
|
-
* Performs a deep merge for nested objects and arrays.
|
|
6
|
-
*
|
|
7
|
-
* @param {Object} target - The target object to extend.
|
|
8
|
-
* @param {...Object} sources - The source objects to merge into the target object.
|
|
9
|
-
* @returns {Object} - The extended target object.
|
|
10
|
-
*/
|
|
11
|
-
export function extend(target, ...sources) {
|
|
12
|
-
return merge({}, target, ...sources)
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Merges multiple source objects into a target object.
|
|
17
|
-
* Performs a deep merge for nested objects and arrays.
|
|
18
|
-
*
|
|
19
|
-
* @param {Object} target - The target object to merge into.
|
|
20
|
-
* @param {...Object} sources - The source objects to merge from.
|
|
21
|
-
* @returns {Object} - The merged target object.
|
|
22
|
-
*/
|
|
23
|
-
export function merge(target, ...sources) {
|
|
24
|
-
return sources
|
|
25
|
-
.filter((source) => source != null)
|
|
26
|
-
.reduce((acc, source) => deepMerge(acc, source), target)
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Recursively merges the properties of the source object into the target object.
|
|
31
|
-
*
|
|
32
|
-
* @param {Object} target - The target object to merge into.
|
|
33
|
-
* @param {Object} source - The source object to merge from.
|
|
34
|
-
* @returns {Object} - The merged target object.
|
|
35
|
-
*/
|
|
36
|
-
function deepMerge(target, source) {
|
|
37
|
-
for (const [key, value] of Object.entries(source)) {
|
|
38
|
-
if (Array.isArray(value) || isObject(value)) {
|
|
39
|
-
if (target[key] === undefined) {
|
|
40
|
-
target[key] = new value.__proto__.constructor()
|
|
41
|
-
}
|
|
42
|
-
target[key] = deepMerge(target[key], value)
|
|
43
|
-
} else {
|
|
44
|
-
target[key] = value
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
return target
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Assigns default properties to a target object from a source object.
|
|
52
|
-
* For each key in `defaultProps`, if `target[key]` is `null` or `undefined`,
|
|
53
|
-
* it is set to `defaultProps[key]`.
|
|
54
|
-
*
|
|
55
|
-
* This function modifies the target object in place.
|
|
56
|
-
*
|
|
57
|
-
* @param {Object} target The object to apply defaults to.
|
|
58
|
-
* @param {Object} defaultProps The object containing the default properties.
|
|
59
|
-
* @returns {Object} The modified target object.
|
|
60
|
-
*/
|
|
61
|
-
export function defaults(target, defaultProps) {
|
|
62
|
-
for (const key in defaultProps) {
|
|
63
|
-
target[key] ??= defaultProps[key]
|
|
64
|
-
}
|
|
65
|
-
return target
|
|
66
|
-
}
|
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest"
|
|
2
|
-
|
|
3
|
-
import { extend, merge } from "./objects.js"
|
|
4
|
-
|
|
5
|
-
test("it should extend an object with another, producing a new object as a result", () => {
|
|
6
|
-
const obj1 = {
|
|
7
|
-
primitiveKept: 1,
|
|
8
|
-
primitiveMerged: 2,
|
|
9
|
-
primitiveArrayKept: [1, 2],
|
|
10
|
-
primitiveArrayMerged: [3, 4],
|
|
11
|
-
primitiveObjectKept: { a: 1 },
|
|
12
|
-
primitiveObjectMerged: { b: 2, c: 3 },
|
|
13
|
-
nestedArrayKept: [{ a: 1 }],
|
|
14
|
-
nestedArrayMerged: [{ b: 2, c: 3 }],
|
|
15
|
-
nestedObjectKept: { a: { b: 2 } },
|
|
16
|
-
nestedObjectMerged: { c: { d: 4 } },
|
|
17
|
-
}
|
|
18
|
-
const obj2 = {
|
|
19
|
-
primitiveMerged: 3,
|
|
20
|
-
primitiveAdded: 4,
|
|
21
|
-
primitiveArrayMerged: [5],
|
|
22
|
-
primitiveArrayAdded: [6, 7],
|
|
23
|
-
primitiveObjectMerged: { c: 4 },
|
|
24
|
-
primitiveObjectAdded: { d: 4 },
|
|
25
|
-
nestedArrayMerged: [{ d: 4 }],
|
|
26
|
-
nestedArrayAdded: [{ e: 5 }],
|
|
27
|
-
nestedObjectMerged: { c: { e: 5 } },
|
|
28
|
-
nestedObjectAdded: { f: { g: 7 } },
|
|
29
|
-
}
|
|
30
|
-
const expectedResult = {
|
|
31
|
-
primitiveKept: 1,
|
|
32
|
-
primitiveMerged: 3,
|
|
33
|
-
primitiveAdded: 4,
|
|
34
|
-
primitiveArrayKept: [1, 2],
|
|
35
|
-
primitiveArrayMerged: [5, 4],
|
|
36
|
-
primitiveArrayAdded: [6, 7],
|
|
37
|
-
primitiveObjectKept: { a: 1 },
|
|
38
|
-
primitiveObjectMerged: { b: 2, c: 4 },
|
|
39
|
-
primitiveObjectAdded: { d: 4 },
|
|
40
|
-
nestedArrayKept: [{ a: 1 }],
|
|
41
|
-
nestedArrayMerged: [{ b: 2, c: 3, d: 4 }],
|
|
42
|
-
nestedArrayAdded: [{ e: 5 }],
|
|
43
|
-
nestedObjectKept: { a: { b: 2 } },
|
|
44
|
-
nestedObjectMerged: { c: { d: 4, e: 5 } },
|
|
45
|
-
nestedObjectAdded: { f: { g: 7 } },
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const result = extend(obj1, obj2)
|
|
49
|
-
expect(result).toStrictEqual(expectedResult)
|
|
50
|
-
expect(result).not.toBe(obj1)
|
|
51
|
-
})
|
|
52
|
-
|
|
53
|
-
test("it should deep merge an two objects, changing the first object in place", () => {
|
|
54
|
-
const obj1 = {
|
|
55
|
-
primitiveKept: 1,
|
|
56
|
-
primitiveMerged: 2,
|
|
57
|
-
primitiveArrayKept: [1, 2],
|
|
58
|
-
primitiveArrayMerged: [3, 4],
|
|
59
|
-
primitiveObjectKept: { a: 1 },
|
|
60
|
-
primitiveObjectMerged: { b: 2, c: 3 },
|
|
61
|
-
nestedArrayKept: [{ a: 1 }],
|
|
62
|
-
nestedArrayMerged: [{ b: 2, c: 3 }],
|
|
63
|
-
nestedObjectKept: { a: { b: 2 } },
|
|
64
|
-
nestedObjectMerged: { c: { d: 4 } },
|
|
65
|
-
}
|
|
66
|
-
const obj2 = {
|
|
67
|
-
primitiveMerged: 3,
|
|
68
|
-
primitiveAdded: 4,
|
|
69
|
-
primitiveArrayMerged: [5],
|
|
70
|
-
primitiveArrayAdded: [6, 7],
|
|
71
|
-
primitiveObjectMerged: { c: 4 },
|
|
72
|
-
primitiveObjectAdded: { d: 4 },
|
|
73
|
-
nestedArrayMerged: [{ d: 4 }],
|
|
74
|
-
nestedArrayAdded: [{ e: 5 }],
|
|
75
|
-
nestedObjectMerged: { c: { e: 5 } },
|
|
76
|
-
nestedObjectAdded: { f: { g: 7 } },
|
|
77
|
-
}
|
|
78
|
-
const expectedResult = {
|
|
79
|
-
primitiveKept: 1,
|
|
80
|
-
primitiveMerged: 3,
|
|
81
|
-
primitiveAdded: 4,
|
|
82
|
-
primitiveArrayKept: [1, 2],
|
|
83
|
-
primitiveArrayMerged: [5, 4],
|
|
84
|
-
primitiveArrayAdded: [6, 7],
|
|
85
|
-
primitiveObjectKept: { a: 1 },
|
|
86
|
-
primitiveObjectMerged: { b: 2, c: 4 },
|
|
87
|
-
primitiveObjectAdded: { d: 4 },
|
|
88
|
-
nestedArrayKept: [{ a: 1 }],
|
|
89
|
-
nestedArrayMerged: [{ b: 2, c: 3, d: 4 }],
|
|
90
|
-
nestedArrayAdded: [{ e: 5 }],
|
|
91
|
-
nestedObjectKept: { a: { b: 2 } },
|
|
92
|
-
nestedObjectMerged: { c: { d: 4, e: 5 } },
|
|
93
|
-
nestedObjectAdded: { f: { g: 7 } },
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
const result = merge(obj1, obj2)
|
|
97
|
-
expect(result).toStrictEqual(expectedResult)
|
|
98
|
-
expect(result).toBe(obj1)
|
|
99
|
-
})
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
/** @typedef {import('./types').Tree} Tree */
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Performs a breadth-first search (BFS) traversal on a tree structure.
|
|
5
|
-
* @param {Tree} tree - The root node of the tree.
|
|
6
|
-
* @returns {Array} An array of values in BFS order.
|
|
7
|
-
*/
|
|
8
|
-
export function bfs(tree) {
|
|
9
|
-
const result = []
|
|
10
|
-
const queue = [tree]
|
|
11
|
-
|
|
12
|
-
while (queue.length) {
|
|
13
|
-
const node = queue.shift()
|
|
14
|
-
result.push(node.value)
|
|
15
|
-
if (node.children) {
|
|
16
|
-
queue.push(...node.children)
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return result
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Performs a depth-first search (DFS) traversal on a tree structure.
|
|
25
|
-
* @param {Tree} tree - The root node of the tree.
|
|
26
|
-
* @returns {Array} An array of values in DFS order.
|
|
27
|
-
*/
|
|
28
|
-
export function dfs(tree) {
|
|
29
|
-
const result = [tree.value]
|
|
30
|
-
|
|
31
|
-
if (tree.children) {
|
|
32
|
-
result.push(...tree.children.flatMap(dfs))
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return result
|
|
36
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest"
|
|
2
|
-
|
|
3
|
-
import { bfs, dfs } from "./tree.js"
|
|
4
|
-
|
|
5
|
-
test("it should perform Breadth-First Search on a tree", () => {
|
|
6
|
-
const tree = {
|
|
7
|
-
value: 1,
|
|
8
|
-
children: [
|
|
9
|
-
{ value: 2, children: [{ value: 4 }, { value: 5 }] },
|
|
10
|
-
{ value: 3, children: [{ value: 6 }, { value: 7 }] },
|
|
11
|
-
],
|
|
12
|
-
}
|
|
13
|
-
const expectedResult = [1, 2, 3, 4, 5, 6, 7]
|
|
14
|
-
|
|
15
|
-
const result = bfs(tree)
|
|
16
|
-
|
|
17
|
-
expect(result).toStrictEqual(expectedResult)
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
test("it should perform Depth-First Search on a tree", () => {
|
|
21
|
-
const tree = {
|
|
22
|
-
value: 1,
|
|
23
|
-
children: [
|
|
24
|
-
{ value: 2, children: [{ value: 3 }, { value: 4 }] },
|
|
25
|
-
{ value: 5, children: [{ value: 6 }, { value: 7 }] },
|
|
26
|
-
],
|
|
27
|
-
}
|
|
28
|
-
const expectedResult = [1, 2, 3, 4, 5, 6, 7]
|
|
29
|
-
|
|
30
|
-
const result = dfs(tree)
|
|
31
|
-
|
|
32
|
-
expect(result).toStrictEqual(expectedResult)
|
|
33
|
-
})
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Composes multiple functions from right to left, as if every function wraps the next one.
|
|
3
|
-
*
|
|
4
|
-
* @param {...Function} fns - Functions to compose.
|
|
5
|
-
* @returns {Function} A function that takes an initial value and applies the composed functions.
|
|
6
|
-
*/
|
|
7
|
-
export function compose(...fns) {
|
|
8
|
-
return (x) => fns.reduceRight((acc, fn) => fn(acc), x)
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Pipes multiple functions from left to right, as if the functions are applied one by one.
|
|
13
|
-
*
|
|
14
|
-
* @param {...Function} fns - Functions to pipe.
|
|
15
|
-
* @returns {Function} A function that takes an initial value and applies the piped functions.
|
|
16
|
-
*/
|
|
17
|
-
export function pipe(...fns) {
|
|
18
|
-
return (x) => fns.reduce((acc, fn) => fn(acc), x)
|
|
19
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest"
|
|
2
|
-
|
|
3
|
-
import { compose, pipe } from "./functions.js"
|
|
4
|
-
|
|
5
|
-
test("it should compose functions", () => {
|
|
6
|
-
const shout = (x) => x.toUpperCase()
|
|
7
|
-
const punctuate = (mark) => (x) => `${x}${mark}`
|
|
8
|
-
const html = (tag) => (x) => `<${tag}>${x}</${tag}>`
|
|
9
|
-
|
|
10
|
-
const fn = compose(html("p"), punctuate("!"), shout)
|
|
11
|
-
|
|
12
|
-
expect(fn("Hello world")).toBe("<p>HELLO WORLD!</p>")
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
test("it should pipe functions", () => {
|
|
16
|
-
const shout = (x) => x.toUpperCase()
|
|
17
|
-
const punctuate = (mark) => (x) => `${x}${mark}`
|
|
18
|
-
const html = (tag) => (x) => `<${tag}>${x}</${tag}>`
|
|
19
|
-
|
|
20
|
-
const fn = pipe(shout, punctuate("!"), html("p"))
|
|
21
|
-
|
|
22
|
-
expect(fn("Hello world")).toBe("<p>HELLO WORLD!</p>")
|
|
23
|
-
})
|