@aidc-toolkit/utility 0.9.4 → 0.9.5
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/build-publish.yml +20 -0
- package/dist/index.cjs +1714 -0
- package/dist/index.d.cts +909 -0
- package/dist/index.d.ts +909 -0
- package/dist/index.js +1674 -0
- package/package.json +5 -4
- package/src/iterator_proxy.ts +175 -187
- package/test/character_set.test.ts +4 -4
- package/test/iterator_proxy.test.ts +34 -2
- package/test/record.test.ts +1 -1
- package/test/reg_exp.test.ts +1 -1
- package/test/sequencer.test.ts +1 -1
- package/test/transformer.test.ts +3 -3
- package/.github/workflows/npm-publish.yml +0 -38
- package/.idea/runConfigurations/build_dev.xml +0 -12
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aidc-toolkit/utility",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.5",
|
|
4
4
|
"description": "Foundational utilities for AIDC Toolkit",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"test": "vitest run"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"@aidc-toolkit/dev": "^0.9.
|
|
28
|
+
"@aidc-toolkit/dev": "^0.9.5",
|
|
29
29
|
"eslint": "^9.15.0",
|
|
30
30
|
"ts-node": "^10.9.2",
|
|
31
31
|
"tsup": "^8.3.5",
|
|
@@ -33,7 +33,8 @@
|
|
|
33
33
|
"vitest": "^2.1.5"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@aidc-toolkit/core": "^0.9.
|
|
37
|
-
"
|
|
36
|
+
"@aidc-toolkit/core": "^0.9.5",
|
|
37
|
+
"@rollup/rollup-linux-x64-gnu": "^4.27.3",
|
|
38
|
+
"i18next": "^23.16.8"
|
|
38
39
|
}
|
|
39
40
|
}
|
package/src/iterator_proxy.ts
CHANGED
|
@@ -1,59 +1,34 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Determine if Iterator variable is supported.
|
|
3
|
-
*
|
|
4
|
-
* @returns
|
|
5
|
-
* True if Iterator variable is supported.
|
|
6
|
-
*/
|
|
7
|
-
function isIteratorSupported(): boolean {
|
|
8
|
-
let supported: boolean;
|
|
9
|
-
|
|
10
|
-
try {
|
|
11
|
-
// Not supported if in testing.
|
|
12
|
-
supported = process.env["NODE_ENV"] !== "test";
|
|
13
|
-
} catch (_e) {
|
|
14
|
-
// Assume supported.
|
|
15
|
-
supported = true;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
if (supported) {
|
|
19
|
-
try {
|
|
20
|
-
Iterator.from([]);
|
|
21
|
-
} catch (_e) {
|
|
22
|
-
supported = false;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
return supported;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
1
|
/**
|
|
30
2
|
* Iteration source; shortcut for iterator or iterable.
|
|
31
|
-
*/
|
|
32
|
-
export type IterationSource<T> = Iterator<T> | Iterable<T>;
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Convert an iteration source to an iterable.
|
|
36
|
-
*
|
|
37
|
-
* @param iterationSource
|
|
38
|
-
* Iteration source.
|
|
39
3
|
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
4
|
+
* Client applications should **not** rely on long-term availability of this variable as it will be removed once there
|
|
5
|
+
* is widespread support for iterator helpers.
|
|
42
6
|
*/
|
|
43
|
-
|
|
44
|
-
return Symbol.iterator in iterationSource ?
|
|
45
|
-
iterationSource :
|
|
46
|
-
{
|
|
47
|
-
[Symbol.iterator](): Iterator<T> {
|
|
48
|
-
return iterationSource;
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
|
-
}
|
|
7
|
+
type IterationSource<T> = Iterator<T> | Iterable<T>;
|
|
52
8
|
|
|
53
9
|
/**
|
|
54
10
|
* Iterator proxy base; provides common functionality for all iterator objects.
|
|
55
11
|
*/
|
|
56
12
|
abstract class IteratorProxyBase<TInitial, TFinal> implements IteratorObject<TFinal, undefined> {
|
|
13
|
+
/**
|
|
14
|
+
* Convert an iteration source to an iterable.
|
|
15
|
+
*
|
|
16
|
+
* @param iterationSource
|
|
17
|
+
* Iteration source.
|
|
18
|
+
*
|
|
19
|
+
* @returns
|
|
20
|
+
* Iteration source if it is already an iterable, otherwise iteration source wrapped in an iterable.
|
|
21
|
+
*/
|
|
22
|
+
protected static toIterable<T>(iterationSource: IterationSource<T>): Iterable<T> {
|
|
23
|
+
return Symbol.iterator in iterationSource ?
|
|
24
|
+
iterationSource :
|
|
25
|
+
{
|
|
26
|
+
[Symbol.iterator](): Iterator<T> {
|
|
27
|
+
return iterationSource;
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
57
32
|
/**
|
|
58
33
|
* Initial iterable.
|
|
59
34
|
*/
|
|
@@ -71,7 +46,7 @@ abstract class IteratorProxyBase<TInitial, TFinal> implements IteratorObject<TFi
|
|
|
71
46
|
* Initial iteration source.
|
|
72
47
|
*/
|
|
73
48
|
constructor(initialIterationSource: IterationSource<TInitial>) {
|
|
74
|
-
this._initialIterable =
|
|
49
|
+
this._initialIterable = IteratorProxyBase.toIterable(initialIterationSource);
|
|
75
50
|
}
|
|
76
51
|
|
|
77
52
|
/**
|
|
@@ -133,8 +108,8 @@ abstract class IteratorProxyBase<TInitial, TFinal> implements IteratorObject<TFi
|
|
|
133
108
|
/**
|
|
134
109
|
* @inheritDoc
|
|
135
110
|
*/
|
|
136
|
-
map<U>(
|
|
137
|
-
return new IteratorMapProxy(this,
|
|
111
|
+
map<U>(callback: (value: TFinal, index: number) => U): IteratorObject<U, undefined> {
|
|
112
|
+
return new IteratorMapProxy(this, callback);
|
|
138
113
|
}
|
|
139
114
|
|
|
140
115
|
/**
|
|
@@ -148,7 +123,7 @@ abstract class IteratorProxyBase<TInitial, TFinal> implements IteratorObject<TFi
|
|
|
148
123
|
* @inheritDoc
|
|
149
124
|
*/
|
|
150
125
|
filter(predicate: (value: TFinal, index: number) => unknown): IteratorObject<TFinal, undefined> {
|
|
151
|
-
return new IteratorFilterProxy(this, predicate);
|
|
126
|
+
return new IteratorFilterProxy(this, predicate, true);
|
|
152
127
|
}
|
|
153
128
|
|
|
154
129
|
/**
|
|
@@ -168,23 +143,24 @@ abstract class IteratorProxyBase<TInitial, TFinal> implements IteratorObject<TFi
|
|
|
168
143
|
/**
|
|
169
144
|
* @inheritDoc
|
|
170
145
|
*/
|
|
171
|
-
reduce<U>(
|
|
146
|
+
reduce<U>(callback: (previousValue: U, currentValue: TFinal, currentIndex: number) => U, initialValue?: U): U {
|
|
172
147
|
let index = 0;
|
|
173
148
|
let result = initialValue;
|
|
174
149
|
|
|
175
150
|
for (const value of this) {
|
|
176
|
-
|
|
177
|
-
|
|
151
|
+
// Need to check arguments length as U could include undefined.
|
|
152
|
+
if (index === 0 && arguments.length === 1) {
|
|
153
|
+
// Initial value is not supplied only when U is identical to TFinal.
|
|
178
154
|
result = value as unknown as U;
|
|
179
155
|
} else {
|
|
180
156
|
// Iteration has occurred at least once so result is of the expected type.
|
|
181
|
-
result =
|
|
157
|
+
result = callback(result as U, value, index);
|
|
182
158
|
}
|
|
183
159
|
|
|
184
160
|
index++;
|
|
185
161
|
}
|
|
186
162
|
|
|
187
|
-
if (index === 0 &&
|
|
163
|
+
if (index === 0 && arguments.length === 1) {
|
|
188
164
|
throw new Error("reduce() of empty iterator with no initial value");
|
|
189
165
|
}
|
|
190
166
|
|
|
@@ -202,69 +178,36 @@ abstract class IteratorProxyBase<TInitial, TFinal> implements IteratorObject<TFi
|
|
|
202
178
|
/**
|
|
203
179
|
* @inheritDoc
|
|
204
180
|
*/
|
|
205
|
-
forEach(
|
|
181
|
+
forEach(callback: (value: TFinal, index: number) => void): void {
|
|
206
182
|
let index = 0;
|
|
207
183
|
|
|
208
184
|
for (const element of this) {
|
|
209
|
-
|
|
185
|
+
callback(element, index++);
|
|
210
186
|
}
|
|
211
187
|
}
|
|
212
188
|
|
|
213
|
-
/**
|
|
214
|
-
* Iterate until the truthy result of the predicate changes or until the iterator is exhausted.
|
|
215
|
-
*
|
|
216
|
-
* @param predicate
|
|
217
|
-
* Predicate.
|
|
218
|
-
*
|
|
219
|
-
* @param initialTruthy
|
|
220
|
-
* Initial truthy result of the predicate.
|
|
221
|
-
*
|
|
222
|
-
* @returns
|
|
223
|
-
* Iterator result.
|
|
224
|
-
*/
|
|
225
|
-
private untilChanged(predicate: (value: TFinal, index: number) => unknown, initialTruthy: boolean): IteratorResult<TFinal, undefined> {
|
|
226
|
-
let result: IteratorResult<TFinal, undefined> | undefined;
|
|
227
|
-
|
|
228
|
-
const iterator = this[Symbol.iterator]();
|
|
229
|
-
let index = 0;
|
|
230
|
-
|
|
231
|
-
do {
|
|
232
|
-
result = iterator.next();
|
|
233
|
-
|
|
234
|
-
if (result.done !== true) {
|
|
235
|
-
const truthy = Boolean(predicate(result.value, index++));
|
|
236
|
-
|
|
237
|
-
if (truthy === initialTruthy) {
|
|
238
|
-
result = undefined;
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
} while (result === undefined);
|
|
242
|
-
|
|
243
|
-
return result;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
189
|
/**
|
|
247
190
|
* @inheritDoc
|
|
248
191
|
*/
|
|
249
192
|
some(predicate: (value: TFinal, index: number) => unknown): boolean {
|
|
250
|
-
//
|
|
251
|
-
return this
|
|
193
|
+
// Filter until predicate returns truthy; return true if found.
|
|
194
|
+
return new IteratorFilterProxy(this, predicate, true).next().done !== true;
|
|
252
195
|
}
|
|
253
196
|
|
|
254
197
|
/**
|
|
255
198
|
* @inheritDoc
|
|
256
199
|
*/
|
|
257
200
|
every(predicate: (value: TFinal, index: number) => unknown): boolean {
|
|
258
|
-
//
|
|
259
|
-
return this
|
|
201
|
+
// Filter until predicate returns falsy; return false if found.
|
|
202
|
+
return new IteratorFilterProxy(this, predicate, false).next().done === true;
|
|
260
203
|
}
|
|
261
204
|
|
|
262
205
|
/**
|
|
263
206
|
* @inheritDoc
|
|
264
207
|
*/
|
|
265
208
|
find(predicate: (value: TFinal, index: number) => unknown): TFinal | undefined {
|
|
266
|
-
//
|
|
267
|
-
return this
|
|
209
|
+
// Filter until predicate returns truthy; return value.
|
|
210
|
+
return new IteratorFilterProxy(this, predicate, true).next().value;
|
|
268
211
|
}
|
|
269
212
|
}
|
|
270
213
|
|
|
@@ -276,14 +219,15 @@ class IteratorProxyObject<T> extends IteratorProxyBase<T, T> {
|
|
|
276
219
|
* @inheritDoc
|
|
277
220
|
*/
|
|
278
221
|
next(...value: [] | [unknown]): IteratorResult<T, undefined> {
|
|
222
|
+
// Initial result is the final result.
|
|
279
223
|
return this.initialNext(...value);
|
|
280
224
|
}
|
|
281
225
|
}
|
|
282
226
|
|
|
283
227
|
/**
|
|
284
|
-
* Iterator
|
|
228
|
+
* Iterator map proxy base.
|
|
285
229
|
*/
|
|
286
|
-
abstract class
|
|
230
|
+
abstract class IteratorMapProxyBase<TInitial, TIntermediate, TFinal> extends IteratorProxyBase<TInitial, TFinal> {
|
|
287
231
|
/**
|
|
288
232
|
* Callback.
|
|
289
233
|
*/
|
|
@@ -313,48 +257,44 @@ abstract class IteratorCallbackProxyBase<TInitial, TIntermediate, TFinal> extend
|
|
|
313
257
|
/**
|
|
314
258
|
* Get the next result from the intermediate iterator.
|
|
315
259
|
*
|
|
316
|
-
* @param
|
|
317
|
-
*
|
|
260
|
+
* @param value
|
|
261
|
+
* Tuple value to be passed to Iterator.next().
|
|
318
262
|
*
|
|
319
263
|
* @returns
|
|
320
264
|
* Next result from the intermediate iterator.
|
|
321
265
|
*/
|
|
322
|
-
protected intermediateNext(
|
|
323
|
-
|
|
266
|
+
protected intermediateNext(...value: [] | [unknown]): IteratorResult<TIntermediate, undefined> {
|
|
267
|
+
const initialResult = this.initialNext(...value);
|
|
324
268
|
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
done: false,
|
|
269
|
+
return initialResult.done !== true ?
|
|
270
|
+
{
|
|
328
271
|
value: this._callback(initialResult.value, this._index++)
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
intermediateResult = {
|
|
272
|
+
} :
|
|
273
|
+
{
|
|
332
274
|
done: true,
|
|
333
275
|
value: undefined
|
|
334
276
|
};
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
return intermediateResult;
|
|
338
277
|
}
|
|
339
278
|
}
|
|
340
279
|
|
|
341
280
|
/**
|
|
342
281
|
* Iterator map proxy.
|
|
343
282
|
*/
|
|
344
|
-
class IteratorMapProxy<TInitial, TFinal> extends
|
|
283
|
+
class IteratorMapProxy<TInitial, TFinal> extends IteratorMapProxyBase<TInitial, TFinal, TFinal> {
|
|
345
284
|
/**
|
|
346
285
|
* @inheritDoc
|
|
347
286
|
*/
|
|
348
287
|
next(...value: [] | [unknown]): IteratorResult<TFinal, undefined> {
|
|
349
|
-
|
|
288
|
+
// Intermediate result is the final result.
|
|
289
|
+
return this.intermediateNext(...value);
|
|
350
290
|
}
|
|
351
291
|
}
|
|
352
292
|
|
|
353
293
|
/**
|
|
354
294
|
* Iterator flat map proxy.
|
|
355
295
|
*/
|
|
356
|
-
class IteratorFlatMapProxy<TInitial, TFinal> extends
|
|
357
|
-
private
|
|
296
|
+
class IteratorFlatMapProxy<TInitial, TFinal> extends IteratorMapProxyBase<TInitial, IterationSource<TFinal>, TFinal> {
|
|
297
|
+
private _intermediateIterator: Iterator<TFinal, undefined> | undefined;
|
|
358
298
|
|
|
359
299
|
/**
|
|
360
300
|
* @inheritDoc
|
|
@@ -363,26 +303,21 @@ class IteratorFlatMapProxy<TInitial, TFinal> extends IteratorCallbackProxyBase<T
|
|
|
363
303
|
let finalResult: IteratorResult<TFinal, undefined> | undefined = undefined;
|
|
364
304
|
|
|
365
305
|
do {
|
|
366
|
-
if (this.
|
|
367
|
-
const intermediateResult = this.intermediateNext(
|
|
306
|
+
if (this._intermediateIterator === undefined) {
|
|
307
|
+
const intermediateResult = this.intermediateNext(...value);
|
|
368
308
|
|
|
369
309
|
if (intermediateResult.done === true) {
|
|
370
310
|
finalResult = intermediateResult;
|
|
371
311
|
} else {
|
|
372
|
-
this.
|
|
312
|
+
this._intermediateIterator = IteratorProxyBase.toIterable(intermediateResult.value)[Symbol.iterator]();
|
|
373
313
|
}
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
if (this._pendingFinalIterator !== undefined) {
|
|
377
|
-
const pendingFinalResult = this._pendingFinalIterator.next();
|
|
314
|
+
} else {
|
|
315
|
+
const pendingFinalResult = this._intermediateIterator.next();
|
|
378
316
|
|
|
379
317
|
if (pendingFinalResult.done === true) {
|
|
380
|
-
this.
|
|
318
|
+
this._intermediateIterator = undefined;
|
|
381
319
|
} else {
|
|
382
|
-
finalResult =
|
|
383
|
-
done: false,
|
|
384
|
-
value: pendingFinalResult.value
|
|
385
|
-
};
|
|
320
|
+
finalResult = pendingFinalResult;
|
|
386
321
|
}
|
|
387
322
|
}
|
|
388
323
|
} while (finalResult === undefined);
|
|
@@ -394,35 +329,56 @@ class IteratorFlatMapProxy<TInitial, TFinal> extends IteratorCallbackProxyBase<T
|
|
|
394
329
|
/**
|
|
395
330
|
* Iterator filter proxy.
|
|
396
331
|
*/
|
|
397
|
-
class IteratorFilterProxy<T> extends
|
|
332
|
+
class IteratorFilterProxy<T> extends IteratorProxyBase<T, T> {
|
|
333
|
+
/**
|
|
334
|
+
* Predicate.
|
|
335
|
+
*/
|
|
336
|
+
private readonly _predicate: (value: T, index: number) => unknown;
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Expected truthy result of the predicate.
|
|
340
|
+
*/
|
|
341
|
+
private readonly _expectedTruthy: boolean;
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Index into iteration source.
|
|
345
|
+
*/
|
|
346
|
+
private _index: number;
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Constructor.
|
|
350
|
+
*
|
|
351
|
+
* @param iterationSource
|
|
352
|
+
* Iteration source.
|
|
353
|
+
*
|
|
354
|
+
* @param predicate
|
|
355
|
+
* Predicate.
|
|
356
|
+
*
|
|
357
|
+
* @param expectedTruthy
|
|
358
|
+
* Expected truthy result of the predicate.
|
|
359
|
+
*/
|
|
360
|
+
constructor(iterationSource: IterationSource<T>, predicate: (element: T, index: number) => unknown, expectedTruthy: boolean) {
|
|
361
|
+
super(iterationSource);
|
|
362
|
+
|
|
363
|
+
this._predicate = predicate;
|
|
364
|
+
this._expectedTruthy = expectedTruthy;
|
|
365
|
+
|
|
366
|
+
this._index = 0;
|
|
367
|
+
}
|
|
368
|
+
|
|
398
369
|
/**
|
|
399
370
|
* @inheritDoc
|
|
400
371
|
*/
|
|
401
372
|
next(...value: [] | [unknown]): IteratorResult<T, undefined> {
|
|
402
|
-
let
|
|
373
|
+
let result: IteratorResult<T, undefined> | undefined;
|
|
403
374
|
|
|
404
|
-
|
|
405
|
-
const initialResult = this.initialNext(...value);
|
|
375
|
+
const expectedTruthy = this._expectedTruthy;
|
|
406
376
|
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
value: undefined
|
|
411
|
-
};
|
|
412
|
-
} else {
|
|
413
|
-
const intermediateResult = this.intermediateNext(initialResult);
|
|
414
|
-
const booleanValue = Boolean(intermediateResult.value);
|
|
415
|
-
|
|
416
|
-
if (booleanValue) {
|
|
417
|
-
finalResult = {
|
|
418
|
-
done: false,
|
|
419
|
-
value: initialResult.value
|
|
420
|
-
};
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
} while (finalResult === undefined);
|
|
377
|
+
do {
|
|
378
|
+
result = this.initialNext(...value);
|
|
379
|
+
} while (result.done !== true && Boolean(this._predicate(result.value, this._index++)) !== expectedTruthy);
|
|
424
380
|
|
|
425
|
-
return
|
|
381
|
+
return result;
|
|
426
382
|
}
|
|
427
383
|
}
|
|
428
384
|
|
|
@@ -448,18 +404,33 @@ abstract class IteratorCountProxyBase<T> extends IteratorProxyObject<T> {
|
|
|
448
404
|
super(initialIterationSource);
|
|
449
405
|
|
|
450
406
|
if (!Number.isInteger(count) || count < 0) {
|
|
451
|
-
throw new RangeError("
|
|
407
|
+
throw new RangeError("Count must be a positive integer");
|
|
452
408
|
}
|
|
453
409
|
|
|
454
410
|
this._count = count;
|
|
455
411
|
}
|
|
456
412
|
|
|
457
413
|
/**
|
|
458
|
-
* Determine if
|
|
414
|
+
* Determine if iterator is exhausted (by count or by iterator itself).
|
|
459
415
|
*/
|
|
460
|
-
protected get
|
|
461
|
-
|
|
462
|
-
|
|
416
|
+
protected get exhausted(): boolean {
|
|
417
|
+
return this._count <= 0;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* @inheritDoc
|
|
422
|
+
*/
|
|
423
|
+
override next(...value: [] | [unknown]): IteratorResult<T, undefined> {
|
|
424
|
+
const result = super.next(...value);
|
|
425
|
+
|
|
426
|
+
if (result.done !== true) {
|
|
427
|
+
this._count--;
|
|
428
|
+
} else {
|
|
429
|
+
// Iterator exhausted before count.
|
|
430
|
+
this._count = 0;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
return result;
|
|
463
434
|
}
|
|
464
435
|
}
|
|
465
436
|
|
|
@@ -471,12 +442,12 @@ class IteratorTakeProxy<T> extends IteratorCountProxyBase<T> {
|
|
|
471
442
|
* @inheritDoc
|
|
472
443
|
*/
|
|
473
444
|
override next(...value: [] | [unknown]): IteratorResult<T, undefined> {
|
|
474
|
-
return this.
|
|
445
|
+
return !this.exhausted ?
|
|
446
|
+
super.next(...value) :
|
|
475
447
|
{
|
|
476
448
|
done: true,
|
|
477
449
|
value: undefined
|
|
478
|
-
}
|
|
479
|
-
super.next(...value);
|
|
450
|
+
};
|
|
480
451
|
}
|
|
481
452
|
}
|
|
482
453
|
|
|
@@ -488,42 +459,59 @@ class IteratorDropProxy<T> extends IteratorCountProxyBase<T> {
|
|
|
488
459
|
* @inheritDoc
|
|
489
460
|
*/
|
|
490
461
|
override next(...value: [] | [unknown]): IteratorResult<T, undefined> {
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
result = super.next(...value);
|
|
495
|
-
|
|
496
|
-
if (result.done !== true && !this.countExhausted) {
|
|
497
|
-
// Discard result.
|
|
498
|
-
result = undefined;
|
|
499
|
-
}
|
|
500
|
-
} while (result === undefined);
|
|
462
|
+
while (!this.exhausted) {
|
|
463
|
+
super.next(...value);
|
|
464
|
+
}
|
|
501
465
|
|
|
502
|
-
return
|
|
466
|
+
return super.next(...value);
|
|
503
467
|
}
|
|
504
468
|
}
|
|
505
469
|
|
|
506
470
|
/**
|
|
507
|
-
* Iterator
|
|
471
|
+
* Get Iterator variable if supported or a proxy for it if not.
|
|
472
|
+
*
|
|
473
|
+
* @returns
|
|
474
|
+
* Iterator variable if supported or a proxy for it if not.
|
|
508
475
|
*/
|
|
509
|
-
|
|
476
|
+
function iteratorProxy(): Pick<typeof Iterator, "from"> {
|
|
477
|
+
let supported: boolean;
|
|
510
478
|
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
479
|
+
try {
|
|
480
|
+
// Not supported if in testing.
|
|
481
|
+
supported = process.env["NODE_ENV"] !== "test";
|
|
482
|
+
} catch (_e) {
|
|
483
|
+
// Assume supported.
|
|
484
|
+
supported = true;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
if (supported) {
|
|
488
|
+
try {
|
|
489
|
+
// This will throw a ReferenceError if Iterator variable is not supported.
|
|
490
|
+
Iterator.from([]);
|
|
491
|
+
} catch (_e) {
|
|
492
|
+
supported = false;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
return supported ?
|
|
497
|
+
Iterator :
|
|
498
|
+
{
|
|
499
|
+
/**
|
|
500
|
+
* @inheritDoc
|
|
501
|
+
*/
|
|
502
|
+
from<T>(value: Iterator<T> | Iterable<T>): IteratorObject<T, undefined> {
|
|
503
|
+
return value instanceof IteratorProxyBase ? value : new IteratorProxyObject(value);
|
|
504
|
+
}
|
|
505
|
+
};
|
|
506
|
+
}
|
|
515
507
|
|
|
516
508
|
/**
|
|
517
|
-
* Iterator proxy. In environments where
|
|
518
|
-
*
|
|
509
|
+
* Iterator proxy. In environments where
|
|
510
|
+
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator#iterator_helpers |
|
|
511
|
+
* iterator helpers} are supported, this references the {@link Iterator} variable directly. Otherwise, it references an
|
|
512
|
+
* implementation of "from" that uses an internally-defined iterator proxy object.
|
|
513
|
+
*
|
|
514
|
+
* Client applications should **not** rely on long-term availability of this variable as it will be removed once there
|
|
515
|
+
* is widespread support for iterator helpers.
|
|
519
516
|
*/
|
|
520
|
-
export const IteratorProxy
|
|
521
|
-
Iterator :
|
|
522
|
-
{
|
|
523
|
-
/**
|
|
524
|
-
* @inheritDoc
|
|
525
|
-
*/
|
|
526
|
-
from<T>(value: IterationSource<T>): IteratorObject<T, undefined> {
|
|
527
|
-
return value instanceof IteratorProxyBase ? value : new IteratorProxyObject(value);
|
|
528
|
-
}
|
|
529
|
-
};
|
|
517
|
+
export const IteratorProxy = iteratorProxy();
|
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
Sequencer
|
|
12
12
|
} from "../src/index.js";
|
|
13
13
|
|
|
14
|
-
await i18nInit(I18NEnvironment.CLI
|
|
14
|
+
await i18nInit(I18NEnvironment.CLI);
|
|
15
15
|
|
|
16
16
|
function testCharacterSetCreator(name: string, characterSetCreator: CharacterSetCreator, characterSetSize: number, length: number, excludeFirstZero: boolean, excludeAllNumeric: boolean): void {
|
|
17
17
|
describe(name, () => {
|
|
@@ -194,7 +194,7 @@ testCharacterSetCreator("Hexadecimal", HEXADECIMAL_CREATOR, 16, 4, true, true);
|
|
|
194
194
|
testCharacterSetCreator("Alphabetic", ALPHABETIC_CREATOR, 26, 3, false, false);
|
|
195
195
|
testCharacterSetCreator("Alphanumeric", ALPHANUMERIC_CREATOR, 36, 3, true, true);
|
|
196
196
|
testCharacterSetCreator("Middle numeric", new CharacterSetCreator([
|
|
197
|
-
"(", ")",
|
|
197
|
+
"(", ")",
|
|
198
198
|
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
|
|
199
|
-
"
|
|
200
|
-
], Exclusion.AllNumeric),
|
|
199
|
+
"<", ">"
|
|
200
|
+
], Exclusion.AllNumeric), 14, 4, false, true);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { describe, expect, test } from "vitest";
|
|
2
|
-
import {
|
|
2
|
+
import { IteratorProxy } from "../src/index.js";
|
|
3
3
|
|
|
4
4
|
const source: readonly string[] = [
|
|
5
5
|
"1", "2", "3", "4", "5"
|
|
@@ -28,7 +28,7 @@ function callbackSource(): string[] {
|
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
describe("Basic", () => {
|
|
31
|
-
function validateIterable(iterationSource:
|
|
31
|
+
function validateIterable(iterationSource: Iterator<string> | Iterable<string>): void {
|
|
32
32
|
const iteratorProxy = IteratorProxy.from(iterationSource);
|
|
33
33
|
|
|
34
34
|
expect(IteratorProxy.from(iteratorProxy)).toBe(iteratorProxy);
|
|
@@ -143,6 +143,22 @@ describe("Helpers", () => {
|
|
|
143
143
|
}
|
|
144
144
|
|
|
145
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);
|
|
146
162
|
});
|
|
147
163
|
|
|
148
164
|
test("Drop", () => {
|
|
@@ -153,6 +169,22 @@ describe("Helpers", () => {
|
|
|
153
169
|
}
|
|
154
170
|
|
|
155
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);
|
|
156
188
|
});
|
|
157
189
|
|
|
158
190
|
test("To array", () => {
|
package/test/record.test.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { I18NEnvironment, i18nInit } from "@aidc-toolkit/core";
|
|
|
2
2
|
import { describe, expect, test } from "vitest";
|
|
3
3
|
import { RecordValidator } from "../src/index.js";
|
|
4
4
|
|
|
5
|
-
await i18nInit(I18NEnvironment.CLI
|
|
5
|
+
await i18nInit(I18NEnvironment.CLI);
|
|
6
6
|
|
|
7
7
|
describe("Record validator", () => {
|
|
8
8
|
enum StringEnum {
|
package/test/reg_exp.test.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { I18NEnvironment, i18nInit } from "@aidc-toolkit/core";
|
|
|
2
2
|
import { describe, expect, test } from "vitest";
|
|
3
3
|
import { RegExpValidator } from "../src/index.js";
|
|
4
4
|
|
|
5
|
-
await i18nInit(I18NEnvironment.CLI
|
|
5
|
+
await i18nInit(I18NEnvironment.CLI);
|
|
6
6
|
|
|
7
7
|
describe("Regular expression validator", () => {
|
|
8
8
|
test("Validation", () => {
|