@inglorious/utils 3.7.0 → 3.7.2

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.
@@ -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
- })
@@ -1,242 +0,0 @@
1
- import { expect, test } from "vitest"
2
-
3
- import { mod } from "../math/numbers.js"
4
- import {
5
- createBoard,
6
- down,
7
- downLeft,
8
- downRight,
9
- left,
10
- right,
11
- toString,
12
- up,
13
- upLeft,
14
- upRight,
15
- } from "./board.js"
16
-
17
- test("it should create an empty board", () => {
18
- const size = [4, 4]
19
- const expectedResult = [
20
- null,
21
- null,
22
- null,
23
- null,
24
- null,
25
- null,
26
- null,
27
- null,
28
- null,
29
- null,
30
- null,
31
- null,
32
- null,
33
- null,
34
- null,
35
- null,
36
- ]
37
-
38
- expect(createBoard(size)).toStrictEqual(expectedResult)
39
- })
40
-
41
- test("it should create a checkerboard", () => {
42
- const size = [4, 4]
43
- const filler = (i, j) => (!mod(i + j, 2) ? 1 : 0)
44
- const expectedResult = [1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1]
45
-
46
- expect(createBoard(size, filler)).toStrictEqual(expectedResult)
47
- })
48
-
49
- test("it should return the coordinates of the cell downward", () => {
50
- const size = [8, 8]
51
- const coordinates = [3, 4]
52
- const expectedResult = [4, 4]
53
-
54
- expect(down(coordinates, size)).toStrictEqual(expectedResult)
55
- })
56
-
57
- test("it should throw going down if cell is downmost", () => {
58
- const size = [8, 8]
59
- const coordinates = [7, 4]
60
-
61
- expect(() => down(coordinates, size)).toThrow()
62
- })
63
-
64
- test("it should return the coordinates of the cell down left", () => {
65
- const size = [8, 8]
66
- const coordinates = [3, 4]
67
- const expectedResult = [4, 3]
68
-
69
- expect(downLeft(coordinates, size)).toStrictEqual(expectedResult)
70
- })
71
-
72
- test("it should throw going down left if cell is downmost", () => {
73
- const size = [8, 8]
74
- const coordinates = [7, 4]
75
-
76
- expect(() => downLeft(coordinates, size)).toThrow()
77
- })
78
-
79
- test("it should throw going down left if cell is downmost", () => {
80
- const size = [8, 8]
81
- const coordinates = [3, 0]
82
-
83
- expect(() => downLeft(coordinates, size)).toThrow()
84
- })
85
-
86
- test("it should throw going down left if cell is on the angle", () => {
87
- const size = [8, 8]
88
- const coordinates = [7, 0]
89
-
90
- expect(() => downLeft(coordinates, size)).toThrow()
91
- })
92
-
93
- test("it should return the coordinates of the cell down right", () => {
94
- const size = [8, 8]
95
- const coordinates = [3, 4]
96
- const expectedResult = [4, 5]
97
-
98
- expect(downRight(coordinates, size)).toStrictEqual(expectedResult)
99
- })
100
-
101
- test("it should throw going down right if cell is downmost", () => {
102
- const size = [8, 8]
103
- const coordinates = [7, 4]
104
-
105
- expect(() => downRight(coordinates, size)).toThrow()
106
- })
107
-
108
- test("it should throw going down right if cell is downmost", () => {
109
- const size = [8, 8]
110
- const coordinates = [3, 7]
111
-
112
- expect(() => downRight(coordinates, size)).toThrow()
113
- })
114
-
115
- test("it should throw going down right if cell is on the angle", () => {
116
- const size = [8, 8]
117
- const coordinates = [7, 7]
118
-
119
- expect(() => downRight(coordinates, size)).toThrow()
120
- })
121
-
122
- test("it should return the coordinates of the cell to the left", () => {
123
- const size = [8, 8]
124
- const coordinates = [3, 4]
125
- const expectedResult = [3, 3]
126
-
127
- expect(left(coordinates, size)).toStrictEqual(expectedResult)
128
- })
129
-
130
- test("it should not return coordinates to the left if cell is leftmost", () => {
131
- const size = [8, 8]
132
- const coordinates = [3, 0]
133
-
134
- expect(() => left(coordinates, size)).toThrow()
135
- })
136
-
137
- test("it should return the coordinates of the cell to the right", () => {
138
- const size = [8, 8]
139
- const coordinates = [3, 4]
140
- const expectedResult = [3, 5]
141
-
142
- expect(right(coordinates, size)).toStrictEqual(expectedResult)
143
- })
144
-
145
- test("it should not return coordinates to the right if cell is rightmost", () => {
146
- const size = [8, 8]
147
- const coordinates = [3, 7]
148
-
149
- expect(() => right(coordinates, size)).toThrow()
150
- })
151
-
152
- test("it should return a string representation of the given board", () => {
153
- const board = [1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1]
154
-
155
- expect(toString(board, [4, 4])).toBe(`1 0 1 0
156
- 0 1 0 1
157
- 1 0 1 0
158
- 0 1 0 1`)
159
- })
160
-
161
- test("it should return a custom string representation of the given board", () => {
162
- const board = [1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1]
163
- const cellToString = (cell) => cell.toFixed(1)
164
-
165
- expect(toString(board, [4, 4], cellToString)).toBe(`1.0 0.0 1.0 0.0
166
- 0.0 1.0 0.0 1.0
167
- 1.0 0.0 1.0 0.0
168
- 0.0 1.0 0.0 1.0`)
169
- })
170
-
171
- test("it should return the coordinates of the cell upward", () => {
172
- const size = [8, 8]
173
- const coordinates = [3, 4]
174
- const expectedResult = [2, 4]
175
-
176
- expect(up(coordinates, size)).toStrictEqual(expectedResult)
177
- })
178
-
179
- test("it should throw going upward if cell is topmost", () => {
180
- const size = [8, 8]
181
- const coordinates = [0, 4]
182
-
183
- expect(() => up(coordinates, size)).toThrow()
184
- })
185
-
186
- test("it should return the coordinates of the cell up left", () => {
187
- const size = [8, 8]
188
- const coordinates = [3, 4]
189
- const expectedResult = [2, 3]
190
-
191
- expect(upLeft(coordinates, size)).toStrictEqual(expectedResult)
192
- })
193
-
194
- test("it should throw going up left if cell is upmost", () => {
195
- const size = [8, 8]
196
- const coordinates = [0, 4]
197
-
198
- expect(() => upLeft(coordinates, size)).toThrow()
199
- })
200
-
201
- test("it should throw going up left if cell is leftmost", () => {
202
- const size = [8, 8]
203
- const coordinates = [3, 0]
204
-
205
- expect(() => upLeft(coordinates, size)).toThrow()
206
- })
207
-
208
- test("it should throw going up left if cell is on the angle", () => {
209
- const size = [8, 8]
210
- const coordinates = [0, 0]
211
-
212
- expect(() => upLeft(coordinates, size)).toThrow()
213
- })
214
-
215
- test("it should return the coordinates of the cell up right", () => {
216
- const size = [8, 8]
217
- const coordinates = [3, 4]
218
- const expectedResult = [2, 5]
219
-
220
- expect(upRight(coordinates, size)).toStrictEqual(expectedResult)
221
- })
222
-
223
- test("it should throw going up right if cell is upmost", () => {
224
- const size = [8, 8]
225
- const coordinates = [0, 4]
226
-
227
- expect(() => upRight(coordinates, size)).toThrow()
228
- })
229
-
230
- test("it should throw going down right if cell is rightmost", () => {
231
- const size = [8, 8]
232
- const coordinates = [3, 7]
233
-
234
- expect(() => upRight(coordinates, size)).toThrow()
235
- })
236
-
237
- test("it should throw going down right if cell is on the angle", () => {
238
- const size = [8, 8]
239
- const coordinates = [0, 7]
240
-
241
- expect(() => upRight(coordinates, size)).toThrow()
242
- })
@@ -1,103 +0,0 @@
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
- })