@inglorious/utils 3.6.3 → 3.7.1
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 +1 -1
- package/package.json +5 -4
- package/src/data-structures/object.js +96 -3
- package/src/algorithms/decision-tree.test.js +0 -154
- package/src/algorithms/path-finding.test.js +0 -151
- package/src/data-structures/array.test.js +0 -206
- package/src/data-structures/board.test.js +0 -242
- package/src/data-structures/heap.test.js +0 -103
- package/src/data-structures/object.test.js +0 -335
- package/src/data-structures/objects.test.js +0 -196
- package/src/data-structures/set.test.js +0 -15
- package/src/data-structures/tree.test.js +0 -33
- package/src/functions/function.test.js +0 -15
- package/src/functions/functions.test.js +0 -51
- package/src/math/circle.test.js +0 -98
- package/src/math/hitmask.test.js +0 -156
- package/src/math/line.test.js +0 -50
- package/src/math/numbers.test.js +0 -198
- package/src/math/point.test.js +0 -94
- package/src/math/quaternion.test.js +0 -25
- package/src/math/quaternions.test.js +0 -29
- package/src/math/rectangle.test.js +0 -43
- package/src/math/rng.test.js +0 -55
- package/src/math/segment.test.js +0 -184
- package/src/math/statistics.test.js +0 -47
- package/src/math/triangle.test.js +0 -11
- package/src/math/trigonometry.test.js +0 -52
- package/src/math/vector.test.js +0 -345
- package/src/math/vectors.test.js +0 -77
- package/src/physics/friction.test.js +0 -43
- package/src/physics/gravity.test.js +0 -78
- package/src/v.test.js +0 -48
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
[](https://www.npmjs.com/package/@inglorious/utils)
|
|
4
4
|
[](https://opensource.org/licenses/MIT)
|
|
5
5
|
|
|
6
|
-
A set of general-purpose utility functions designed with functional programming principles in mind. This package is part of the [Inglorious
|
|
6
|
+
A set of general-purpose utility functions designed with functional programming principles in mind. This package is part of the [Inglorious Forge](https://github.com/IngloriousCoderz/inglorious-forge) monorepo.
|
|
7
7
|
|
|
8
8
|
---
|
|
9
9
|
|
package/package.json
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/utils",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.7.1",
|
|
4
4
|
"description": "A set of general-purpose utility functions designed with functional programming principles in mind.",
|
|
5
5
|
"author": "IceOnFire <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
|
-
"url": "git+https://github.com/IngloriousCoderz/inglorious-
|
|
9
|
+
"url": "git+https://github.com/IngloriousCoderz/inglorious-forge.git",
|
|
10
10
|
"directory": "packages/utils"
|
|
11
11
|
},
|
|
12
12
|
"homepage": "https://inglorious-engine.vercel.app/",
|
|
13
13
|
"bugs": {
|
|
14
|
-
"url": "https://github.com/IngloriousCoderz/inglorious-
|
|
14
|
+
"url": "https://github.com/IngloriousCoderz/inglorious-forge/issues"
|
|
15
15
|
},
|
|
16
16
|
"keywords": [
|
|
17
17
|
"functional-programming",
|
|
@@ -33,6 +33,7 @@
|
|
|
33
33
|
},
|
|
34
34
|
"files": [
|
|
35
35
|
"src",
|
|
36
|
+
"!src/**/*.test.js",
|
|
36
37
|
"types"
|
|
37
38
|
],
|
|
38
39
|
"publishConfig": {
|
|
@@ -42,7 +43,7 @@
|
|
|
42
43
|
"prettier": "^3.6.2",
|
|
43
44
|
"vite": "^7.1.3",
|
|
44
45
|
"vitest": "^1.6.1",
|
|
45
|
-
"@inglorious/eslint-config": "1.
|
|
46
|
+
"@inglorious/eslint-config": "1.1.1"
|
|
46
47
|
},
|
|
47
48
|
"engines": {
|
|
48
49
|
"node": ">= 22"
|
|
@@ -2,13 +2,26 @@ import { v } from "../v.js"
|
|
|
2
2
|
import { isArray } from "./array.js"
|
|
3
3
|
|
|
4
4
|
const INITIAL_LEVEL = 0
|
|
5
|
+
const LAST_KEY_OFFSET = 1
|
|
6
|
+
const NEXT_KEY_OFFSET = 1
|
|
5
7
|
const NEXT_LEVEL = 2
|
|
6
8
|
|
|
7
9
|
/**
|
|
8
|
-
* Creates a deep clone of
|
|
10
|
+
* Creates a deep clone of a given value using the structured clone algorithm.
|
|
11
|
+
* This is a modern and efficient way to deep-clone objects.
|
|
9
12
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
13
|
+
* Note: `structuredClone` has some limitations. It cannot clone functions,
|
|
14
|
+
* DOM nodes, property descriptors, getters, setters, or certain other
|
|
15
|
+
* non-serializable objects. It will throw a `DataCloneError` in such cases.
|
|
16
|
+
*
|
|
17
|
+
* For more details, see:
|
|
18
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
|
|
19
|
+
*
|
|
20
|
+
* This is similar to Lodash's `_.cloneDeep`.
|
|
21
|
+
*
|
|
22
|
+
* @template T
|
|
23
|
+
* @param {T} obj - The value to clone.
|
|
24
|
+
* @returns {T} A deep clone of the input value.
|
|
12
25
|
*/
|
|
13
26
|
export function clone(obj) {
|
|
14
27
|
return deserialize(serialize(obj))
|
|
@@ -83,6 +96,42 @@ export function find(obj, callback) {
|
|
|
83
96
|
])
|
|
84
97
|
}
|
|
85
98
|
|
|
99
|
+
/**
|
|
100
|
+
* Gets the value at a specified path of an object. If the resolved value is
|
|
101
|
+
* `undefined`, the `defaultValue` is returned in its place. This function
|
|
102
|
+
* supports dot-notation for nested properties and numbers for array indices.
|
|
103
|
+
*
|
|
104
|
+
* This is similar to Lodash's `_.get`.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* const object = { 'a': [{ 'b': { 'c': 3 } }] };
|
|
108
|
+
* get(object, 'a.0.b.c');
|
|
109
|
+
* // => 3
|
|
110
|
+
*
|
|
111
|
+
* get(object, 'a.0.d', 'default');
|
|
112
|
+
* // => 'default'
|
|
113
|
+
*
|
|
114
|
+
* @param {Object} obj - The object to query.
|
|
115
|
+
* @param {string} path - The path of the property to retrieve.
|
|
116
|
+
* @param {*} [defaultValue=undefined] - The value returned for `undefined` resolved values.
|
|
117
|
+
* @returns {*} Returns the resolved value, else the `defaultValue`.
|
|
118
|
+
*/
|
|
119
|
+
export function get(obj, path, defaultValue = undefined) {
|
|
120
|
+
if (!path) return defaultValue
|
|
121
|
+
|
|
122
|
+
const keys = path.split(".")
|
|
123
|
+
let result = obj
|
|
124
|
+
|
|
125
|
+
for (const key of keys) {
|
|
126
|
+
if (result == null || typeof result !== "object") {
|
|
127
|
+
return defaultValue
|
|
128
|
+
}
|
|
129
|
+
result = result[key]
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return result !== undefined ? result : defaultValue
|
|
133
|
+
}
|
|
134
|
+
|
|
86
135
|
/**
|
|
87
136
|
* Checks if a value is a plain object.
|
|
88
137
|
*
|
|
@@ -185,6 +234,50 @@ export function serialize(obj) {
|
|
|
185
234
|
return JSON.stringify(obj, replacer)
|
|
186
235
|
}
|
|
187
236
|
|
|
237
|
+
/**
|
|
238
|
+
* Sets the value at a specified path of an object. If a portion of the path
|
|
239
|
+
* doesn't exist, it's created. Arrays are created for missing index properties
|
|
240
|
+
* while objects are created for all other missing properties. This function
|
|
241
|
+
* mutates the object.
|
|
242
|
+
*
|
|
243
|
+
* This is similar to Lodash's `_.set`.
|
|
244
|
+
*
|
|
245
|
+
* @example
|
|
246
|
+
* const object = { 'a': [{ 'b': { 'c': 3 } }] };
|
|
247
|
+
* set(object, 'a.0.b.d', 4);
|
|
248
|
+
* // object is now { 'a': [{ 'b': { 'c': 3, 'd': 4 } }] }
|
|
249
|
+
*
|
|
250
|
+
* set(object, 'x.0.y', 5);
|
|
251
|
+
* // object is now { 'a': [...], 'x': [{ 'y': 5 }] }
|
|
252
|
+
*
|
|
253
|
+
* @param {Object} obj - The object to modify.
|
|
254
|
+
* @param {string} path - The path of the property to set.
|
|
255
|
+
* @param {*} value - The value to set.
|
|
256
|
+
* @returns {Object} Returns the modified object.
|
|
257
|
+
*/
|
|
258
|
+
export function set(obj, path, value) {
|
|
259
|
+
if (!path) return obj
|
|
260
|
+
|
|
261
|
+
const keys = path.split(".")
|
|
262
|
+
let current = obj
|
|
263
|
+
|
|
264
|
+
for (let i = 0; i < keys.length - LAST_KEY_OFFSET; i++) {
|
|
265
|
+
const key = keys[i]
|
|
266
|
+
const nextKey = keys[i + NEXT_KEY_OFFSET]
|
|
267
|
+
|
|
268
|
+
// Create missing intermediate objects
|
|
269
|
+
if (!(key in current) || typeof current[key] !== "object") {
|
|
270
|
+
// Create array if next key is a number, object otherwise
|
|
271
|
+
current[key] = /^\d+$/.test(nextKey) ? [] : {}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
current = current[key]
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
current[keys[keys.length - LAST_KEY_OFFSET]] = value
|
|
278
|
+
return obj
|
|
279
|
+
}
|
|
280
|
+
|
|
188
281
|
/**
|
|
189
282
|
* Converts an object or array to a formatted string representation.
|
|
190
283
|
*
|
|
@@ -1,154 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest"
|
|
2
|
-
|
|
3
|
-
import { length } from "../math/vector.js"
|
|
4
|
-
import { subtract } from "../math/vectors.js"
|
|
5
|
-
import { v } from "../v.js"
|
|
6
|
-
import { decide } from "./decision-tree.js"
|
|
7
|
-
|
|
8
|
-
test("it should make a decision based on a binary decision tree", () => {
|
|
9
|
-
const entity = { state: "sleeping", position: v(0, 0, 0) }
|
|
10
|
-
const target = { position: v(10, 0, 0) }
|
|
11
|
-
const tree = {
|
|
12
|
-
test({ entity }) {
|
|
13
|
-
return entity.state === "idle"
|
|
14
|
-
},
|
|
15
|
-
true() {
|
|
16
|
-
return {
|
|
17
|
-
test({ entity, target }) {
|
|
18
|
-
const distance = length(subtract(target.position, entity.position))
|
|
19
|
-
return distance < 250
|
|
20
|
-
},
|
|
21
|
-
true() {
|
|
22
|
-
return "aware"
|
|
23
|
-
},
|
|
24
|
-
}
|
|
25
|
-
},
|
|
26
|
-
false() {
|
|
27
|
-
return {
|
|
28
|
-
test({ entity }) {
|
|
29
|
-
return entity.state === "chasing"
|
|
30
|
-
},
|
|
31
|
-
true() {
|
|
32
|
-
return {
|
|
33
|
-
test({ entity, target }) {
|
|
34
|
-
const distance = length(
|
|
35
|
-
subtract(target.position, entity.position),
|
|
36
|
-
)
|
|
37
|
-
return distance >= 250
|
|
38
|
-
},
|
|
39
|
-
true() {
|
|
40
|
-
return "idle"
|
|
41
|
-
},
|
|
42
|
-
false() {
|
|
43
|
-
return {
|
|
44
|
-
test({ entity, target }) {
|
|
45
|
-
const distance = length(
|
|
46
|
-
subtract(target.position, entity.position),
|
|
47
|
-
)
|
|
48
|
-
return distance < 10
|
|
49
|
-
},
|
|
50
|
-
true() {
|
|
51
|
-
return "sleepy"
|
|
52
|
-
},
|
|
53
|
-
}
|
|
54
|
-
},
|
|
55
|
-
}
|
|
56
|
-
},
|
|
57
|
-
false() {
|
|
58
|
-
return {
|
|
59
|
-
test({ entity }) {
|
|
60
|
-
return ["sleepy", "sleeping"].includes(entity.state)
|
|
61
|
-
},
|
|
62
|
-
true() {
|
|
63
|
-
return {
|
|
64
|
-
test({ entity, target }) {
|
|
65
|
-
const distance = length(
|
|
66
|
-
subtract(target.position, entity.position),
|
|
67
|
-
)
|
|
68
|
-
return distance >= 10
|
|
69
|
-
},
|
|
70
|
-
true() {
|
|
71
|
-
return "aware"
|
|
72
|
-
},
|
|
73
|
-
}
|
|
74
|
-
},
|
|
75
|
-
}
|
|
76
|
-
},
|
|
77
|
-
}
|
|
78
|
-
},
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
const state = decide(tree, { entity, target })
|
|
82
|
-
|
|
83
|
-
expect(state).toBe("aware")
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
test("it should make a decision on a multi-child tree", () => {
|
|
87
|
-
const entity = { state: "sleeping", position: v(0, 0, 0) }
|
|
88
|
-
const target = { position: v(10, 0, 0) }
|
|
89
|
-
const wakeUp = () => ({
|
|
90
|
-
test({ entity, target }) {
|
|
91
|
-
const distance = length(subtract(target.position, entity.position))
|
|
92
|
-
return distance >= 10
|
|
93
|
-
},
|
|
94
|
-
true() {
|
|
95
|
-
return "aware"
|
|
96
|
-
},
|
|
97
|
-
false({ entity }) {
|
|
98
|
-
return entity.state
|
|
99
|
-
},
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
const tree = {
|
|
103
|
-
test({ entity }) {
|
|
104
|
-
return entity.state
|
|
105
|
-
},
|
|
106
|
-
idle() {
|
|
107
|
-
return {
|
|
108
|
-
test({ entity, target }) {
|
|
109
|
-
const distance = length(subtract(target.position, entity.position))
|
|
110
|
-
return distance < 250
|
|
111
|
-
},
|
|
112
|
-
true() {
|
|
113
|
-
return "aware"
|
|
114
|
-
},
|
|
115
|
-
false({ entity }) {
|
|
116
|
-
return entity.state
|
|
117
|
-
},
|
|
118
|
-
}
|
|
119
|
-
},
|
|
120
|
-
chasing() {
|
|
121
|
-
return {
|
|
122
|
-
test({ entity, target }) {
|
|
123
|
-
const distance = length(subtract(target.position, entity.position))
|
|
124
|
-
return distance >= 250
|
|
125
|
-
},
|
|
126
|
-
true() {
|
|
127
|
-
return "idle"
|
|
128
|
-
},
|
|
129
|
-
false() {
|
|
130
|
-
return {
|
|
131
|
-
test({ entity, target }) {
|
|
132
|
-
const distance = length(
|
|
133
|
-
subtract(target.position, entity.position),
|
|
134
|
-
)
|
|
135
|
-
return distance < 10
|
|
136
|
-
},
|
|
137
|
-
true() {
|
|
138
|
-
return "sleepy"
|
|
139
|
-
},
|
|
140
|
-
false({ entity }) {
|
|
141
|
-
return entity.state
|
|
142
|
-
},
|
|
143
|
-
}
|
|
144
|
-
},
|
|
145
|
-
}
|
|
146
|
-
},
|
|
147
|
-
sleepy: wakeUp,
|
|
148
|
-
sleeping: wakeUp,
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
const state = decide(tree, { entity, target })
|
|
152
|
-
|
|
153
|
-
expect(state).toBe("aware")
|
|
154
|
-
})
|
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest"
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
createBoard,
|
|
5
|
-
down,
|
|
6
|
-
downRight,
|
|
7
|
-
right,
|
|
8
|
-
} from "../data-structures/board.js"
|
|
9
|
-
import { findPath } from "./path-finding.js"
|
|
10
|
-
|
|
11
|
-
test("it should find the shortest path in a trivial example", () => {
|
|
12
|
-
const graph = {
|
|
13
|
-
nodes: {
|
|
14
|
-
A: [0, 0],
|
|
15
|
-
B: [2, 0],
|
|
16
|
-
},
|
|
17
|
-
arcs: [{ from: "A", to: "B", cost: 2 }],
|
|
18
|
-
}
|
|
19
|
-
const start = "A"
|
|
20
|
-
const end = "B"
|
|
21
|
-
const expectedResult = ["A", "B"]
|
|
22
|
-
|
|
23
|
-
expect(findPath(graph, start, end)).toStrictEqual(expectedResult)
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
test("it should find the shortest path between two nodes with default heuristic", () => {
|
|
27
|
-
const graph = {
|
|
28
|
-
nodes: {
|
|
29
|
-
A: [0, 0],
|
|
30
|
-
B: [2, 0],
|
|
31
|
-
C: [3, 2],
|
|
32
|
-
D: [4, 4],
|
|
33
|
-
E: [1, 1],
|
|
34
|
-
F: [2, 3],
|
|
35
|
-
G: [0, 4],
|
|
36
|
-
},
|
|
37
|
-
arcs: [
|
|
38
|
-
{ from: "A", to: "B" },
|
|
39
|
-
{ from: "B", to: "C" },
|
|
40
|
-
{ from: "C", to: "D" },
|
|
41
|
-
{ from: "A", to: "E" },
|
|
42
|
-
{ from: "E", to: "F" },
|
|
43
|
-
{ from: "F", to: "G" },
|
|
44
|
-
{ from: "G", to: "D" },
|
|
45
|
-
],
|
|
46
|
-
}
|
|
47
|
-
const start = "A"
|
|
48
|
-
const end = "D"
|
|
49
|
-
const expectedResult = ["A", "B", "C", "D"]
|
|
50
|
-
|
|
51
|
-
expect(findPath(graph, start, end)).toStrictEqual(expectedResult)
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
test("it should find the shortest path between two nodes with node costs", () => {
|
|
55
|
-
const graph = {
|
|
56
|
-
nodes: [
|
|
57
|
-
{ id: "A", position: [0, 0], cost: 0 },
|
|
58
|
-
{ id: "B", position: [2, 0], cost: 7 },
|
|
59
|
-
{ id: "C", position: [3, 2], cost: 3 },
|
|
60
|
-
{ id: "D", position: [4, 4], cost: 2 },
|
|
61
|
-
{ id: "E", position: [1, 1], cost: 1.5 },
|
|
62
|
-
{ id: "F", position: [2, 3], cost: 2 },
|
|
63
|
-
{ id: "G", position: [0, 4], cost: 3 },
|
|
64
|
-
],
|
|
65
|
-
arcs: [
|
|
66
|
-
{ from: "A", to: "B" },
|
|
67
|
-
{ from: "B", to: "C" },
|
|
68
|
-
{ from: "C", to: "D" },
|
|
69
|
-
{ from: "A", to: "E" },
|
|
70
|
-
{ from: "E", to: "F" },
|
|
71
|
-
{ from: "F", to: "G" },
|
|
72
|
-
{ from: "G", to: "D" },
|
|
73
|
-
],
|
|
74
|
-
}
|
|
75
|
-
const start = "A"
|
|
76
|
-
const end = "D"
|
|
77
|
-
const expectedResult = ["A", "E", "F", "G", "D"]
|
|
78
|
-
|
|
79
|
-
expect(findPath(graph, start, end)).toStrictEqual(expectedResult)
|
|
80
|
-
})
|
|
81
|
-
|
|
82
|
-
test("it should find the shortest path between two nodes with arc costs", () => {
|
|
83
|
-
const graph = {
|
|
84
|
-
nodes: {
|
|
85
|
-
A: [0, 0],
|
|
86
|
-
B: [2, 0],
|
|
87
|
-
C: [3, 2],
|
|
88
|
-
D: [4, 4],
|
|
89
|
-
E: [1, 1],
|
|
90
|
-
F: [2, 3],
|
|
91
|
-
G: [0, 4],
|
|
92
|
-
},
|
|
93
|
-
arcs: [
|
|
94
|
-
{ from: "A", to: "B", cost: 7 },
|
|
95
|
-
{ from: "B", to: "C", cost: 3 },
|
|
96
|
-
{ from: "C", to: "D", cost: 2 },
|
|
97
|
-
{ from: "A", to: "E", cost: 1.5 },
|
|
98
|
-
{ from: "E", to: "F", cost: 2 },
|
|
99
|
-
{ from: "F", to: "G", cost: 3 },
|
|
100
|
-
{ from: "G", to: "D", cost: 4 },
|
|
101
|
-
],
|
|
102
|
-
}
|
|
103
|
-
const start = "A"
|
|
104
|
-
const end = "D"
|
|
105
|
-
const expectedResult = ["A", "E", "F", "G", "D"]
|
|
106
|
-
|
|
107
|
-
expect(findPath(graph, start, end)).toStrictEqual(expectedResult)
|
|
108
|
-
})
|
|
109
|
-
|
|
110
|
-
test("it should find the best path in a board graph", () => {
|
|
111
|
-
const rows = 5
|
|
112
|
-
const columns = 5
|
|
113
|
-
const size = [rows, columns]
|
|
114
|
-
const filler = (i, j) => ({ id: `${i}${j}`, position: [i, j] })
|
|
115
|
-
const board = createBoard(size, filler)
|
|
116
|
-
|
|
117
|
-
const graph = {
|
|
118
|
-
nodes: board,
|
|
119
|
-
arcs: [],
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
for (let i = 0; i < rows; i++) {
|
|
123
|
-
for (let j = 0; j < columns; j++) {
|
|
124
|
-
const current = `${i}${j}`
|
|
125
|
-
if (i + 1 < rows) {
|
|
126
|
-
graph.arcs.push({
|
|
127
|
-
from: current,
|
|
128
|
-
to: down([i, j], size).join(""),
|
|
129
|
-
})
|
|
130
|
-
}
|
|
131
|
-
if (j + 1 < columns) {
|
|
132
|
-
graph.arcs.push({
|
|
133
|
-
from: current,
|
|
134
|
-
to: right([i, j], size).join(""),
|
|
135
|
-
})
|
|
136
|
-
}
|
|
137
|
-
if (i + 1 < rows && j + 1 < columns) {
|
|
138
|
-
graph.arcs.push({
|
|
139
|
-
from: current,
|
|
140
|
-
to: downRight([i, j], size).join(""),
|
|
141
|
-
})
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
const start = "00"
|
|
147
|
-
const end = "44"
|
|
148
|
-
const expectedResult = ["00", "11", "22", "33", "44"]
|
|
149
|
-
|
|
150
|
-
expect(findPath(graph, start, end)).toStrictEqual(expectedResult)
|
|
151
|
-
})
|
|
@@ -1,206 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest"
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
contains,
|
|
5
|
-
ensureArray,
|
|
6
|
-
isArray,
|
|
7
|
-
max,
|
|
8
|
-
min,
|
|
9
|
-
pop,
|
|
10
|
-
push,
|
|
11
|
-
remove,
|
|
12
|
-
} from "./array.js"
|
|
13
|
-
|
|
14
|
-
test("it should check if an array contains a value", () => {
|
|
15
|
-
const arr = [3, 2, 6, 1, 7, 4, 5]
|
|
16
|
-
const item = 1
|
|
17
|
-
const expectedResult = true
|
|
18
|
-
|
|
19
|
-
expect(contains(arr, item)).toBe(expectedResult)
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
test("it should check if an array contains an object", () => {
|
|
23
|
-
const arr = [
|
|
24
|
-
{ value: 3 },
|
|
25
|
-
{ value: 2 },
|
|
26
|
-
{ value: 6 },
|
|
27
|
-
{ value: 1 },
|
|
28
|
-
{ value: 7 },
|
|
29
|
-
{ value: 4 },
|
|
30
|
-
{ value: 5 },
|
|
31
|
-
]
|
|
32
|
-
const item = arr[3]
|
|
33
|
-
const expectedResult = true
|
|
34
|
-
|
|
35
|
-
expect(contains(arr, item)).toBe(expectedResult)
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
test("it should return the same array if the value is already an array", () => {
|
|
39
|
-
const arr = [1, "two", { three: 3 }]
|
|
40
|
-
|
|
41
|
-
expect(ensureArray(arr)).toBe(arr)
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
test("it should wrap the value in an array if it is not an array", () => {
|
|
45
|
-
const str = "hello"
|
|
46
|
-
|
|
47
|
-
expect(ensureArray(str)).toStrictEqual([str])
|
|
48
|
-
})
|
|
49
|
-
|
|
50
|
-
test("it should correctly identify an array", () => {
|
|
51
|
-
const arr = [1, "two", { three: 3 }]
|
|
52
|
-
|
|
53
|
-
expect(isArray(arr)).toBe(true)
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
test("it should correctly identify a non-array", () => {
|
|
57
|
-
const str = "hello"
|
|
58
|
-
|
|
59
|
-
expect(isArray(str)).toBe(false)
|
|
60
|
-
})
|
|
61
|
-
|
|
62
|
-
test("it should find the maximum value of an array", () => {
|
|
63
|
-
const arr = [3, 2, 6, 1, 7, 4, 5]
|
|
64
|
-
const expectedResult = 7
|
|
65
|
-
|
|
66
|
-
expect(max(arr)).toBe(expectedResult)
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
test("it should find the maximum value in an array of objects", () => {
|
|
70
|
-
const arr = [
|
|
71
|
-
{ value: 3 },
|
|
72
|
-
{ value: 2 },
|
|
73
|
-
{ value: 6 },
|
|
74
|
-
{ value: 1 },
|
|
75
|
-
{ value: 7 },
|
|
76
|
-
{ value: 4 },
|
|
77
|
-
{ value: 5 },
|
|
78
|
-
]
|
|
79
|
-
const comparator = (a, b) => b.value - a.value
|
|
80
|
-
const expectedResult = { value: 7 }
|
|
81
|
-
|
|
82
|
-
expect(max(arr, comparator)).toStrictEqual(expectedResult)
|
|
83
|
-
})
|
|
84
|
-
|
|
85
|
-
test("it should find the minimum value of an array", () => {
|
|
86
|
-
const arr = [3, 2, 6, 1, 7, 4, 5]
|
|
87
|
-
const expectedResult = 1
|
|
88
|
-
|
|
89
|
-
expect(min(arr)).toBe(expectedResult)
|
|
90
|
-
})
|
|
91
|
-
|
|
92
|
-
test("it should find the minimum value in an array of objects", () => {
|
|
93
|
-
const arr = [
|
|
94
|
-
{ value: 3 },
|
|
95
|
-
{ value: 2 },
|
|
96
|
-
{ value: 6 },
|
|
97
|
-
{ value: 1 },
|
|
98
|
-
{ value: 7 },
|
|
99
|
-
{ value: 4 },
|
|
100
|
-
{ value: 5 },
|
|
101
|
-
]
|
|
102
|
-
const comparator = (a, b) => b.value - a.value
|
|
103
|
-
const expectedResult = { value: 1 }
|
|
104
|
-
|
|
105
|
-
expect(min(arr, comparator)).toStrictEqual(expectedResult)
|
|
106
|
-
})
|
|
107
|
-
|
|
108
|
-
test("it should remove the minimum value from an array", () => {
|
|
109
|
-
const arr = [3, 2, 6, 1, 7, 4, 5]
|
|
110
|
-
const expectedResult = [3, 2, 6, 7, 4, 5]
|
|
111
|
-
|
|
112
|
-
expect(pop(arr)).toStrictEqual(expectedResult)
|
|
113
|
-
})
|
|
114
|
-
|
|
115
|
-
test("it should remove the minimum object from an array", () => {
|
|
116
|
-
const arr = [
|
|
117
|
-
{ value: 3 },
|
|
118
|
-
{ value: 2 },
|
|
119
|
-
{ value: 6 },
|
|
120
|
-
{ value: 1 },
|
|
121
|
-
{ value: 7 },
|
|
122
|
-
{ value: 4 },
|
|
123
|
-
{ value: 5 },
|
|
124
|
-
]
|
|
125
|
-
const comparator = (a, b) => b.value - a.value
|
|
126
|
-
const expectedResult = [
|
|
127
|
-
{ value: 3 },
|
|
128
|
-
{ value: 2 },
|
|
129
|
-
{ value: 6 },
|
|
130
|
-
{ value: 7 },
|
|
131
|
-
{ value: 4 },
|
|
132
|
-
{ value: 5 },
|
|
133
|
-
]
|
|
134
|
-
|
|
135
|
-
expect(pop(arr, comparator)).toStrictEqual(expectedResult)
|
|
136
|
-
})
|
|
137
|
-
|
|
138
|
-
test("it should remove the maximum value from an array", () => {
|
|
139
|
-
const arr = [3, 2, 6, 1, 7, 4, 5]
|
|
140
|
-
const comparator = (a, b) => a - b
|
|
141
|
-
const expectedResult = [3, 2, 6, 1, 4, 5]
|
|
142
|
-
|
|
143
|
-
expect(pop(arr, comparator)).toStrictEqual(expectedResult)
|
|
144
|
-
})
|
|
145
|
-
|
|
146
|
-
test("it should remove the maximum object from an array", () => {
|
|
147
|
-
const arr = [
|
|
148
|
-
{ value: 3 },
|
|
149
|
-
{ value: 2 },
|
|
150
|
-
{ value: 6 },
|
|
151
|
-
{ value: 1 },
|
|
152
|
-
{ value: 7 },
|
|
153
|
-
{ value: 4 },
|
|
154
|
-
{ value: 5 },
|
|
155
|
-
]
|
|
156
|
-
const comparator = (a, b) => a.value - b.value
|
|
157
|
-
const expectedResult = [
|
|
158
|
-
{ value: 3 },
|
|
159
|
-
{ value: 2 },
|
|
160
|
-
{ value: 6 },
|
|
161
|
-
{ value: 1 },
|
|
162
|
-
{ value: 4 },
|
|
163
|
-
{ value: 5 },
|
|
164
|
-
]
|
|
165
|
-
|
|
166
|
-
expect(pop(arr, comparator)).toStrictEqual(expectedResult)
|
|
167
|
-
})
|
|
168
|
-
|
|
169
|
-
test("it should add a value at the end of the array", () => {
|
|
170
|
-
const arr = [3, 2, 6, 1, 7, 4]
|
|
171
|
-
const item = 5
|
|
172
|
-
const expectedResult = [3, 2, 6, 1, 7, 4, 5]
|
|
173
|
-
|
|
174
|
-
expect(push(arr, item)).toStrictEqual(expectedResult)
|
|
175
|
-
})
|
|
176
|
-
|
|
177
|
-
test("it should remove a primitive value from an array", () => {
|
|
178
|
-
const arr = [3, 2, 6, 1, 7, 4, 5]
|
|
179
|
-
const item = 1
|
|
180
|
-
const expectedResult = [3, 2, 6, 7, 4, 5]
|
|
181
|
-
|
|
182
|
-
expect(remove(arr, item)).toStrictEqual(expectedResult)
|
|
183
|
-
})
|
|
184
|
-
|
|
185
|
-
test("it should remove an object from an array", () => {
|
|
186
|
-
const arr = [
|
|
187
|
-
{ value: 3 },
|
|
188
|
-
{ value: 2 },
|
|
189
|
-
{ value: 6 },
|
|
190
|
-
{ value: 1 },
|
|
191
|
-
{ value: 7 },
|
|
192
|
-
{ value: 4 },
|
|
193
|
-
{ value: 5 },
|
|
194
|
-
]
|
|
195
|
-
const item = arr[3]
|
|
196
|
-
const expectedResult = [
|
|
197
|
-
{ value: 3 },
|
|
198
|
-
{ value: 2 },
|
|
199
|
-
{ value: 6 },
|
|
200
|
-
{ value: 7 },
|
|
201
|
-
{ value: 4 },
|
|
202
|
-
{ value: 5 },
|
|
203
|
-
]
|
|
204
|
-
|
|
205
|
-
expect(remove(arr, item)).toStrictEqual(expectedResult)
|
|
206
|
-
})
|