@inglorious/utils 1.0.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -11
- package/package.json +4 -4
- package/src/algorithms/decision-tree.js +24 -0
- package/src/algorithms/decision-tree.test.js +153 -0
- package/src/algorithms/path-finding.js +150 -0
- package/src/algorithms/path-finding.test.js +151 -0
- package/src/algorithms/types.d.ts +28 -0
- package/src/data-structures/array.js +93 -0
- package/src/data-structures/array.test.js +185 -0
- package/src/data-structures/board.js +159 -0
- package/src/data-structures/board.test.js +242 -0
- package/src/data-structures/boolean.js +9 -0
- package/src/data-structures/heap.js +164 -0
- package/src/data-structures/heap.test.js +103 -0
- package/src/data-structures/object.js +138 -0
- package/src/data-structures/object.test.js +218 -0
- package/src/data-structures/objects.js +66 -0
- package/src/data-structures/objects.test.js +99 -0
- package/src/data-structures/set.js +9 -0
- package/src/data-structures/set.test.js +15 -0
- package/src/data-structures/tree.js +36 -0
- package/src/data-structures/tree.test.js +33 -0
- package/src/data-structures/types.d.ts +4 -0
- package/src/functions/functions.js +19 -0
- package/src/functions/functions.test.js +23 -0
- package/src/math/geometry/circle.js +69 -0
- package/src/math/geometry/circle.test.js +97 -0
- package/src/math/geometry/hitmask.js +70 -0
- package/src/math/geometry/hitmask.test.js +155 -0
- package/src/math/geometry/line.js +34 -0
- package/src/math/geometry/line.test.js +49 -0
- package/src/math/geometry/point.js +78 -0
- package/src/math/geometry/point.test.js +81 -0
- package/src/math/geometry/rectangle.js +76 -0
- package/src/math/geometry/rectangle.test.js +42 -0
- package/src/math/geometry/segment.js +72 -0
- package/src/math/geometry/segment.test.js +183 -0
- package/src/math/geometry/triangle.js +15 -0
- package/src/math/geometry/triangle.test.js +11 -0
- package/src/math/geometry/types.d.ts +23 -0
- package/src/math/linear-algebra/2d.js +28 -0
- package/src/math/linear-algebra/2d.test.js +17 -0
- package/src/math/linear-algebra/quaternion.js +22 -0
- package/src/math/linear-algebra/quaternion.test.js +25 -0
- package/src/math/linear-algebra/quaternions.js +20 -0
- package/src/math/linear-algebra/quaternions.test.js +29 -0
- package/src/math/linear-algebra/types.d.ts +4 -0
- package/src/math/linear-algebra/vector.js +337 -0
- package/src/math/linear-algebra/vector.test.js +265 -0
- package/src/math/linear-algebra/vectors.js +122 -0
- package/src/math/linear-algebra/vectors.test.js +65 -0
- package/src/math/linear-interpolation.js +9 -0
- package/src/math/numbers.js +90 -0
- package/src/math/numbers.test.js +137 -0
- package/src/math/rng.js +44 -0
- package/src/math/rng.test.js +39 -0
- package/src/math/statistics.js +43 -0
- package/src/math/statistics.test.js +47 -0
- package/src/math/trigonometry.js +89 -0
- package/src/math/trigonometry.test.js +52 -0
- package/src/physics/acceleration.js +56 -0
- package/src/physics/friction.js +24 -0
- package/src/physics/friction.test.js +42 -0
- package/src/physics/gravity.js +69 -0
- package/src/physics/gravity.test.js +77 -0
- package/src/physics/jump.js +31 -0
- package/src/physics/velocity.js +32 -0
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
// @see https://stackfull.dev/heaps-in-javascript
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Default comparator for a max-heap.
|
|
5
|
+
* @param {*} a - First element.
|
|
6
|
+
* @param {*} b - Second element.
|
|
7
|
+
* @returns {number} Positive if `a` should come before `b`, negative otherwise.
|
|
8
|
+
*/
|
|
9
|
+
const DEFAULT_COMPARATOR = (a, b) => b - a
|
|
10
|
+
|
|
11
|
+
// Constants for heap operations
|
|
12
|
+
const ROOT_INDEX = 0 // Index of the root element in the heap.
|
|
13
|
+
const SINGLE_ELEMENT = 1 // Heap size when it contains a single element.
|
|
14
|
+
const LEFT_CHILD_MULTIPLIER = 2 // Multiplier to calculate the left child index.
|
|
15
|
+
const RIGHT_CHILD_MULTIPLIER = 2 // Multiplier to calculate the right child index.
|
|
16
|
+
const PARENT_DIVISOR = 2 // Divisor to calculate the parent index.
|
|
17
|
+
const LEFT_CHILD_OFFSET = 1 // Offset to calculate the left child index.
|
|
18
|
+
const RIGHT_CHILD_OFFSET = 2 // Offset to calculate the right child index.
|
|
19
|
+
const LAST_ELEMENT_OFFSET = 1 // Offset to get the last element in the heap.
|
|
20
|
+
const INDEX_ADJUSTMENT = 1 // Adjustment for zero-based indexing.
|
|
21
|
+
const COMPARATOR_THRESHOLD = 0 // Threshold for comparator results.
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Checks if a heap contains a specific item.
|
|
25
|
+
* @param {Array} heap - The heap array.
|
|
26
|
+
* @param {*} item - The item to check for.
|
|
27
|
+
* @returns {boolean} True if the item exists in the heap, false otherwise.
|
|
28
|
+
*/
|
|
29
|
+
export function contains(heap, item) {
|
|
30
|
+
return heap.includes(item)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Converts an array into a heap.
|
|
35
|
+
* @param {Array} arr - The input array.
|
|
36
|
+
* @param {Function} [comparator=DEFAULT_COMPARATOR] - Comparator function to define heap order.
|
|
37
|
+
* @returns {Array} The heapified array.
|
|
38
|
+
*/
|
|
39
|
+
export function heapify(arr, comparator = DEFAULT_COMPARATOR) {
|
|
40
|
+
let heap = []
|
|
41
|
+
for (const item of arr) {
|
|
42
|
+
heap = push(heap, item, comparator)
|
|
43
|
+
}
|
|
44
|
+
return heap
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Adds an item to the heap and maintains the heap property.
|
|
49
|
+
* @param {Array} heap - The heap array.
|
|
50
|
+
* @param {*} item - The item to add.
|
|
51
|
+
* @param {Function} [comparator=DEFAULT_COMPARATOR] - Comparator function to define heap order.
|
|
52
|
+
* @returns {Array} The updated heap array.
|
|
53
|
+
*/
|
|
54
|
+
export function push(heap, item, comparator = DEFAULT_COMPARATOR) {
|
|
55
|
+
const h = [...heap, item]
|
|
56
|
+
|
|
57
|
+
if (h.length === SINGLE_ELEMENT) {
|
|
58
|
+
return h
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
let index = h.length - LAST_ELEMENT_OFFSET
|
|
62
|
+
let parentIndex = parent(index)
|
|
63
|
+
|
|
64
|
+
while (
|
|
65
|
+
index > ROOT_INDEX &&
|
|
66
|
+
comparator(h[index], h[parentIndex]) > COMPARATOR_THRESHOLD
|
|
67
|
+
) {
|
|
68
|
+
;[h[index], h[parentIndex]] = [h[parentIndex], h[index]]
|
|
69
|
+
|
|
70
|
+
index = parentIndex
|
|
71
|
+
parentIndex = parent(index)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return h
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Calculates the index of the left child of a given node.
|
|
79
|
+
* @param {number} index - The index of the parent node.
|
|
80
|
+
* @returns {number} The index of the left child.
|
|
81
|
+
*/
|
|
82
|
+
export function left(index) {
|
|
83
|
+
return LEFT_CHILD_MULTIPLIER * index + LEFT_CHILD_OFFSET
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Calculates the index of the parent of a given node.
|
|
88
|
+
* @param {number} index - The index of the child node.
|
|
89
|
+
* @returns {number} The index of the parent node.
|
|
90
|
+
*/
|
|
91
|
+
export function parent(index) {
|
|
92
|
+
return Math.floor((index - INDEX_ADJUSTMENT) / PARENT_DIVISOR)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Removes and returns the root element of the heap.
|
|
97
|
+
* @param {Array} heap - The heap array.
|
|
98
|
+
* @param {Function} [comparator=DEFAULT_COMPARATOR] - Comparator function to define heap order.
|
|
99
|
+
* @returns {Array} The updated heap array after removing the root.
|
|
100
|
+
*/
|
|
101
|
+
export function pop(heap, comparator = DEFAULT_COMPARATOR) {
|
|
102
|
+
const h = [...heap]
|
|
103
|
+
;[h[ROOT_INDEX], h[h.length - LAST_ELEMENT_OFFSET]] = [
|
|
104
|
+
h[h.length - LAST_ELEMENT_OFFSET],
|
|
105
|
+
h[ROOT_INDEX],
|
|
106
|
+
]
|
|
107
|
+
|
|
108
|
+
h.pop()
|
|
109
|
+
|
|
110
|
+
let index = ROOT_INDEX
|
|
111
|
+
let leftIndex = left(index)
|
|
112
|
+
let rightIndex = right(index)
|
|
113
|
+
let newMinFound = false
|
|
114
|
+
|
|
115
|
+
while (leftIndex < h.length && !newMinFound) {
|
|
116
|
+
const minIndex =
|
|
117
|
+
rightIndex < h.length &&
|
|
118
|
+
comparator(h[rightIndex], h[leftIndex]) > COMPARATOR_THRESHOLD
|
|
119
|
+
? rightIndex
|
|
120
|
+
: leftIndex
|
|
121
|
+
newMinFound = comparator(h[minIndex], h[index]) > COMPARATOR_THRESHOLD
|
|
122
|
+
|
|
123
|
+
if (newMinFound) {
|
|
124
|
+
;[h[minIndex], h[index]] = [h[index], h[minIndex]]
|
|
125
|
+
|
|
126
|
+
index = minIndex
|
|
127
|
+
leftIndex = left(index)
|
|
128
|
+
rightIndex = right(index)
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return h
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Removes the root element and reorders the heap.
|
|
137
|
+
* @param {Array} heap - The heap array.
|
|
138
|
+
* @returns {Array} The updated heap array.
|
|
139
|
+
*/
|
|
140
|
+
export function remove(heap) {
|
|
141
|
+
const [, ...rest] = heap
|
|
142
|
+
return heapify([
|
|
143
|
+
...rest.slice(-LAST_ELEMENT_OFFSET),
|
|
144
|
+
...rest.slice(ROOT_INDEX, -LAST_ELEMENT_OFFSET),
|
|
145
|
+
])
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Calculates the index of the right child of a given node.
|
|
150
|
+
* @param {number} index - The index of the parent node.
|
|
151
|
+
* @returns {number} The index of the right child.
|
|
152
|
+
*/
|
|
153
|
+
export function right(index) {
|
|
154
|
+
return RIGHT_CHILD_MULTIPLIER * index + RIGHT_CHILD_OFFSET
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Retrieves the root element of the heap.
|
|
159
|
+
* @param {Array} heap - The heap array.
|
|
160
|
+
* @returns {*} The root element of the heap.
|
|
161
|
+
*/
|
|
162
|
+
export function root(heap) {
|
|
163
|
+
return heap[ROOT_INDEX]
|
|
164
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { expect, test } from "vitest"
|
|
2
|
+
|
|
3
|
+
import { contains, heapify, pop, push, remove, root } from "./heap.js"
|
|
4
|
+
|
|
5
|
+
test("it should check if a heap contains a value", () => {
|
|
6
|
+
const heap = [3, 2, 6, 1, 7, 4, 5]
|
|
7
|
+
const item = 1
|
|
8
|
+
const expectedResult = true
|
|
9
|
+
|
|
10
|
+
expect(contains(heap, item)).toBe(expectedResult)
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
test("it should check if a heap contains an object", () => {
|
|
14
|
+
const heap = [
|
|
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 = heap[3]
|
|
24
|
+
const expectedResult = true
|
|
25
|
+
|
|
26
|
+
expect(contains(heap, item)).toBe(expectedResult)
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
test("it should not convert an array in existing heap form into a heap", () => {
|
|
30
|
+
const arr = [1, 2, 3, 4, 5, 6, 7]
|
|
31
|
+
const expectedResult = [1, 2, 3, 4, 5, 6, 7]
|
|
32
|
+
|
|
33
|
+
expect(heapify(arr)).toStrictEqual(expectedResult)
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
test("it should convert an array into a heap", () => {
|
|
37
|
+
const arr = [7, 6, 5, 4, 3, 2, 1]
|
|
38
|
+
const expectedResult = [1, 4, 2, 7, 5, 6, 3]
|
|
39
|
+
|
|
40
|
+
expect(heapify(arr)).toStrictEqual(expectedResult)
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
test("it should convert an array into a maxheap", () => {
|
|
44
|
+
const arr = [1, 2, 3, 4, 5, 6, 7]
|
|
45
|
+
const comparator = (a, b) => a - b
|
|
46
|
+
const expectedResult = [7, 4, 6, 1, 3, 2, 5]
|
|
47
|
+
|
|
48
|
+
expect(heapify(arr, comparator)).toStrictEqual(expectedResult)
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
test("it should find the minimum value in a min heap", () => {
|
|
52
|
+
const heap = [1, 4, 2, 7, 5, 6, 3]
|
|
53
|
+
const expectedResult = 1
|
|
54
|
+
|
|
55
|
+
expect(root(heap)).toBe(expectedResult)
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
test("it should push the first element of the heap", () => {
|
|
59
|
+
const heap = []
|
|
60
|
+
const value = 7
|
|
61
|
+
const expectedResult = [7]
|
|
62
|
+
|
|
63
|
+
expect(push(heap, value)).toStrictEqual(expectedResult)
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
test("it should push the biggest value at the end of the heap", () => {
|
|
67
|
+
const heap = [3, 13, 19, 33, 42, 23, 21]
|
|
68
|
+
const value = 50
|
|
69
|
+
const expectedResult = [3, 13, 19, 33, 42, 23, 21, 50]
|
|
70
|
+
|
|
71
|
+
expect(push(heap, value)).toStrictEqual(expectedResult)
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
test("it should push a value in the right spot of a heap", () => {
|
|
75
|
+
const heap = [3, 13, 19, 33, 42, 23, 21]
|
|
76
|
+
const value = 7
|
|
77
|
+
const expectedResult = [3, 7, 19, 13, 42, 23, 21, 33]
|
|
78
|
+
|
|
79
|
+
expect(push(heap, value)).toStrictEqual(expectedResult)
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
test("it should pop the lowest value from the end of the heap", () => {
|
|
83
|
+
const heap = [3, 13, 19, 33, 42, 23, 21]
|
|
84
|
+
const expectedResult = [13, 21, 19, 33, 42, 23]
|
|
85
|
+
|
|
86
|
+
expect(pop(heap)).toStrictEqual(expectedResult)
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
test("it should push the biggest value at the beginning of a maxheap", () => {
|
|
90
|
+
const heap = [3, 13, 19, 33, 42, 23, 21]
|
|
91
|
+
const comparator = (a, b) => a - b
|
|
92
|
+
const value = 50
|
|
93
|
+
const expectedResult = [50, 3, 19, 13, 42, 23, 21, 33]
|
|
94
|
+
|
|
95
|
+
expect(push(heap, value, comparator)).toStrictEqual(expectedResult)
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
test("it should remove the heap's root element", () => {
|
|
99
|
+
const heap = [3, 13, 19, 33, 42, 23, 21]
|
|
100
|
+
const expectedResult = [13, 21, 19, 33, 42, 23]
|
|
101
|
+
|
|
102
|
+
expect(remove(heap)).toStrictEqual(expectedResult)
|
|
103
|
+
})
|
|
@@ -0,0 +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
|
+
* 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
|
+
}
|
|
@@ -0,0 +1,218 @@
|
|
|
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
|
+
})
|
|
@@ -0,0 +1,66 @@
|
|
|
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
|
+
}
|