@aidc-toolkit/utility 0.9.7-beta → 0.9.9-beta

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,252 +0,0 @@
1
- import { describe, expect, test } from "vitest";
2
- import { IteratorProxy } from "../src/index.js";
3
-
4
- const source: readonly string[] = [
5
- "1", "2", "3", "4", "5"
6
- ];
7
-
8
- function iterableSource(): Iterable<string> {
9
- return [...source];
10
- }
11
-
12
- function arraySource(): string[] {
13
- return [...source];
14
- }
15
-
16
- function iteratorSource(): Iterator<string> {
17
- return [...source][Symbol.iterator]();
18
- }
19
-
20
- function * generatorSource(): Generator<string> {
21
- for (const s of source) {
22
- yield s;
23
- }
24
- }
25
-
26
- function callbackSource(): string[] {
27
- return [...source];
28
- }
29
-
30
- describe("Basic", () => {
31
- function validateIterable(iterationSource: Iterator<string> | Iterable<string>): void {
32
- const iteratorProxy = IteratorProxy.from(iterationSource);
33
-
34
- expect(IteratorProxy.from(iteratorProxy)).toBe(iteratorProxy);
35
-
36
- // @ts-expect-error -- Property exists.
37
- expect(iteratorProxy["_initialIterable"]).toBe(iterationSource);
38
-
39
- expect(Array.from(iteratorProxy)).toStrictEqual(source);
40
- }
41
-
42
- test("Iterator proxy", () => {
43
- expect(IteratorProxy).not.toBe(Iterator);
44
- });
45
-
46
- test("Iterable", () => {
47
- validateIterable(iterableSource());
48
- });
49
-
50
- test("Array", () => {
51
- validateIterable(arraySource());
52
- });
53
-
54
- test("Iterator", () => {
55
- validateIterable(iteratorSource());
56
- });
57
-
58
- test("Generator", () => {
59
- validateIterable(generatorSource());
60
- });
61
-
62
- test("Callback", () => {
63
- validateIterable(callbackSource());
64
- });
65
- });
66
-
67
- describe("Helpers", () => {
68
- test("Map", () => {
69
- let count = 0;
70
-
71
- const mapIteratorProxy = IteratorProxy.from(source).map((element, index) => {
72
- expect(Number(element)).toBe(index + 1);
73
- expect(index).toBe(count++);
74
-
75
- return -count;
76
- });
77
-
78
- expect(count).toBe(0);
79
-
80
- let negativeCount = 0;
81
-
82
- for (const element of mapIteratorProxy) {
83
- expect(element).toBe(--negativeCount);
84
- }
85
-
86
- expect(count).toBe(source.length);
87
- });
88
-
89
- test("Flat map", () => {
90
- let count = 0;
91
-
92
- const flatMapIteratorProxy = IteratorProxy.from(source).flatMap((element, index) => {
93
- expect(Number(element)).toBe(index + 1);
94
- expect(index).toBe(count++);
95
-
96
- return [count, -count];
97
- });
98
-
99
- expect(count).toBe(0);
100
-
101
- let index = 0;
102
-
103
- for (const element of flatMapIteratorProxy) {
104
- const absoluteElement = Math.floor(index / 2) + 1;
105
-
106
- expect(element).toBe(index % 2 === 0 ? absoluteElement : -absoluteElement);
107
- index++;
108
- }
109
-
110
- expect(count).toBe(source.length);
111
- });
112
-
113
- test("Filter", () => {
114
- let count = 0;
115
-
116
- const filteredIterable = IteratorProxy.from(source).filter((element, index) => {
117
- expect(Number(element)).toBe(index + 1);
118
- expect(index).toBe(count++);
119
-
120
- return Number(element) % 2 === 0;
121
- });
122
-
123
- expect(count).toBe(0);
124
-
125
- let evenCount = 0;
126
-
127
- for (const element of filteredIterable) {
128
- const n = Number(element);
129
-
130
- expect(n % 2).toBe(0);
131
- expect(Math.floor((n - 1) / 2)).toBe(evenCount++);
132
- }
133
-
134
- expect(count).toBe(source.length);
135
- expect(evenCount).toBe(Math.floor(source.length / 2));
136
- });
137
-
138
- test("Take", () => {
139
- let count = 0;
140
-
141
- for (const element of IteratorProxy.from(source).take(3)) {
142
- expect(element).toBe(String(++count));
143
- }
144
-
145
- expect(count).toBe(3);
146
-
147
- count = 0;
148
-
149
- for (const element of IteratorProxy.from(source).take(source.length + 1)) {
150
- expect(element).toBe(String(++count));
151
- }
152
-
153
- expect(count).toBe(source.length);
154
-
155
- count = 0;
156
-
157
- for (const element of IteratorProxy.from(source).take(0)) {
158
- expect(element).toBe(String(++count));
159
- }
160
-
161
- expect(count).toBe(0);
162
- });
163
-
164
- test("Drop", () => {
165
- let count = 0;
166
-
167
- for (const element of IteratorProxy.from(source).drop(3)) {
168
- expect(element).toBe(String(++count + 3));
169
- }
170
-
171
- expect(count).toBe(source.length - 3);
172
-
173
- count = 0;
174
-
175
- for (const element of IteratorProxy.from(source).drop(0)) {
176
- expect(element).toBe(String(++count));
177
- }
178
-
179
- expect(count).toBe(source.length);
180
-
181
- count = 0;
182
-
183
- for (const element of IteratorProxy.from(source).drop(source.length)) {
184
- expect(element).toBe(String(++count));
185
- }
186
-
187
- expect(count).toBe(0);
188
- });
189
-
190
- test("To array", () => {
191
- const sourceToArray = IteratorProxy.from(source).toArray();
192
-
193
- expect(sourceToArray).not.toBe(source);
194
- expect(sourceToArray).toStrictEqual(source);
195
- });
196
-
197
- test("For each", () => {
198
- let count = 0;
199
-
200
- IteratorProxy.from(source).forEach((value, index) => {
201
- expect(Number(value)).toBe(index + 1);
202
- expect(index).toBe(count++);
203
- });
204
-
205
- expect(count).toBe(source.length);
206
- });
207
-
208
- test("Reduce no initial value", () => {
209
- let count = 0;
210
-
211
- expect(IteratorProxy.from(source).reduce((previousValue, currentValue, currentIndex) => {
212
- expect(Number(currentValue)).toBe(currentIndex + 1);
213
- expect(currentIndex - 1).toBe(count++);
214
-
215
- return previousValue + currentValue;
216
- })).toBe("".concat(...source));
217
-
218
- expect(count).toBe(source.length - 1);
219
-
220
- expect(() => IteratorProxy.from<string>([]).reduce(() => "")).toThrow("reduce() of empty iterator with no initial value");
221
- });
222
-
223
- test("Reduce initial value", () => {
224
- let count = 0;
225
-
226
- expect(IteratorProxy.from(source).reduce((previousValue, currentValue, currentIndex) => {
227
- expect(Number(currentValue)).toBe(currentIndex + 1);
228
- expect(currentIndex).toBe(count++);
229
-
230
- return previousValue + currentValue;
231
- }, "0")).toBe("0".concat(...source));
232
-
233
- expect(count).toBe(source.length);
234
-
235
- expect(IteratorProxy.from<string>([]).reduce(() => "", "0")).toBe("0");
236
- });
237
-
238
- test("Some", () => {
239
- expect(IteratorProxy.from(source).some(value => value === "3")).toBe(true);
240
- expect(IteratorProxy.from(source).some(value => value === "6")).toBe(false);
241
- });
242
-
243
- test("Every", () => {
244
- expect(IteratorProxy.from(source).every(value => Number(value) > 0)).toBe(true);
245
- expect(IteratorProxy.from(source).every(value => Number(value) < Number(source[source.length - 1]))).toBe(false);
246
- });
247
-
248
- test("Find", () => {
249
- expect(IteratorProxy.from(source).find(value => Number(value) % 3 === 0)).toBe("3");
250
- expect(IteratorProxy.from(source).find(value => Number(value) % 7 === 0)).toBeUndefined();
251
- });
252
- });
@@ -1,72 +0,0 @@
1
- import { I18NEnvironment, i18nInit } from "@aidc-toolkit/core";
2
- import { describe, expect, test } from "vitest";
3
- import { Sequencer } from "../src/index.js";
4
-
5
- await i18nInit(I18NEnvironment.CLI);
6
-
7
- describe("Sequence", () => {
8
- const sequencer1 = new Sequencer(10, 20);
9
- const sequencer2 = new Sequencer(29, -20);
10
-
11
- test("Structure", () => {
12
- expect(sequencer1.startValue).toBe(10n);
13
- expect(sequencer1.endValue).toBe(30n);
14
- expect(sequencer1.count).toBe(20);
15
- expect(sequencer1.minValue).toBe(10n);
16
- expect(sequencer1.maxValue).toBe(29n);
17
-
18
- expect(sequencer2.startValue).toBe(29n);
19
- expect(sequencer2.endValue).toBe(9n);
20
- expect(sequencer2.count).toBe(-20);
21
- expect(sequencer2.minValue).toBe(10n);
22
- expect(sequencer2.maxValue).toBe(29n);
23
- });
24
-
25
- test("Iteration", () => {
26
- let expectedValue: bigint;
27
- let count: number;
28
-
29
- expectedValue = 10n;
30
- count = 0;
31
-
32
- for (const value of Iterator.from(sequencer1)) {
33
- expect(value).toBe(expectedValue);
34
-
35
- expectedValue++;
36
- count++;
37
- }
38
-
39
- expect(count).toBe(20);
40
-
41
- expectedValue = 29n;
42
- count = 0;
43
-
44
- for (const value of Iterator.from(sequencer2)) {
45
- expect(value).toBe(expectedValue);
46
-
47
- expectedValue--;
48
- count++;
49
- }
50
-
51
- expect(count).toBe(20);
52
- });
53
-
54
- test("Reset", () => {
55
- let expectedValue: bigint;
56
- let count: number;
57
-
58
- expectedValue = 10n;
59
- count = 0;
60
-
61
- sequencer1.reset();
62
-
63
- for (const value of Iterator.from(sequencer1)) {
64
- expect(value).toBe(expectedValue);
65
-
66
- expectedValue++;
67
- count++;
68
- }
69
-
70
- expect(count).toBe(20);
71
- });
72
- });