@aidc-toolkit/utility 0.9.0 → 0.9.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,282 +0,0 @@
1
- import { describe, expect, test } from "vitest";
2
- import { IterationHelper, type IterationSource } from "../src/index.js";
3
-
4
- const source: readonly string[] = [
5
- "1", "2", "3", "4", "5"
6
- ];
7
-
8
- // eslint-disable-next-line jsdoc/require-jsdoc
9
- function iterableSource(): Iterable<string> {
10
- return [...source];
11
- }
12
-
13
- // eslint-disable-next-line jsdoc/require-jsdoc
14
- function arraySource(): string[] {
15
- return [...source];
16
- }
17
-
18
- // eslint-disable-next-line jsdoc/require-jsdoc
19
- function iteratorSource(): Iterator<string> {
20
- return [...source][Symbol.iterator]();
21
- }
22
-
23
- // eslint-disable-next-line jsdoc/require-jsdoc
24
- function * generatorSource(): Generator<string> {
25
- for (const s of source) {
26
- yield s;
27
- }
28
- }
29
-
30
- // eslint-disable-next-line jsdoc/require-jsdoc
31
- function callbackSource(): string[] {
32
- return [...source];
33
- }
34
-
35
- describe("Iterable", () => {
36
- // eslint-disable-next-line jsdoc/require-jsdoc
37
- function validateIterable(iterationSource: IterationSource<string>, testEquality: boolean): void {
38
- const iterationHelper = IterationHelper.from(iterationSource);
39
-
40
- expect(IterationHelper.from(iterationHelper)).toBe(iterationHelper);
41
- expect(iterationHelper.iterationSource).toBe(iterationSource);
42
-
43
- expect(iterationHelper.asIterable() === iterationSource).toBe(testEquality);
44
- expect(Array.from(iterationHelper)).toStrictEqual(source);
45
- }
46
-
47
- test("Iterable", () => {
48
- validateIterable(iterableSource, false);
49
- validateIterable(iterableSource(), true);
50
- });
51
-
52
- test("Array", () => {
53
- validateIterable(arraySource, false);
54
- validateIterable(arraySource(), true);
55
- });
56
-
57
- test("Iterator", () => {
58
- validateIterable(iteratorSource, false);
59
- validateIterable(iteratorSource(), true);
60
- });
61
-
62
- test("Generator", () => {
63
- validateIterable(generatorSource, false);
64
- validateIterable(generatorSource(), true);
65
- });
66
-
67
- test("Callback", () => {
68
- validateIterable(callbackSource, false);
69
- validateIterable(callbackSource(), true);
70
- });
71
- });
72
-
73
- describe("Array", () => {
74
- // eslint-disable-next-line jsdoc/require-jsdoc
75
- function validateArray(iterationSource: IterationSource<string>, testEquality: boolean): void {
76
- const iterationHelper = IterationHelper.from(iterationSource);
77
-
78
- expect(IterationHelper.from(iterationHelper)).toBe(iterationHelper);
79
- expect(iterationHelper.iterationSource).toBe(iterationSource);
80
-
81
- const array = iterationHelper.asArray();
82
-
83
- expect(array === iterationSource).toBe(testEquality);
84
- expect(array).toStrictEqual(source);
85
- }
86
-
87
- test("Iterable", () => {
88
- validateArray(iterableSource, false);
89
- validateArray(iterableSource(), true);
90
- });
91
-
92
- test("Array", () => {
93
- validateArray(arraySource, false);
94
- validateArray(arraySource(), true);
95
- });
96
-
97
- test("Iterator", () => {
98
- validateArray(iteratorSource, false);
99
- validateArray(iteratorSource(), false);
100
- });
101
-
102
- test("Generator", () => {
103
- validateArray(generatorSource, false);
104
- validateArray(generatorSource(), false);
105
- });
106
-
107
- test("Callback", () => {
108
- validateArray(callbackSource, false);
109
- validateArray(callbackSource(), true);
110
- });
111
- });
112
-
113
- describe("Iterator", () => {
114
- // eslint-disable-next-line jsdoc/require-jsdoc
115
- function validateIterator(iterationSource: IterationSource<string>, testEquality: boolean): void {
116
- const iterationHelper = IterationHelper.from(iterationSource);
117
-
118
- expect(IterationHelper.from(iterationHelper)).toBe(iterationHelper);
119
- expect(iterationHelper.iterationSource).toBe(iterationSource);
120
-
121
- const iterator = iterationHelper.asIterator();
122
-
123
- expect(iterator === iterationSource).toBe(testEquality);
124
- expect(Array.from({
125
- [Symbol.iterator](): Iterator<string> {
126
- return iterator;
127
- }
128
- })).toStrictEqual(source);
129
- }
130
-
131
- test("Iterable", () => {
132
- validateIterator(iterableSource, false);
133
- validateIterator(iterableSource(), false);
134
- });
135
-
136
- test("Array", () => {
137
- validateIterator(arraySource, false);
138
- validateIterator(arraySource(), false);
139
- });
140
-
141
- test("Iterator", () => {
142
- validateIterator(iteratorSource, false);
143
- validateIterator(iteratorSource(), true);
144
- });
145
-
146
- test("Generator", () => {
147
- validateIterator(generatorSource, false);
148
- validateIterator(generatorSource(), true);
149
- });
150
-
151
- test("Callback", () => {
152
- validateIterator(callbackSource, false);
153
- validateIterator(callbackSource(), false);
154
- });
155
- });
156
-
157
- describe("Callback", () => {
158
- // eslint-disable-next-line jsdoc/require-jsdoc
159
- function validateCallback(iterationSource: IterationSource<string>, testEquality: boolean): void {
160
- const iterationHelper = IterationHelper.from(iterationSource);
161
-
162
- expect(IterationHelper.from(iterationHelper)).toBe(iterationHelper);
163
- expect(iterationHelper.iterationSource).toBe(iterationSource);
164
-
165
- expect(iterationHelper.asCallback() === iterationSource).toBe(testEquality);
166
- expect(iterationHelper.asArray()).toStrictEqual(source);
167
- }
168
-
169
- test("Iterable", () => {
170
- validateCallback(iterableSource, true);
171
- validateCallback(iterableSource(), false);
172
- });
173
-
174
- test("Array", () => {
175
- validateCallback(arraySource, true);
176
- validateCallback(arraySource(), false);
177
- });
178
-
179
- test("Iterator", () => {
180
- validateCallback(iteratorSource, true);
181
- validateCallback(iteratorSource(), false);
182
- });
183
-
184
- test("Generator", () => {
185
- validateCallback(generatorSource, true);
186
- validateCallback(generatorSource(), false);
187
- });
188
-
189
- test("Callback", () => {
190
- validateCallback(callbackSource, true);
191
- validateCallback(callbackSource(), false);
192
- });
193
- });
194
-
195
- describe("Helpers", () => {
196
- test("For each", () => {
197
- let count = 0;
198
-
199
- IterationHelper.from(source).forEach((value, index) => {
200
- expect(Number(value)).toBe(index + 1);
201
- expect(index).toBe(count++);
202
- });
203
-
204
- expect(count).toBe(source.length);
205
- });
206
-
207
- test("Map", () => {
208
- let count = 0;
209
-
210
- const mappedIterationHelper = IterationHelper.from(source).map((element, index) => {
211
- expect(Number(element)).toBe(index + 1);
212
- expect(index).toBe(count++);
213
-
214
- return -count;
215
- });
216
-
217
- expect(count).toBe(0);
218
-
219
- let negativeCount = 0;
220
-
221
- for (const element of mappedIterationHelper) {
222
- expect(element).toBe(--negativeCount);
223
- }
224
-
225
- expect(count).toBe(source.length);
226
- });
227
-
228
- test("Filter", () => {
229
- let count = 0;
230
-
231
- const filteredIterable = IterationHelper.from(source).filter((element, index) => {
232
- expect(Number(element)).toBe(index + 1);
233
- expect(index).toBe(count++);
234
-
235
- return Number(element) % 2 === 0;
236
- });
237
-
238
- expect(count).toBe(0);
239
-
240
- let evenCount = 0;
241
-
242
- for (const element of filteredIterable) {
243
- const n = Number(element);
244
-
245
- expect(n % 2).toBe(0);
246
- expect(Math.floor((n - 1) / 2)).toBe(evenCount++);
247
- }
248
-
249
- expect(count).toBe(source.length);
250
- expect(evenCount).toBe(Math.floor(source.length / 2));
251
- });
252
-
253
- test("Reduce no initial value", () => {
254
- let count = 0;
255
-
256
- expect(IterationHelper.from(source).reduce((previousValue, currentValue, currentIndex) => {
257
- expect(Number(currentValue)).toBe(currentIndex + 1);
258
- expect(currentIndex - 1).toBe(count++);
259
-
260
- return previousValue + currentValue;
261
- })).toBe("".concat(...source));
262
-
263
- expect(count).toBe(source.length - 1);
264
-
265
- expect(() => IterationHelper.from<string>([]).reduce(() => "")).toThrow("reduce() of empty iterator with no initial value");
266
- });
267
-
268
- test("Reduce initial value", () => {
269
- let count = 0;
270
-
271
- expect(IterationHelper.from(source).reduce((previousValue, currentValue, currentIndex) => {
272
- expect(Number(currentValue)).toBe(currentIndex + 1);
273
- expect(currentIndex).toBe(count++);
274
-
275
- return previousValue + currentValue;
276
- }, "0")).toBe("0".concat(...source));
277
-
278
- expect(count).toBe(source.length);
279
-
280
- expect(IterationHelper.from<string>([]).reduce(() => "", "0")).toBe("0");
281
- });
282
- });