iterflow 0.5.0 → 0.8.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/README.md +38 -20
- package/dist/fn/index.cjs +48 -22
- package/dist/fn/index.cjs.map +1 -1
- package/dist/fn/index.js +48 -22
- package/dist/fn/index.js.map +1 -1
- package/dist/index.cjs +311 -120
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +106 -140
- package/dist/index.d.ts +106 -140
- package/dist/index.js +310 -114
- package/dist/index.js.map +1 -1
- package/package.json +6 -2
package/dist/index.js
CHANGED
|
@@ -97,6 +97,30 @@ var TypeConversionError = class extends iterflowError {
|
|
|
97
97
|
this.expectedType = expectedType;
|
|
98
98
|
}
|
|
99
99
|
};
|
|
100
|
+
var TimeoutError = class extends iterflowError {
|
|
101
|
+
timeoutMs;
|
|
102
|
+
constructor(timeoutMs, operation) {
|
|
103
|
+
super(
|
|
104
|
+
`Operation timed out after ${timeoutMs}ms`,
|
|
105
|
+
operation,
|
|
106
|
+
{ timeoutMs }
|
|
107
|
+
);
|
|
108
|
+
this.name = "TimeoutError";
|
|
109
|
+
this.timeoutMs = timeoutMs;
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
var AbortError = class extends iterflowError {
|
|
113
|
+
reason;
|
|
114
|
+
constructor(operation, reason) {
|
|
115
|
+
super(
|
|
116
|
+
reason ? `Operation aborted: ${reason}` : "Operation aborted",
|
|
117
|
+
operation,
|
|
118
|
+
{ reason }
|
|
119
|
+
);
|
|
120
|
+
this.name = "AbortError";
|
|
121
|
+
this.reason = reason;
|
|
122
|
+
}
|
|
123
|
+
};
|
|
100
124
|
|
|
101
125
|
// src/validation.ts
|
|
102
126
|
function validatePositiveInteger(value, paramName, operation) {
|
|
@@ -211,6 +235,7 @@ function validateIndex(index, size, operation) {
|
|
|
211
235
|
// src/iter-flow.ts
|
|
212
236
|
var iterflow = class _iterflow {
|
|
213
237
|
source;
|
|
238
|
+
_sourceArray;
|
|
214
239
|
/**
|
|
215
240
|
* Creates a new iterflow instance from an iterable or iterator.
|
|
216
241
|
*
|
|
@@ -222,7 +247,13 @@ var iterflow = class _iterflow {
|
|
|
222
247
|
* ```
|
|
223
248
|
*/
|
|
224
249
|
constructor(source) {
|
|
225
|
-
|
|
250
|
+
if (Array.isArray(source)) {
|
|
251
|
+
this._sourceArray = source;
|
|
252
|
+
this.source = source[Symbol.iterator]();
|
|
253
|
+
} else {
|
|
254
|
+
this._sourceArray = void 0;
|
|
255
|
+
this.source = Symbol.iterator in source ? source[Symbol.iterator]() : source;
|
|
256
|
+
}
|
|
226
257
|
}
|
|
227
258
|
// Iterator protocol
|
|
228
259
|
/**
|
|
@@ -298,6 +329,9 @@ var iterflow = class _iterflow {
|
|
|
298
329
|
* ```
|
|
299
330
|
*/
|
|
300
331
|
take(limit) {
|
|
332
|
+
if (this._sourceArray) {
|
|
333
|
+
return new _iterflow(this._sourceArray.slice(0, limit));
|
|
334
|
+
}
|
|
301
335
|
const self = this;
|
|
302
336
|
return new _iterflow({
|
|
303
337
|
*[Symbol.iterator]() {
|
|
@@ -310,6 +344,56 @@ var iterflow = class _iterflow {
|
|
|
310
344
|
}
|
|
311
345
|
});
|
|
312
346
|
}
|
|
347
|
+
/**
|
|
348
|
+
* Limits the maximum number of iterations to prevent infinite loops.
|
|
349
|
+
* Unlike `take()` which silently stops at the limit, this method throws
|
|
350
|
+
* an OperationError if the limit is exceeded, making infinite loops explicit.
|
|
351
|
+
*
|
|
352
|
+
* @param maxIterations - Maximum number of iterations allowed (must be at least 1)
|
|
353
|
+
* @returns A new iterflow that will throw if limit is exceeded
|
|
354
|
+
* @throws {ValidationError} If maxIterations is not a positive integer
|
|
355
|
+
* @throws {OperationError} If iteration count exceeds maxIterations
|
|
356
|
+
* @example
|
|
357
|
+
* ```typescript
|
|
358
|
+
* // Safely process potentially infinite iterator
|
|
359
|
+
* iter.range(Infinity).limit(1000).toArray(); // Throws after 1000 iterations
|
|
360
|
+
*
|
|
361
|
+
* // Regular finite iterator works normally
|
|
362
|
+
* iter([1, 2, 3]).limit(10).toArray(); // [1, 2, 3]
|
|
363
|
+
* ```
|
|
364
|
+
*/
|
|
365
|
+
limit(maxIterations) {
|
|
366
|
+
validatePositiveInteger(maxIterations, "maxIterations", "limit");
|
|
367
|
+
if (this._sourceArray) {
|
|
368
|
+
if (this._sourceArray.length > maxIterations) {
|
|
369
|
+
throw new OperationError(
|
|
370
|
+
`Iterator exceeded limit of ${maxIterations} iterations`,
|
|
371
|
+
"limit",
|
|
372
|
+
void 0,
|
|
373
|
+
{ maxIterations, actualCount: this._sourceArray.length }
|
|
374
|
+
);
|
|
375
|
+
}
|
|
376
|
+
return new _iterflow(this._sourceArray);
|
|
377
|
+
}
|
|
378
|
+
const self = this;
|
|
379
|
+
return new _iterflow({
|
|
380
|
+
*[Symbol.iterator]() {
|
|
381
|
+
let count = 0;
|
|
382
|
+
for (const value of self) {
|
|
383
|
+
if (count >= maxIterations) {
|
|
384
|
+
throw new OperationError(
|
|
385
|
+
`Iterator exceeded limit of ${maxIterations} iterations`,
|
|
386
|
+
"limit",
|
|
387
|
+
void 0,
|
|
388
|
+
{ maxIterations }
|
|
389
|
+
);
|
|
390
|
+
}
|
|
391
|
+
yield value;
|
|
392
|
+
count++;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
});
|
|
396
|
+
}
|
|
313
397
|
/**
|
|
314
398
|
* Skips the first `count` elements from the iterator.
|
|
315
399
|
*
|
|
@@ -321,6 +405,9 @@ var iterflow = class _iterflow {
|
|
|
321
405
|
* ```
|
|
322
406
|
*/
|
|
323
407
|
drop(count) {
|
|
408
|
+
if (this._sourceArray) {
|
|
409
|
+
return new _iterflow(this._sourceArray.slice(count));
|
|
410
|
+
}
|
|
324
411
|
const self = this;
|
|
325
412
|
return new _iterflow({
|
|
326
413
|
*[Symbol.iterator]() {
|
|
@@ -473,6 +560,9 @@ var iterflow = class _iterflow {
|
|
|
473
560
|
* ```
|
|
474
561
|
*/
|
|
475
562
|
reverse() {
|
|
563
|
+
if (this._sourceArray) {
|
|
564
|
+
return new _iterflow(this._sourceArray.slice().reverse());
|
|
565
|
+
}
|
|
476
566
|
const self = this;
|
|
477
567
|
return new _iterflow({
|
|
478
568
|
*[Symbol.iterator]() {
|
|
@@ -500,6 +590,16 @@ var iterflow = class _iterflow {
|
|
|
500
590
|
* ```
|
|
501
591
|
*/
|
|
502
592
|
sort() {
|
|
593
|
+
if (this._sourceArray) {
|
|
594
|
+
return new _iterflow(
|
|
595
|
+
this._sourceArray.slice().sort((a, b) => {
|
|
596
|
+
if (typeof a === "number" && typeof b === "number") {
|
|
597
|
+
return a - b;
|
|
598
|
+
}
|
|
599
|
+
return String(a).localeCompare(String(b));
|
|
600
|
+
})
|
|
601
|
+
);
|
|
602
|
+
}
|
|
503
603
|
const self = this;
|
|
504
604
|
return new _iterflow({
|
|
505
605
|
*[Symbol.iterator]() {
|
|
@@ -532,6 +632,9 @@ var iterflow = class _iterflow {
|
|
|
532
632
|
* ```
|
|
533
633
|
*/
|
|
534
634
|
sortBy(compareFn) {
|
|
635
|
+
if (this._sourceArray) {
|
|
636
|
+
return new _iterflow(this._sourceArray.slice().sort(compareFn));
|
|
637
|
+
}
|
|
535
638
|
const self = this;
|
|
536
639
|
return new _iterflow({
|
|
537
640
|
*[Symbol.iterator]() {
|
|
@@ -546,14 +649,43 @@ var iterflow = class _iterflow {
|
|
|
546
649
|
* Collects all elements into an array.
|
|
547
650
|
* This is a terminal operation that consumes the iterator.
|
|
548
651
|
*
|
|
549
|
-
* @
|
|
652
|
+
* @param maxSize - Optional maximum array size. If provided and the iterator produces
|
|
653
|
+
* more elements, collection stops at maxSize (no error thrown).
|
|
654
|
+
* This is useful for safely collecting from potentially large iterators.
|
|
655
|
+
* @returns Array containing all elements (up to maxSize if specified)
|
|
656
|
+
* @throws {ValidationError} If maxSize is provided but not a positive integer
|
|
550
657
|
* @example
|
|
551
658
|
* ```typescript
|
|
552
659
|
* iter([1, 2, 3]).map(x => x * 2).toArray(); // [2, 4, 6]
|
|
660
|
+
*
|
|
661
|
+
* // Safely collect from large/infinite iterator
|
|
662
|
+
* iter.range(Infinity).toArray(1000); // [0, 1, 2, ..., 999]
|
|
663
|
+
*
|
|
664
|
+
* // Works with finite iterators too
|
|
665
|
+
* iter([1, 2, 3]).toArray(10); // [1, 2, 3] (stops early)
|
|
553
666
|
* ```
|
|
554
667
|
*/
|
|
555
|
-
toArray() {
|
|
556
|
-
|
|
668
|
+
toArray(maxSize) {
|
|
669
|
+
if (maxSize !== void 0) {
|
|
670
|
+
validatePositiveInteger(maxSize, "maxSize", "toArray");
|
|
671
|
+
}
|
|
672
|
+
if (this._sourceArray) {
|
|
673
|
+
if (maxSize !== void 0) {
|
|
674
|
+
return this._sourceArray.slice(0, maxSize);
|
|
675
|
+
}
|
|
676
|
+
return this._sourceArray.slice();
|
|
677
|
+
}
|
|
678
|
+
if (maxSize === void 0) {
|
|
679
|
+
return Array.from(this);
|
|
680
|
+
}
|
|
681
|
+
const result = [];
|
|
682
|
+
for (const value of this) {
|
|
683
|
+
if (result.length >= maxSize) {
|
|
684
|
+
break;
|
|
685
|
+
}
|
|
686
|
+
result.push(value);
|
|
687
|
+
}
|
|
688
|
+
return result;
|
|
557
689
|
}
|
|
558
690
|
/**
|
|
559
691
|
* Counts the total number of elements in the iterator.
|
|
@@ -566,6 +698,9 @@ var iterflow = class _iterflow {
|
|
|
566
698
|
* ```
|
|
567
699
|
*/
|
|
568
700
|
count() {
|
|
701
|
+
if (this._sourceArray) {
|
|
702
|
+
return this._sourceArray.length;
|
|
703
|
+
}
|
|
569
704
|
let count = 0;
|
|
570
705
|
for (const _ of this) {
|
|
571
706
|
count++;
|
|
@@ -586,6 +721,13 @@ var iterflow = class _iterflow {
|
|
|
586
721
|
* ```
|
|
587
722
|
*/
|
|
588
723
|
sum() {
|
|
724
|
+
if (this._sourceArray) {
|
|
725
|
+
let total2 = 0;
|
|
726
|
+
for (let i = 0; i < this._sourceArray.length; i++) {
|
|
727
|
+
total2 += this._sourceArray[i];
|
|
728
|
+
}
|
|
729
|
+
return total2;
|
|
730
|
+
}
|
|
589
731
|
let total = 0;
|
|
590
732
|
for (const value of this) {
|
|
591
733
|
total += value;
|
|
@@ -674,14 +816,14 @@ var iterflow = class _iterflow {
|
|
|
674
816
|
* ```
|
|
675
817
|
*/
|
|
676
818
|
median() {
|
|
677
|
-
const values = this.toArray();
|
|
819
|
+
const values = this._sourceArray || this.toArray();
|
|
678
820
|
if (values.length === 0) return void 0;
|
|
679
|
-
values.sort((a, b) => a - b);
|
|
680
|
-
const mid = Math.floor(
|
|
681
|
-
if (
|
|
682
|
-
return (
|
|
821
|
+
const sorted = values.slice().sort((a, b) => a - b);
|
|
822
|
+
const mid = Math.floor(sorted.length / 2);
|
|
823
|
+
if (sorted.length % 2 === 0) {
|
|
824
|
+
return (sorted[mid - 1] + sorted[mid]) / 2;
|
|
683
825
|
} else {
|
|
684
|
-
return
|
|
826
|
+
return sorted[mid];
|
|
685
827
|
}
|
|
686
828
|
}
|
|
687
829
|
/**
|
|
@@ -699,7 +841,7 @@ var iterflow = class _iterflow {
|
|
|
699
841
|
* ```
|
|
700
842
|
*/
|
|
701
843
|
variance() {
|
|
702
|
-
const values = this.toArray();
|
|
844
|
+
const values = this._sourceArray || this.toArray();
|
|
703
845
|
if (values.length === 0) return void 0;
|
|
704
846
|
const mean = values.reduce((sum, val) => sum + val, 0) / values.length;
|
|
705
847
|
let sumSquaredDiffs = 0;
|
|
@@ -747,19 +889,19 @@ var iterflow = class _iterflow {
|
|
|
747
889
|
*/
|
|
748
890
|
percentile(p) {
|
|
749
891
|
validateRange(p, 0, 100, "percentile", "percentile");
|
|
750
|
-
const values = this.toArray();
|
|
892
|
+
const values = this._sourceArray || this.toArray();
|
|
751
893
|
if (values.length === 0) return void 0;
|
|
752
|
-
values.sort((a, b) => a - b);
|
|
753
|
-
if (p === 0) return
|
|
754
|
-
if (p === 100) return
|
|
755
|
-
const index = p / 100 * (
|
|
894
|
+
const sorted = values.slice().sort((a, b) => a - b);
|
|
895
|
+
if (p === 0) return sorted[0];
|
|
896
|
+
if (p === 100) return sorted[sorted.length - 1];
|
|
897
|
+
const index = p / 100 * (sorted.length - 1);
|
|
756
898
|
const lower = Math.floor(index);
|
|
757
899
|
const upper = Math.ceil(index);
|
|
758
900
|
if (lower === upper) {
|
|
759
|
-
return
|
|
901
|
+
return sorted[lower];
|
|
760
902
|
}
|
|
761
903
|
const weight = index - lower;
|
|
762
|
-
return
|
|
904
|
+
return sorted[lower] * (1 - weight) + sorted[upper] * weight;
|
|
763
905
|
}
|
|
764
906
|
/**
|
|
765
907
|
* Finds the most frequent value(s) in the dataset.
|
|
@@ -777,7 +919,7 @@ var iterflow = class _iterflow {
|
|
|
777
919
|
* ```
|
|
778
920
|
*/
|
|
779
921
|
mode() {
|
|
780
|
-
const values = this.toArray();
|
|
922
|
+
const values = this._sourceArray || this.toArray();
|
|
781
923
|
if (values.length === 0) return void 0;
|
|
782
924
|
const frequency = /* @__PURE__ */ new Map();
|
|
783
925
|
let maxFreq = 0;
|
|
@@ -810,20 +952,20 @@ var iterflow = class _iterflow {
|
|
|
810
952
|
* ```
|
|
811
953
|
*/
|
|
812
954
|
quartiles() {
|
|
813
|
-
const values = this.toArray();
|
|
955
|
+
const values = this._sourceArray || this.toArray();
|
|
814
956
|
if (values.length === 0) return void 0;
|
|
815
|
-
values.sort((a, b) => a - b);
|
|
957
|
+
const sorted = values.slice().sort((a, b) => a - b);
|
|
816
958
|
const calculatePercentile = (p) => {
|
|
817
|
-
if (p === 0) return
|
|
818
|
-
if (p === 100) return
|
|
819
|
-
const index = p / 100 * (
|
|
959
|
+
if (p === 0) return sorted[0];
|
|
960
|
+
if (p === 100) return sorted[sorted.length - 1];
|
|
961
|
+
const index = p / 100 * (sorted.length - 1);
|
|
820
962
|
const lower = Math.floor(index);
|
|
821
963
|
const upper = Math.ceil(index);
|
|
822
964
|
if (lower === upper) {
|
|
823
|
-
return
|
|
965
|
+
return sorted[lower];
|
|
824
966
|
}
|
|
825
967
|
const weight = index - lower;
|
|
826
|
-
return
|
|
968
|
+
return sorted[lower] * (1 - weight) + sorted[upper] * weight;
|
|
827
969
|
};
|
|
828
970
|
return {
|
|
829
971
|
Q1: calculatePercentile(25),
|
|
@@ -895,7 +1037,7 @@ var iterflow = class _iterflow {
|
|
|
895
1037
|
* ```
|
|
896
1038
|
*/
|
|
897
1039
|
covariance(other) {
|
|
898
|
-
const values1 = this.toArray();
|
|
1040
|
+
const values1 = this._sourceArray || this.toArray();
|
|
899
1041
|
const values2 = Array.from(other);
|
|
900
1042
|
if (values1.length === 0 || values2.length === 0 || values1.length !== values2.length) {
|
|
901
1043
|
return void 0;
|
|
@@ -926,7 +1068,7 @@ var iterflow = class _iterflow {
|
|
|
926
1068
|
* ```
|
|
927
1069
|
*/
|
|
928
1070
|
correlation(other) {
|
|
929
|
-
const values1 = this.toArray();
|
|
1071
|
+
const values1 = this._sourceArray || this.toArray();
|
|
930
1072
|
const values2 = Array.from(other);
|
|
931
1073
|
if (values1.length === 0 || values2.length === 0 || values1.length !== values2.length) {
|
|
932
1074
|
return void 0;
|
|
@@ -1350,6 +1492,9 @@ var iterflow = class _iterflow {
|
|
|
1350
1492
|
* ```
|
|
1351
1493
|
*/
|
|
1352
1494
|
first(defaultValue) {
|
|
1495
|
+
if (this._sourceArray) {
|
|
1496
|
+
return this._sourceArray.length > 0 ? this._sourceArray[0] : defaultValue;
|
|
1497
|
+
}
|
|
1353
1498
|
const result = this.source.next();
|
|
1354
1499
|
return result.done ? defaultValue : result.value;
|
|
1355
1500
|
}
|
|
@@ -1367,6 +1512,9 @@ var iterflow = class _iterflow {
|
|
|
1367
1512
|
* ```
|
|
1368
1513
|
*/
|
|
1369
1514
|
last(defaultValue) {
|
|
1515
|
+
if (this._sourceArray) {
|
|
1516
|
+
return this._sourceArray.length > 0 ? this._sourceArray[this._sourceArray.length - 1] : defaultValue;
|
|
1517
|
+
}
|
|
1370
1518
|
let lastValue = defaultValue;
|
|
1371
1519
|
let hasValue = false;
|
|
1372
1520
|
for (const value of this) {
|
|
@@ -1389,6 +1537,9 @@ var iterflow = class _iterflow {
|
|
|
1389
1537
|
* ```
|
|
1390
1538
|
*/
|
|
1391
1539
|
nth(index) {
|
|
1540
|
+
if (this._sourceArray) {
|
|
1541
|
+
return index >= 0 && index < this._sourceArray.length ? this._sourceArray[index] : void 0;
|
|
1542
|
+
}
|
|
1392
1543
|
if (index < 0) {
|
|
1393
1544
|
return void 0;
|
|
1394
1545
|
}
|
|
@@ -1413,6 +1564,9 @@ var iterflow = class _iterflow {
|
|
|
1413
1564
|
* ```
|
|
1414
1565
|
*/
|
|
1415
1566
|
isEmpty() {
|
|
1567
|
+
if (this._sourceArray) {
|
|
1568
|
+
return this._sourceArray.length === 0;
|
|
1569
|
+
}
|
|
1416
1570
|
const result = this.source.next();
|
|
1417
1571
|
return result.done === true;
|
|
1418
1572
|
}
|
|
@@ -1431,6 +1585,9 @@ var iterflow = class _iterflow {
|
|
|
1431
1585
|
* ```
|
|
1432
1586
|
*/
|
|
1433
1587
|
includes(searchValue) {
|
|
1588
|
+
if (this._sourceArray) {
|
|
1589
|
+
return this._sourceArray.includes(searchValue);
|
|
1590
|
+
}
|
|
1434
1591
|
for (const value of this) {
|
|
1435
1592
|
if (value === searchValue) {
|
|
1436
1593
|
return true;
|
|
@@ -1887,15 +2044,28 @@ var Asynciterflow = class _Asynciterflow {
|
|
|
1887
2044
|
* Collects all elements into an array.
|
|
1888
2045
|
* This is a terminal operation that consumes the async iterator.
|
|
1889
2046
|
*
|
|
1890
|
-
* @
|
|
2047
|
+
* @param maxSize - Optional maximum array size. If provided and the iterator produces
|
|
2048
|
+
* more elements, collection stops at maxSize (no error thrown).
|
|
2049
|
+
* This is useful for safely collecting from potentially large iterators.
|
|
2050
|
+
* @returns Promise of array containing all elements (up to maxSize if specified)
|
|
2051
|
+
* @throws {ValidationError} If maxSize is provided but not a positive integer
|
|
1891
2052
|
* @example
|
|
1892
2053
|
* ```typescript
|
|
1893
2054
|
* await asyncIter([1, 2, 3]).map(async x => x * 2).toArray(); // [2, 4, 6]
|
|
2055
|
+
*
|
|
2056
|
+
* // Safely collect from large async iterator
|
|
2057
|
+
* await asyncIter(infiniteStream).toArray(1000); // First 1000 items
|
|
1894
2058
|
* ```
|
|
1895
2059
|
*/
|
|
1896
|
-
async toArray() {
|
|
2060
|
+
async toArray(maxSize) {
|
|
2061
|
+
if (maxSize !== void 0) {
|
|
2062
|
+
validatePositiveInteger(maxSize, "maxSize", "toArray");
|
|
2063
|
+
}
|
|
1897
2064
|
const result = [];
|
|
1898
2065
|
for await (const value of this) {
|
|
2066
|
+
if (maxSize !== void 0 && result.length >= maxSize) {
|
|
2067
|
+
break;
|
|
2068
|
+
}
|
|
1899
2069
|
result.push(value);
|
|
1900
2070
|
}
|
|
1901
2071
|
return result;
|
|
@@ -3147,6 +3317,111 @@ var Asynciterflow = class _Asynciterflow {
|
|
|
3147
3317
|
}
|
|
3148
3318
|
});
|
|
3149
3319
|
}
|
|
3320
|
+
/**
|
|
3321
|
+
* Adds a timeout to async iteration operations.
|
|
3322
|
+
* If any single iteration (next() call) takes longer than the specified timeout,
|
|
3323
|
+
* a TimeoutError is thrown. Each iteration gets its own timeout window.
|
|
3324
|
+
*
|
|
3325
|
+
* @param ms - Timeout duration in milliseconds (must be positive)
|
|
3326
|
+
* @returns A new async iterflow with timeout protection
|
|
3327
|
+
* @throws {ValidationError} If ms is not a positive number
|
|
3328
|
+
* @throws {TimeoutError} If any iteration exceeds the timeout
|
|
3329
|
+
* @example
|
|
3330
|
+
* ```typescript
|
|
3331
|
+
* // Protect against slow async operations
|
|
3332
|
+
* await asyncIter([1, 2, 3])
|
|
3333
|
+
* .map(async x => {
|
|
3334
|
+
* await slowOperation(x); // Each must complete within 5000ms
|
|
3335
|
+
* return x;
|
|
3336
|
+
* })
|
|
3337
|
+
* .timeout(5000)
|
|
3338
|
+
* .toArray();
|
|
3339
|
+
* ```
|
|
3340
|
+
*/
|
|
3341
|
+
timeout(ms) {
|
|
3342
|
+
validatePositiveInteger(ms, "timeout", "timeout");
|
|
3343
|
+
const self = this;
|
|
3344
|
+
return new _Asynciterflow({
|
|
3345
|
+
async *[Symbol.asyncIterator]() {
|
|
3346
|
+
const iterator = self[Symbol.asyncIterator]();
|
|
3347
|
+
while (true) {
|
|
3348
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
3349
|
+
setTimeout(() => {
|
|
3350
|
+
reject(new TimeoutError(ms, "timeout"));
|
|
3351
|
+
}, ms);
|
|
3352
|
+
});
|
|
3353
|
+
const result = await Promise.race([iterator.next(), timeoutPromise]);
|
|
3354
|
+
if (result.done) break;
|
|
3355
|
+
yield result.value;
|
|
3356
|
+
}
|
|
3357
|
+
}
|
|
3358
|
+
});
|
|
3359
|
+
}
|
|
3360
|
+
/**
|
|
3361
|
+
* Adds AbortSignal support to the async iteration.
|
|
3362
|
+
* If the signal is aborted, iteration stops immediately with an AbortError.
|
|
3363
|
+
* Checks the signal before each iteration and listens for abort events.
|
|
3364
|
+
*
|
|
3365
|
+
* @param signal - AbortSignal to control iteration cancellation
|
|
3366
|
+
* @returns A new async iterflow with abort support
|
|
3367
|
+
* @throws {AbortError} If the signal is aborted
|
|
3368
|
+
* @example
|
|
3369
|
+
* ```typescript
|
|
3370
|
+
* const controller = new AbortController();
|
|
3371
|
+
*
|
|
3372
|
+
* // Cancel after 5 seconds
|
|
3373
|
+
* setTimeout(() => controller.abort('Timeout reached'), 5000);
|
|
3374
|
+
*
|
|
3375
|
+
* try {
|
|
3376
|
+
* await asyncIter(largeDataset)
|
|
3377
|
+
* .withSignal(controller.signal)
|
|
3378
|
+
* .map(processItem)
|
|
3379
|
+
* .toArray();
|
|
3380
|
+
* } catch (error) {
|
|
3381
|
+
* if (error instanceof AbortError) {
|
|
3382
|
+
* console.log('Operation cancelled:', error.reason);
|
|
3383
|
+
* }
|
|
3384
|
+
* }
|
|
3385
|
+
* ```
|
|
3386
|
+
*/
|
|
3387
|
+
withSignal(signal) {
|
|
3388
|
+
const self = this;
|
|
3389
|
+
return new _Asynciterflow({
|
|
3390
|
+
async *[Symbol.asyncIterator]() {
|
|
3391
|
+
if (signal.aborted) {
|
|
3392
|
+
throw new AbortError(
|
|
3393
|
+
"withSignal",
|
|
3394
|
+
typeof signal.reason === "string" ? signal.reason : void 0
|
|
3395
|
+
);
|
|
3396
|
+
}
|
|
3397
|
+
const iterator = self[Symbol.asyncIterator]();
|
|
3398
|
+
let abortReject;
|
|
3399
|
+
const abortPromise = new Promise((_, reject) => {
|
|
3400
|
+
abortReject = reject;
|
|
3401
|
+
});
|
|
3402
|
+
const abortHandler = () => {
|
|
3403
|
+
if (abortReject) {
|
|
3404
|
+
abortReject(
|
|
3405
|
+
new AbortError(
|
|
3406
|
+
"withSignal",
|
|
3407
|
+
typeof signal.reason === "string" ? signal.reason : void 0
|
|
3408
|
+
)
|
|
3409
|
+
);
|
|
3410
|
+
}
|
|
3411
|
+
};
|
|
3412
|
+
signal.addEventListener("abort", abortHandler);
|
|
3413
|
+
try {
|
|
3414
|
+
while (true) {
|
|
3415
|
+
const result = await Promise.race([iterator.next(), abortPromise]);
|
|
3416
|
+
if (result.done) break;
|
|
3417
|
+
yield result.value;
|
|
3418
|
+
}
|
|
3419
|
+
} finally {
|
|
3420
|
+
signal.removeEventListener("abort", abortHandler);
|
|
3421
|
+
}
|
|
3422
|
+
}
|
|
3423
|
+
});
|
|
3424
|
+
}
|
|
3150
3425
|
};
|
|
3151
3426
|
function asyncIter(source) {
|
|
3152
3427
|
if (Symbol.asyncIterator in source) {
|
|
@@ -3373,12 +3648,12 @@ var DebugState = class {
|
|
|
3373
3648
|
/**
|
|
3374
3649
|
* Enable debug mode with optional configuration
|
|
3375
3650
|
*/
|
|
3376
|
-
enable(
|
|
3651
|
+
enable(config) {
|
|
3377
3652
|
this.config = {
|
|
3378
3653
|
...this.config,
|
|
3379
3654
|
enabled: true,
|
|
3380
3655
|
traceOperations: true,
|
|
3381
|
-
...
|
|
3656
|
+
...config
|
|
3382
3657
|
};
|
|
3383
3658
|
if (this.config.logToConsole) {
|
|
3384
3659
|
console.log("[iterflow Debug] Debug mode enabled", this.config);
|
|
@@ -3498,8 +3773,8 @@ var DebugState = class {
|
|
|
3498
3773
|
}
|
|
3499
3774
|
};
|
|
3500
3775
|
var debugState = new DebugState();
|
|
3501
|
-
function enableDebug(
|
|
3502
|
-
debugState.enable(
|
|
3776
|
+
function enableDebug(config) {
|
|
3777
|
+
debugState.enable(config);
|
|
3503
3778
|
}
|
|
3504
3779
|
function disableDebug() {
|
|
3505
3780
|
debugState.disable();
|
|
@@ -3732,85 +4007,6 @@ function errorBoundary(fn, options = {}) {
|
|
|
3732
4007
|
};
|
|
3733
4008
|
}
|
|
3734
4009
|
|
|
3735
|
-
// src/deprecation.ts
|
|
3736
|
-
var config = {
|
|
3737
|
-
enabled: typeof process !== "undefined" && process.env.NODE_ENV !== "production",
|
|
3738
|
-
showStackTrace: false
|
|
3739
|
-
};
|
|
3740
|
-
var warnedFeatures = /* @__PURE__ */ new Set();
|
|
3741
|
-
function configureDeprecation(options) {
|
|
3742
|
-
config = { ...config, ...options };
|
|
3743
|
-
}
|
|
3744
|
-
function getDeprecationConfig() {
|
|
3745
|
-
return { ...config };
|
|
3746
|
-
}
|
|
3747
|
-
function clearDeprecationWarnings() {
|
|
3748
|
-
warnedFeatures.clear();
|
|
3749
|
-
}
|
|
3750
|
-
function emitWarning(warning) {
|
|
3751
|
-
if (!config.enabled) return;
|
|
3752
|
-
if (warnedFeatures.has(warning.feature)) return;
|
|
3753
|
-
warnedFeatures.add(warning.feature);
|
|
3754
|
-
if (config.handler) {
|
|
3755
|
-
config.handler(warning);
|
|
3756
|
-
return;
|
|
3757
|
-
}
|
|
3758
|
-
let message = `[iterflow] DEPRECATED: ${warning.feature} has been deprecated since v${warning.since}`;
|
|
3759
|
-
if (warning.removeIn) {
|
|
3760
|
-
message += ` and will be removed in v${warning.removeIn}`;
|
|
3761
|
-
}
|
|
3762
|
-
if (warning.alternative) {
|
|
3763
|
-
message += `
|
|
3764
|
-
Please use ${warning.alternative} instead.`;
|
|
3765
|
-
}
|
|
3766
|
-
if (warning.message) {
|
|
3767
|
-
message += `
|
|
3768
|
-
${warning.message}`;
|
|
3769
|
-
}
|
|
3770
|
-
if (typeof process !== "undefined" && process.emitWarning) {
|
|
3771
|
-
const options = {
|
|
3772
|
-
type: "DeprecationWarning",
|
|
3773
|
-
code: "ITERFLOW_DEPRECATION",
|
|
3774
|
-
detail: warning.message
|
|
3775
|
-
};
|
|
3776
|
-
process.emitWarning(message, options);
|
|
3777
|
-
} else {
|
|
3778
|
-
console.warn(message);
|
|
3779
|
-
}
|
|
3780
|
-
if (config.showStackTrace && warning.stack) {
|
|
3781
|
-
console.warn("Stack trace:", warning.stack);
|
|
3782
|
-
}
|
|
3783
|
-
}
|
|
3784
|
-
function deprecate(options) {
|
|
3785
|
-
const warning = {
|
|
3786
|
-
...options,
|
|
3787
|
-
stack: config.showStackTrace ? new Error().stack : void 0
|
|
3788
|
-
};
|
|
3789
|
-
emitWarning(warning);
|
|
3790
|
-
}
|
|
3791
|
-
function deprecated(options) {
|
|
3792
|
-
return function(target, propertyKey, descriptor) {
|
|
3793
|
-
const originalMethod = descriptor.value;
|
|
3794
|
-
if (!originalMethod) return;
|
|
3795
|
-
const className = target.constructor?.name || "Object";
|
|
3796
|
-
const feature = `${className}.${propertyKey}`;
|
|
3797
|
-
descriptor.value = function(...args) {
|
|
3798
|
-
deprecate({ ...options, feature });
|
|
3799
|
-
return originalMethod.apply(this, args);
|
|
3800
|
-
};
|
|
3801
|
-
return descriptor;
|
|
3802
|
-
};
|
|
3803
|
-
}
|
|
3804
|
-
function deprecatedFunction(fn, options) {
|
|
3805
|
-
return function(...args) {
|
|
3806
|
-
deprecate(options);
|
|
3807
|
-
return fn.apply(this, args);
|
|
3808
|
-
};
|
|
3809
|
-
}
|
|
3810
|
-
function hasDeprecationWarning(feature) {
|
|
3811
|
-
return warnedFeatures.has(feature);
|
|
3812
|
-
}
|
|
3813
|
-
|
|
3814
4010
|
// src/index.ts
|
|
3815
4011
|
function iter(source) {
|
|
3816
4012
|
return new iterflow(source);
|
|
@@ -3969,6 +4165,6 @@ function iter(source) {
|
|
|
3969
4165
|
iter2.chain = chain;
|
|
3970
4166
|
})(iter || (iter = {}));
|
|
3971
4167
|
|
|
3972
|
-
export { Asynciterflow, EmptySequenceError, IndexOutOfBoundsError, OperationError, TypeConversionError, ValidationError, addTrace, asyncIter,
|
|
4168
|
+
export { AbortError, Asynciterflow, EmptySequenceError, IndexOutOfBoundsError, OperationError, TimeoutError, TypeConversionError, ValidationError, addTrace, asyncIter, clearTraces, disableDebug, enableDebug, errorBoundary, getDebugConfig, getTraceSummary, getTraces, getTracesForOperation, isDebugEnabled, iter, iterflow, iterflowError, safeComparator, safePredicate, toInteger, toNumber, toResult, toResultAsync, traceOperation, traceOperationAsync, tryCatch, tryCatchAsync, tryOr, validateComparator, validateFiniteNumber, validateFunction, validateIndex, validateIterable, validateNonEmpty, validateNonNegativeInteger, validateNonZero, validatePositiveInteger, validateRange, withDefault, withErrorRecovery, withRetry, withRetryAsync };
|
|
3973
4169
|
//# sourceMappingURL=index.js.map
|
|
3974
4170
|
//# sourceMappingURL=index.js.map
|