@tkeron/tools 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/npm_deploy.yml +25 -28
- package/LICENSE +21 -21
- package/bun.lock +6 -4
- package/package.json +28 -23
- package/readme.md +3 -3
- package/src/getPaths.test.ts +492 -0
- package/src/getPaths.ts +112 -0
- package/src/index.ts +3 -2
- package/src/random.test.ts +135 -135
- package/src/random.ts +11 -11
- package/src/stack.test.ts +102 -102
- package/src/stack.ts +70 -70
- package/tsconfig.json +27 -27
- package/changelog.md +0 -3
package/src/random.test.ts
CHANGED
|
@@ -1,135 +1,135 @@
|
|
|
1
|
-
import { describe, it, expect } from "bun:test";
|
|
2
|
-
import { rng } from "./random";
|
|
3
|
-
|
|
4
|
-
describe("testing rng", () => {
|
|
5
|
-
it("should return the same 10 random numbers for seed=344", () => {
|
|
6
|
-
const numbers = [...rng(344, 10)];
|
|
7
|
-
const expected = [
|
|
8
|
-
88811757, 1786880006, -1901549690, -783089686, 1532981021, 1629592611,
|
|
9
|
-
102726481, -941665653, -1672827444, -141168747,
|
|
10
|
-
];
|
|
11
|
-
expect(numbers).toEqual(expected);
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
it("should return the same 100 random numbers for seed=158484", () => {
|
|
15
|
-
const numbers = [...rng(158484, 100)];
|
|
16
|
-
const expected = [
|
|
17
|
-
-512132828, 98688842, -15169138, 721513946, -771752345, -397269314,
|
|
18
|
-
1010577276, -2086534859, -1290576361, -1025237601, 1806465972, 2139370233,
|
|
19
|
-
-1028051820, 39829281, 1120459388, 1768530271, -183529410, -436067078,
|
|
20
|
-
-248249548, 1527105597, -1571897668, -960918928, 151404779, -495137626,
|
|
21
|
-
116267794, 1946452820, 1773133059, -738265980, -1600113581, 1667592070,
|
|
22
|
-
472076620, -1218993303, 611253364, -2014334716, 1210987907, 1565772620,
|
|
23
|
-
-1552845967, 787622462, 487465828, -181095051, 283030456, 329010540,
|
|
24
|
-
-603058444, -1480555620, 1104662757, -1029632768, -2094961536, 2023342296,
|
|
25
|
-
1356788665, 1536461133, -1659154829, 1852856121, -159388878, 1747745150,
|
|
26
|
-
125113631, 521001332, 541976517, 169589375, -673644939, -891357762,
|
|
27
|
-
-1722888502, 1382186657, -1417912518, 872079767, -1764820973, 1885020757,
|
|
28
|
-
-1776253955, 1304230546, -1891946683, -1331478612, 1032049895,
|
|
29
|
-
-1526859160, 1840120240, 513620220, -1982733411, 32420044, 689159639,
|
|
30
|
-
-1954077981, -1800022834, -341018408, -228713757, 290336290, 1170540679,
|
|
31
|
-
2120828749, -2036773459, 1253342555, 1727989076, 1289914814, -1484935724,
|
|
32
|
-
1243853399, -1321156605, -2125340477, 343773831, 1669764017, -1881013655,
|
|
33
|
-
977826430, -1259165731, -517468935, -162314014, 1990733797,
|
|
34
|
-
];
|
|
35
|
-
expect(numbers).toEqual(expected);
|
|
36
|
-
});
|
|
37
|
-
it.skip("should return info about series of 98688842", () => {
|
|
38
|
-
const seed = 98688842;
|
|
39
|
-
const gen = rng(seed);
|
|
40
|
-
const info = {
|
|
41
|
-
seed,
|
|
42
|
-
period: 0,
|
|
43
|
-
max: Number.MIN_SAFE_INTEGER,
|
|
44
|
-
min: Number.MAX_SAFE_INTEGER,
|
|
45
|
-
minPositive: Number.MAX_SAFE_INTEGER,
|
|
46
|
-
maxNegative: Number.MIN_SAFE_INTEGER,
|
|
47
|
-
};
|
|
48
|
-
while (true) {
|
|
49
|
-
const num = gen.next().value!;
|
|
50
|
-
|
|
51
|
-
if (num > info.max) info.max = num;
|
|
52
|
-
if (num < info.min) info.min = num;
|
|
53
|
-
|
|
54
|
-
if (num > 0 && num < info.minPositive) info.minPositive = num;
|
|
55
|
-
if (num < 0 && num > info.maxNegative) info.maxNegative = num;
|
|
56
|
-
|
|
57
|
-
info.period++;
|
|
58
|
-
|
|
59
|
-
if (num === seed) break;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
console.log(JSON.stringify(info, null, 4));
|
|
63
|
-
|
|
64
|
-
expect(info).toEqual({
|
|
65
|
-
seed: 98688842,
|
|
66
|
-
period: 1073741822,
|
|
67
|
-
max: 2147483647,
|
|
68
|
-
min: -2147483646,
|
|
69
|
-
minPositive: 1,
|
|
70
|
-
maxNegative: -4,
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
it.skip("should return info about series of numbers...", () => {
|
|
74
|
-
const seeds = [1, 4, 8];
|
|
75
|
-
const gens = seeds.map((s) => rng(s));
|
|
76
|
-
const infos = seeds.map((seed) => ({
|
|
77
|
-
seed,
|
|
78
|
-
period: 0,
|
|
79
|
-
max: Number.MIN_SAFE_INTEGER,
|
|
80
|
-
min: Number.MAX_SAFE_INTEGER,
|
|
81
|
-
minPositive: Number.MAX_SAFE_INTEGER,
|
|
82
|
-
maxNegative: Number.MIN_SAFE_INTEGER,
|
|
83
|
-
}));
|
|
84
|
-
let i = 0;
|
|
85
|
-
while (true) {
|
|
86
|
-
const info = infos[i];
|
|
87
|
-
const gen = gens[i];
|
|
88
|
-
const seed = seeds[i];
|
|
89
|
-
|
|
90
|
-
const num = gen.next().value!;
|
|
91
|
-
|
|
92
|
-
if (num > info.max) info.max = num;
|
|
93
|
-
if (num < info.min) info.min = num;
|
|
94
|
-
|
|
95
|
-
if (num > 0 && num < info.minPositive) info.minPositive = num;
|
|
96
|
-
if (num < 0 && num > info.maxNegative) info.maxNegative = num;
|
|
97
|
-
|
|
98
|
-
info.period++;
|
|
99
|
-
|
|
100
|
-
if (num === seed) break;
|
|
101
|
-
|
|
102
|
-
i++;
|
|
103
|
-
if (i >= seeds.length) i = 0;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
console.log(JSON.stringify(infos, null, 4));
|
|
107
|
-
|
|
108
|
-
expect(infos).toEqual([
|
|
109
|
-
{
|
|
110
|
-
seed: 1,
|
|
111
|
-
period: 1073741822,
|
|
112
|
-
max: 2147483647,
|
|
113
|
-
min: -2147483646,
|
|
114
|
-
minPositive: 1,
|
|
115
|
-
maxNegative: -4,
|
|
116
|
-
},
|
|
117
|
-
{
|
|
118
|
-
seed: 4,
|
|
119
|
-
period: 1073741821,
|
|
120
|
-
max: 2147483647,
|
|
121
|
-
min: -2147483646,
|
|
122
|
-
minPositive: 1,
|
|
123
|
-
maxNegative: -4,
|
|
124
|
-
},
|
|
125
|
-
{
|
|
126
|
-
seed: 8,
|
|
127
|
-
period: 1073741821,
|
|
128
|
-
max: 2147483647,
|
|
129
|
-
min: -2147483646,
|
|
130
|
-
minPositive: 1,
|
|
131
|
-
maxNegative: -4,
|
|
132
|
-
},
|
|
133
|
-
]);
|
|
134
|
-
});
|
|
135
|
-
});
|
|
1
|
+
import { describe, it, expect } from "bun:test";
|
|
2
|
+
import { rng } from "./random";
|
|
3
|
+
|
|
4
|
+
describe("testing rng", () => {
|
|
5
|
+
it("should return the same 10 random numbers for seed=344", () => {
|
|
6
|
+
const numbers = [...rng(344, 10)];
|
|
7
|
+
const expected = [
|
|
8
|
+
88811757, 1786880006, -1901549690, -783089686, 1532981021, 1629592611,
|
|
9
|
+
102726481, -941665653, -1672827444, -141168747,
|
|
10
|
+
];
|
|
11
|
+
expect(numbers).toEqual(expected);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it("should return the same 100 random numbers for seed=158484", () => {
|
|
15
|
+
const numbers = [...rng(158484, 100)];
|
|
16
|
+
const expected = [
|
|
17
|
+
-512132828, 98688842, -15169138, 721513946, -771752345, -397269314,
|
|
18
|
+
1010577276, -2086534859, -1290576361, -1025237601, 1806465972, 2139370233,
|
|
19
|
+
-1028051820, 39829281, 1120459388, 1768530271, -183529410, -436067078,
|
|
20
|
+
-248249548, 1527105597, -1571897668, -960918928, 151404779, -495137626,
|
|
21
|
+
116267794, 1946452820, 1773133059, -738265980, -1600113581, 1667592070,
|
|
22
|
+
472076620, -1218993303, 611253364, -2014334716, 1210987907, 1565772620,
|
|
23
|
+
-1552845967, 787622462, 487465828, -181095051, 283030456, 329010540,
|
|
24
|
+
-603058444, -1480555620, 1104662757, -1029632768, -2094961536, 2023342296,
|
|
25
|
+
1356788665, 1536461133, -1659154829, 1852856121, -159388878, 1747745150,
|
|
26
|
+
125113631, 521001332, 541976517, 169589375, -673644939, -891357762,
|
|
27
|
+
-1722888502, 1382186657, -1417912518, 872079767, -1764820973, 1885020757,
|
|
28
|
+
-1776253955, 1304230546, -1891946683, -1331478612, 1032049895,
|
|
29
|
+
-1526859160, 1840120240, 513620220, -1982733411, 32420044, 689159639,
|
|
30
|
+
-1954077981, -1800022834, -341018408, -228713757, 290336290, 1170540679,
|
|
31
|
+
2120828749, -2036773459, 1253342555, 1727989076, 1289914814, -1484935724,
|
|
32
|
+
1243853399, -1321156605, -2125340477, 343773831, 1669764017, -1881013655,
|
|
33
|
+
977826430, -1259165731, -517468935, -162314014, 1990733797,
|
|
34
|
+
];
|
|
35
|
+
expect(numbers).toEqual(expected);
|
|
36
|
+
});
|
|
37
|
+
it.skip("should return info about series of 98688842", () => {
|
|
38
|
+
const seed = 98688842;
|
|
39
|
+
const gen = rng(seed);
|
|
40
|
+
const info = {
|
|
41
|
+
seed,
|
|
42
|
+
period: 0,
|
|
43
|
+
max: Number.MIN_SAFE_INTEGER,
|
|
44
|
+
min: Number.MAX_SAFE_INTEGER,
|
|
45
|
+
minPositive: Number.MAX_SAFE_INTEGER,
|
|
46
|
+
maxNegative: Number.MIN_SAFE_INTEGER,
|
|
47
|
+
};
|
|
48
|
+
while (true) {
|
|
49
|
+
const num = gen.next().value!;
|
|
50
|
+
|
|
51
|
+
if (num > info.max) info.max = num;
|
|
52
|
+
if (num < info.min) info.min = num;
|
|
53
|
+
|
|
54
|
+
if (num > 0 && num < info.minPositive) info.minPositive = num;
|
|
55
|
+
if (num < 0 && num > info.maxNegative) info.maxNegative = num;
|
|
56
|
+
|
|
57
|
+
info.period++;
|
|
58
|
+
|
|
59
|
+
if (num === seed) break;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
console.log(JSON.stringify(info, null, 4));
|
|
63
|
+
|
|
64
|
+
expect(info).toEqual({
|
|
65
|
+
seed: 98688842,
|
|
66
|
+
period: 1073741822,
|
|
67
|
+
max: 2147483647,
|
|
68
|
+
min: -2147483646,
|
|
69
|
+
minPositive: 1,
|
|
70
|
+
maxNegative: -4,
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
it.skip("should return info about series of numbers...", () => {
|
|
74
|
+
const seeds = [1, 4, 8];
|
|
75
|
+
const gens = seeds.map((s) => rng(s));
|
|
76
|
+
const infos = seeds.map((seed) => ({
|
|
77
|
+
seed,
|
|
78
|
+
period: 0,
|
|
79
|
+
max: Number.MIN_SAFE_INTEGER,
|
|
80
|
+
min: Number.MAX_SAFE_INTEGER,
|
|
81
|
+
minPositive: Number.MAX_SAFE_INTEGER,
|
|
82
|
+
maxNegative: Number.MIN_SAFE_INTEGER,
|
|
83
|
+
}));
|
|
84
|
+
let i = 0;
|
|
85
|
+
while (true) {
|
|
86
|
+
const info = infos[i];
|
|
87
|
+
const gen = gens[i];
|
|
88
|
+
const seed = seeds[i];
|
|
89
|
+
|
|
90
|
+
const num = gen.next().value!;
|
|
91
|
+
|
|
92
|
+
if (num > info.max) info.max = num;
|
|
93
|
+
if (num < info.min) info.min = num;
|
|
94
|
+
|
|
95
|
+
if (num > 0 && num < info.minPositive) info.minPositive = num;
|
|
96
|
+
if (num < 0 && num > info.maxNegative) info.maxNegative = num;
|
|
97
|
+
|
|
98
|
+
info.period++;
|
|
99
|
+
|
|
100
|
+
if (num === seed) break;
|
|
101
|
+
|
|
102
|
+
i++;
|
|
103
|
+
if (i >= seeds.length) i = 0;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
console.log(JSON.stringify(infos, null, 4));
|
|
107
|
+
|
|
108
|
+
expect(infos).toEqual([
|
|
109
|
+
{
|
|
110
|
+
seed: 1,
|
|
111
|
+
period: 1073741822,
|
|
112
|
+
max: 2147483647,
|
|
113
|
+
min: -2147483646,
|
|
114
|
+
minPositive: 1,
|
|
115
|
+
maxNegative: -4,
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
seed: 4,
|
|
119
|
+
period: 1073741821,
|
|
120
|
+
max: 2147483647,
|
|
121
|
+
min: -2147483646,
|
|
122
|
+
minPositive: 1,
|
|
123
|
+
maxNegative: -4,
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
seed: 8,
|
|
127
|
+
period: 1073741821,
|
|
128
|
+
max: 2147483647,
|
|
129
|
+
min: -2147483646,
|
|
130
|
+
minPositive: 1,
|
|
131
|
+
maxNegative: -4,
|
|
132
|
+
},
|
|
133
|
+
]);
|
|
134
|
+
});
|
|
135
|
+
});
|
package/src/random.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
export function* rng(seed = 0, limit = 0) {
|
|
2
|
-
let current = 0;
|
|
3
|
-
while (true) {
|
|
4
|
-
seed ^= seed << 13;
|
|
5
|
-
seed ^= seed >> 17;
|
|
6
|
-
seed ^= seed << 5;
|
|
7
|
-
yield seed;
|
|
8
|
-
if (limit > 0) current++;
|
|
9
|
-
if (limit > 0 && current >= limit) break;
|
|
10
|
-
}
|
|
11
|
-
}
|
|
1
|
+
export function* rng(seed = 0, limit = 0) {
|
|
2
|
+
let current = 0;
|
|
3
|
+
while (true) {
|
|
4
|
+
seed ^= seed << 13;
|
|
5
|
+
seed ^= seed >> 17;
|
|
6
|
+
seed ^= seed << 5;
|
|
7
|
+
yield seed;
|
|
8
|
+
if (limit > 0) current++;
|
|
9
|
+
if (limit > 0 && current >= limit) break;
|
|
10
|
+
}
|
|
11
|
+
}
|
package/src/stack.test.ts
CHANGED
|
@@ -1,102 +1,102 @@
|
|
|
1
|
-
import { describe, it, expect } from "bun:test";
|
|
2
|
-
import { getFIFO } from "./stack";
|
|
3
|
-
import { getLIFO } from "./stack";
|
|
4
|
-
|
|
5
|
-
describe("LIFO stack tests", () => {
|
|
6
|
-
it("should create an empty lifo stack", () => {
|
|
7
|
-
const lifo = getLIFO<number>();
|
|
8
|
-
expect(lifo.length).toBe(0);
|
|
9
|
-
expect(lifo.current).toBe(null);
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
it("should push an item to the lifo stack", () => {
|
|
13
|
-
const lifo = getLIFO<number>();
|
|
14
|
-
lifo.push(1);
|
|
15
|
-
expect(lifo.length).toBe(1);
|
|
16
|
-
expect(lifo.current).toBe(1);
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
it("should pop an item from the lifo stack", () => {
|
|
20
|
-
const lifo = getLIFO<number>();
|
|
21
|
-
lifo.push(1);
|
|
22
|
-
expect(lifo.current).toBe(1);
|
|
23
|
-
expect(lifo.pop()).toBe(1);
|
|
24
|
-
expect(lifo.length).toBe(0);
|
|
25
|
-
expect(lifo.current).toBe(null);
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
it("should push multiple items to the lifo stack", () => {
|
|
29
|
-
const lifo = getLIFO<number>();
|
|
30
|
-
lifo.push(1);
|
|
31
|
-
lifo.push(2);
|
|
32
|
-
lifo.push(3);
|
|
33
|
-
expect(lifo.length).toBe(3);
|
|
34
|
-
expect(lifo.current).toBe(3);
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
it("should pop multiple items from the lifo stack", () => {
|
|
38
|
-
const lifo = getLIFO<number>();
|
|
39
|
-
lifo.push(1);
|
|
40
|
-
lifo.push(2);
|
|
41
|
-
lifo.push(3);
|
|
42
|
-
expect(lifo.pop()).toBe(3);
|
|
43
|
-
expect(lifo.pop()).toBe(2);
|
|
44
|
-
expect(lifo.pop()).toBe(1);
|
|
45
|
-
expect(lifo.length).toBe(0);
|
|
46
|
-
expect(lifo.current).toBe(null);
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
it("verify LIFO behavior with direct current modification", () => {
|
|
50
|
-
const lifo = getLIFO<number>();
|
|
51
|
-
lifo.push(1);
|
|
52
|
-
lifo.push(2);
|
|
53
|
-
lifo.current = 3;
|
|
54
|
-
expect(lifo.length).toBe(2);
|
|
55
|
-
expect(lifo.current).toBe(3);
|
|
56
|
-
});
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
describe("FIFO stack tests", () => {
|
|
60
|
-
it("should create an empty fifo stack", () => {
|
|
61
|
-
const fifo = getFIFO<number>();
|
|
62
|
-
expect(fifo.length).toBe(0);
|
|
63
|
-
expect(fifo.current).toBe(null);
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
it("should push an item to the fifo stack", () => {
|
|
67
|
-
const fifo = getFIFO<number>();
|
|
68
|
-
fifo.push(1);
|
|
69
|
-
expect(fifo.length).toBe(1);
|
|
70
|
-
expect(fifo.current).toBe(1);
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it("should pop an item from the fifo stack", () => {
|
|
74
|
-
const fifo = getFIFO<number>();
|
|
75
|
-
fifo.push(1);
|
|
76
|
-
expect(fifo.current).toBe(1);
|
|
77
|
-
expect(fifo.pop()).toBe(1);
|
|
78
|
-
expect(fifo.length).toBe(0);
|
|
79
|
-
expect(fifo.current).toBe(null);
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
it("should push multiple items to the fifo stack", () => {
|
|
83
|
-
const fifo = getFIFO<number>();
|
|
84
|
-
fifo.push(1);
|
|
85
|
-
fifo.push(2);
|
|
86
|
-
fifo.push(3);
|
|
87
|
-
expect(fifo.length).toBe(3);
|
|
88
|
-
expect(fifo.current).toBe(1);
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
it("should pop multiple items from the fifo stack", () => {
|
|
92
|
-
const fifo = getFIFO<number>();
|
|
93
|
-
fifo.push(1);
|
|
94
|
-
fifo.push(2);
|
|
95
|
-
fifo.push(3);
|
|
96
|
-
expect(fifo.pop()).toBe(1);
|
|
97
|
-
expect(fifo.pop()).toBe(2);
|
|
98
|
-
expect(fifo.pop()).toBe(3);
|
|
99
|
-
expect(fifo.length).toBe(0);
|
|
100
|
-
expect(fifo.current).toBe(null);
|
|
101
|
-
});
|
|
102
|
-
});
|
|
1
|
+
import { describe, it, expect } from "bun:test";
|
|
2
|
+
import { getFIFO } from "./stack";
|
|
3
|
+
import { getLIFO } from "./stack";
|
|
4
|
+
|
|
5
|
+
describe("LIFO stack tests", () => {
|
|
6
|
+
it("should create an empty lifo stack", () => {
|
|
7
|
+
const lifo = getLIFO<number>();
|
|
8
|
+
expect(lifo.length).toBe(0);
|
|
9
|
+
expect(lifo.current).toBe(null);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it("should push an item to the lifo stack", () => {
|
|
13
|
+
const lifo = getLIFO<number>();
|
|
14
|
+
lifo.push(1);
|
|
15
|
+
expect(lifo.length).toBe(1);
|
|
16
|
+
expect(lifo.current).toBe(1);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("should pop an item from the lifo stack", () => {
|
|
20
|
+
const lifo = getLIFO<number>();
|
|
21
|
+
lifo.push(1);
|
|
22
|
+
expect(lifo.current).toBe(1);
|
|
23
|
+
expect(lifo.pop()).toBe(1);
|
|
24
|
+
expect(lifo.length).toBe(0);
|
|
25
|
+
expect(lifo.current).toBe(null);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("should push multiple items to the lifo stack", () => {
|
|
29
|
+
const lifo = getLIFO<number>();
|
|
30
|
+
lifo.push(1);
|
|
31
|
+
lifo.push(2);
|
|
32
|
+
lifo.push(3);
|
|
33
|
+
expect(lifo.length).toBe(3);
|
|
34
|
+
expect(lifo.current).toBe(3);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("should pop multiple items from the lifo stack", () => {
|
|
38
|
+
const lifo = getLIFO<number>();
|
|
39
|
+
lifo.push(1);
|
|
40
|
+
lifo.push(2);
|
|
41
|
+
lifo.push(3);
|
|
42
|
+
expect(lifo.pop()).toBe(3);
|
|
43
|
+
expect(lifo.pop()).toBe(2);
|
|
44
|
+
expect(lifo.pop()).toBe(1);
|
|
45
|
+
expect(lifo.length).toBe(0);
|
|
46
|
+
expect(lifo.current).toBe(null);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("verify LIFO behavior with direct current modification", () => {
|
|
50
|
+
const lifo = getLIFO<number>();
|
|
51
|
+
lifo.push(1);
|
|
52
|
+
lifo.push(2);
|
|
53
|
+
lifo.current = 3;
|
|
54
|
+
expect(lifo.length).toBe(2);
|
|
55
|
+
expect(lifo.current).toBe(3);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
describe("FIFO stack tests", () => {
|
|
60
|
+
it("should create an empty fifo stack", () => {
|
|
61
|
+
const fifo = getFIFO<number>();
|
|
62
|
+
expect(fifo.length).toBe(0);
|
|
63
|
+
expect(fifo.current).toBe(null);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("should push an item to the fifo stack", () => {
|
|
67
|
+
const fifo = getFIFO<number>();
|
|
68
|
+
fifo.push(1);
|
|
69
|
+
expect(fifo.length).toBe(1);
|
|
70
|
+
expect(fifo.current).toBe(1);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("should pop an item from the fifo stack", () => {
|
|
74
|
+
const fifo = getFIFO<number>();
|
|
75
|
+
fifo.push(1);
|
|
76
|
+
expect(fifo.current).toBe(1);
|
|
77
|
+
expect(fifo.pop()).toBe(1);
|
|
78
|
+
expect(fifo.length).toBe(0);
|
|
79
|
+
expect(fifo.current).toBe(null);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("should push multiple items to the fifo stack", () => {
|
|
83
|
+
const fifo = getFIFO<number>();
|
|
84
|
+
fifo.push(1);
|
|
85
|
+
fifo.push(2);
|
|
86
|
+
fifo.push(3);
|
|
87
|
+
expect(fifo.length).toBe(3);
|
|
88
|
+
expect(fifo.current).toBe(1);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("should pop multiple items from the fifo stack", () => {
|
|
92
|
+
const fifo = getFIFO<number>();
|
|
93
|
+
fifo.push(1);
|
|
94
|
+
fifo.push(2);
|
|
95
|
+
fifo.push(3);
|
|
96
|
+
expect(fifo.pop()).toBe(1);
|
|
97
|
+
expect(fifo.pop()).toBe(2);
|
|
98
|
+
expect(fifo.pop()).toBe(3);
|
|
99
|
+
expect(fifo.length).toBe(0);
|
|
100
|
+
expect(fifo.current).toBe(null);
|
|
101
|
+
});
|
|
102
|
+
});
|
package/src/stack.ts
CHANGED
|
@@ -1,70 +1,70 @@
|
|
|
1
|
-
|
|
2
|
-
export interface Stack<T> {
|
|
3
|
-
pop: () => T;
|
|
4
|
-
push: (item: T) => void;
|
|
5
|
-
readonly length: number;
|
|
6
|
-
current: T | null;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Returns a LIFO (Last In First Out) stack.
|
|
11
|
-
* Same as FILO (First In Last Out) stack.
|
|
12
|
-
* @returns an empty LIFO stack.
|
|
13
|
-
*/
|
|
14
|
-
export const getLIFO = <T>(): Stack<T> => {
|
|
15
|
-
|
|
16
|
-
const _stack: T[] = [];
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const stack = <Stack<T>>{
|
|
20
|
-
pop: () => _stack.pop(),
|
|
21
|
-
push: (item) => _stack.push(item),
|
|
22
|
-
length: 0,
|
|
23
|
-
current: <T><unknown>null
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
Object.defineProperty(stack, "current", {
|
|
27
|
-
get: () => _stack.length ? _stack[_stack.length - 1] : null,
|
|
28
|
-
set: (value) => {
|
|
29
|
-
if (_stack.length) _stack[_stack.length - 1] = value;
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
Object.defineProperty(stack, "length", {
|
|
34
|
-
get: () => _stack.length,
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
return stack;
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Returns a FIFO (First In First Out) stack
|
|
45
|
-
* @returns an empty FIFO stack
|
|
46
|
-
*/
|
|
47
|
-
export const getFIFO = <T>(): Stack<T> => {
|
|
48
|
-
|
|
49
|
-
const _stack: T[] = [];
|
|
50
|
-
const stack = <Stack<T>>{
|
|
51
|
-
pop: () => _stack.shift(),
|
|
52
|
-
push: (item) => _stack.push(item),
|
|
53
|
-
length: 0,
|
|
54
|
-
current: <T><unknown>null
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
Object.defineProperty(stack, "current", {
|
|
58
|
-
get: () => _stack.length ? _stack[0] : null,
|
|
59
|
-
set: (value) => {
|
|
60
|
-
if (_stack.length) _stack[0] = value;
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
Object.defineProperty(stack, "length", {
|
|
65
|
-
get: () => _stack.length,
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
return stack;
|
|
69
|
-
|
|
70
|
-
}
|
|
1
|
+
|
|
2
|
+
export interface Stack<T> {
|
|
3
|
+
pop: () => T;
|
|
4
|
+
push: (item: T) => void;
|
|
5
|
+
readonly length: number;
|
|
6
|
+
current: T | null;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Returns a LIFO (Last In First Out) stack.
|
|
11
|
+
* Same as FILO (First In Last Out) stack.
|
|
12
|
+
* @returns an empty LIFO stack.
|
|
13
|
+
*/
|
|
14
|
+
export const getLIFO = <T>(): Stack<T> => {
|
|
15
|
+
|
|
16
|
+
const _stack: T[] = [];
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
const stack = <Stack<T>>{
|
|
20
|
+
pop: () => _stack.pop(),
|
|
21
|
+
push: (item) => _stack.push(item),
|
|
22
|
+
length: 0,
|
|
23
|
+
current: <T><unknown>null
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
Object.defineProperty(stack, "current", {
|
|
27
|
+
get: () => _stack.length ? _stack[_stack.length - 1] : null,
|
|
28
|
+
set: (value) => {
|
|
29
|
+
if (_stack.length) _stack[_stack.length - 1] = value;
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
Object.defineProperty(stack, "length", {
|
|
34
|
+
get: () => _stack.length,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
return stack;
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Returns a FIFO (First In First Out) stack
|
|
45
|
+
* @returns an empty FIFO stack
|
|
46
|
+
*/
|
|
47
|
+
export const getFIFO = <T>(): Stack<T> => {
|
|
48
|
+
|
|
49
|
+
const _stack: T[] = [];
|
|
50
|
+
const stack = <Stack<T>>{
|
|
51
|
+
pop: () => _stack.shift(),
|
|
52
|
+
push: (item) => _stack.push(item),
|
|
53
|
+
length: 0,
|
|
54
|
+
current: <T><unknown>null
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
Object.defineProperty(stack, "current", {
|
|
58
|
+
get: () => _stack.length ? _stack[0] : null,
|
|
59
|
+
set: (value) => {
|
|
60
|
+
if (_stack.length) _stack[0] = value;
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
Object.defineProperty(stack, "length", {
|
|
65
|
+
get: () => _stack.length,
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
return stack;
|
|
69
|
+
|
|
70
|
+
}
|