iterflow 0.8.0 → 0.10.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 -0
- package/dist/fn/index.cjs.map +1 -1
- package/dist/fn/index.js.map +1 -1
- package/dist/index.cjs +153 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +47 -0
- package/dist/index.d.ts +47 -0
- package/dist/index.js +153 -0
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.d.cts
CHANGED
|
@@ -758,11 +758,15 @@ declare class iterflow<T> implements Iterable<T> {
|
|
|
758
758
|
/**
|
|
759
759
|
* Alias for stdDev() method for compatibility.
|
|
760
760
|
* Calculates the standard deviation of all numeric elements.
|
|
761
|
+
*
|
|
762
|
+
* @deprecated Use stdDev() instead. This alias will be removed in v1.0.0.
|
|
761
763
|
*/
|
|
762
764
|
stddev(this: iterflow<number>): number | undefined;
|
|
763
765
|
/**
|
|
764
766
|
* Alias for drop() method for compatibility.
|
|
765
767
|
* Skips the first `count` elements from the iterator.
|
|
768
|
+
*
|
|
769
|
+
* @deprecated Use drop() instead. This alias will be removed in v1.0.0.
|
|
766
770
|
*/
|
|
767
771
|
skip(count: number): iterflow<T>;
|
|
768
772
|
/**
|
|
@@ -876,6 +880,25 @@ declare class Asynciterflow<T> implements AsyncIterable<T> {
|
|
|
876
880
|
* ```
|
|
877
881
|
*/
|
|
878
882
|
drop(count: number): Asynciterflow<T>;
|
|
883
|
+
/**
|
|
884
|
+
* Limits the maximum number of iterations to prevent infinite loops.
|
|
885
|
+
* Unlike `take()` which silently stops at the limit, this method throws
|
|
886
|
+
* an OperationError if the limit is exceeded, making infinite loops explicit.
|
|
887
|
+
*
|
|
888
|
+
* @param maxIterations - Maximum number of iterations allowed (must be at least 1)
|
|
889
|
+
* @returns A new async iterflow that will throw if limit is exceeded
|
|
890
|
+
* @throws {ValidationError} If maxIterations is not a positive integer
|
|
891
|
+
* @throws {OperationError} If iteration count exceeds maxIterations
|
|
892
|
+
* @example
|
|
893
|
+
* ```typescript
|
|
894
|
+
* // Safely process potentially infinite async iterator
|
|
895
|
+
* await asyncIter.range(Infinity).limit(1000).toArray(); // Throws after 1000 iterations
|
|
896
|
+
*
|
|
897
|
+
* // Regular finite iterator works normally
|
|
898
|
+
* await asyncIter([1, 2, 3]).limit(10).toArray(); // [1, 2, 3]
|
|
899
|
+
* ```
|
|
900
|
+
*/
|
|
901
|
+
limit(maxIterations: number): Asynciterflow<T>;
|
|
879
902
|
/**
|
|
880
903
|
* Maps each element to an async iterable and flattens the results.
|
|
881
904
|
*
|
|
@@ -1631,6 +1654,30 @@ declare class Asynciterflow<T> implements AsyncIterable<T> {
|
|
|
1631
1654
|
* ```
|
|
1632
1655
|
*/
|
|
1633
1656
|
withSignal(signal: AbortSignal): Asynciterflow<T>;
|
|
1657
|
+
/**
|
|
1658
|
+
* Interleaves elements from this async iterator with elements from other async/sync iterables.
|
|
1659
|
+
* Takes one element from each iterable in round-robin fashion.
|
|
1660
|
+
*
|
|
1661
|
+
* @param others - Variable number of async/sync iterables to interleave with
|
|
1662
|
+
* @returns A new async iterflow with elements from all iterables interleaved
|
|
1663
|
+
* @example
|
|
1664
|
+
* ```typescript
|
|
1665
|
+
* await asyncIter([1, 2, 3]).interleave([4, 5, 6]).toArray(); // [1, 4, 2, 5, 3, 6]
|
|
1666
|
+
* ```
|
|
1667
|
+
*/
|
|
1668
|
+
interleave(...others: Array<AsyncIterable<T> | Iterable<T>>): Asynciterflow<T>;
|
|
1669
|
+
/**
|
|
1670
|
+
* Merges this async iterator with other sorted async/sync iterables into a single sorted async iterator.
|
|
1671
|
+
* Assumes all input iterables are already sorted in ascending order.
|
|
1672
|
+
*
|
|
1673
|
+
* @param others - Variable number of sorted async/sync iterables to merge with
|
|
1674
|
+
* @returns A new async iterflow with all elements merged in sorted order
|
|
1675
|
+
* @example
|
|
1676
|
+
* ```typescript
|
|
1677
|
+
* await asyncIter([1, 3, 5]).merge([2, 4, 6]).toArray(); // [1, 2, 3, 4, 5, 6]
|
|
1678
|
+
* ```
|
|
1679
|
+
*/
|
|
1680
|
+
merge(...others: Array<AsyncIterable<T> | Iterable<T>>): Asynciterflow<T>;
|
|
1634
1681
|
}
|
|
1635
1682
|
/**
|
|
1636
1683
|
* Creates an async iterflow instance from an async iterable or iterable.
|
package/dist/index.d.ts
CHANGED
|
@@ -758,11 +758,15 @@ declare class iterflow<T> implements Iterable<T> {
|
|
|
758
758
|
/**
|
|
759
759
|
* Alias for stdDev() method for compatibility.
|
|
760
760
|
* Calculates the standard deviation of all numeric elements.
|
|
761
|
+
*
|
|
762
|
+
* @deprecated Use stdDev() instead. This alias will be removed in v1.0.0.
|
|
761
763
|
*/
|
|
762
764
|
stddev(this: iterflow<number>): number | undefined;
|
|
763
765
|
/**
|
|
764
766
|
* Alias for drop() method for compatibility.
|
|
765
767
|
* Skips the first `count` elements from the iterator.
|
|
768
|
+
*
|
|
769
|
+
* @deprecated Use drop() instead. This alias will be removed in v1.0.0.
|
|
766
770
|
*/
|
|
767
771
|
skip(count: number): iterflow<T>;
|
|
768
772
|
/**
|
|
@@ -876,6 +880,25 @@ declare class Asynciterflow<T> implements AsyncIterable<T> {
|
|
|
876
880
|
* ```
|
|
877
881
|
*/
|
|
878
882
|
drop(count: number): Asynciterflow<T>;
|
|
883
|
+
/**
|
|
884
|
+
* Limits the maximum number of iterations to prevent infinite loops.
|
|
885
|
+
* Unlike `take()` which silently stops at the limit, this method throws
|
|
886
|
+
* an OperationError if the limit is exceeded, making infinite loops explicit.
|
|
887
|
+
*
|
|
888
|
+
* @param maxIterations - Maximum number of iterations allowed (must be at least 1)
|
|
889
|
+
* @returns A new async iterflow that will throw if limit is exceeded
|
|
890
|
+
* @throws {ValidationError} If maxIterations is not a positive integer
|
|
891
|
+
* @throws {OperationError} If iteration count exceeds maxIterations
|
|
892
|
+
* @example
|
|
893
|
+
* ```typescript
|
|
894
|
+
* // Safely process potentially infinite async iterator
|
|
895
|
+
* await asyncIter.range(Infinity).limit(1000).toArray(); // Throws after 1000 iterations
|
|
896
|
+
*
|
|
897
|
+
* // Regular finite iterator works normally
|
|
898
|
+
* await asyncIter([1, 2, 3]).limit(10).toArray(); // [1, 2, 3]
|
|
899
|
+
* ```
|
|
900
|
+
*/
|
|
901
|
+
limit(maxIterations: number): Asynciterflow<T>;
|
|
879
902
|
/**
|
|
880
903
|
* Maps each element to an async iterable and flattens the results.
|
|
881
904
|
*
|
|
@@ -1631,6 +1654,30 @@ declare class Asynciterflow<T> implements AsyncIterable<T> {
|
|
|
1631
1654
|
* ```
|
|
1632
1655
|
*/
|
|
1633
1656
|
withSignal(signal: AbortSignal): Asynciterflow<T>;
|
|
1657
|
+
/**
|
|
1658
|
+
* Interleaves elements from this async iterator with elements from other async/sync iterables.
|
|
1659
|
+
* Takes one element from each iterable in round-robin fashion.
|
|
1660
|
+
*
|
|
1661
|
+
* @param others - Variable number of async/sync iterables to interleave with
|
|
1662
|
+
* @returns A new async iterflow with elements from all iterables interleaved
|
|
1663
|
+
* @example
|
|
1664
|
+
* ```typescript
|
|
1665
|
+
* await asyncIter([1, 2, 3]).interleave([4, 5, 6]).toArray(); // [1, 4, 2, 5, 3, 6]
|
|
1666
|
+
* ```
|
|
1667
|
+
*/
|
|
1668
|
+
interleave(...others: Array<AsyncIterable<T> | Iterable<T>>): Asynciterflow<T>;
|
|
1669
|
+
/**
|
|
1670
|
+
* Merges this async iterator with other sorted async/sync iterables into a single sorted async iterator.
|
|
1671
|
+
* Assumes all input iterables are already sorted in ascending order.
|
|
1672
|
+
*
|
|
1673
|
+
* @param others - Variable number of sorted async/sync iterables to merge with
|
|
1674
|
+
* @returns A new async iterflow with all elements merged in sorted order
|
|
1675
|
+
* @example
|
|
1676
|
+
* ```typescript
|
|
1677
|
+
* await asyncIter([1, 3, 5]).merge([2, 4, 6]).toArray(); // [1, 2, 3, 4, 5, 6]
|
|
1678
|
+
* ```
|
|
1679
|
+
*/
|
|
1680
|
+
merge(...others: Array<AsyncIterable<T> | Iterable<T>>): Asynciterflow<T>;
|
|
1634
1681
|
}
|
|
1635
1682
|
/**
|
|
1636
1683
|
* Creates an async iterflow instance from an async iterable or iterable.
|
package/dist/index.js
CHANGED
|
@@ -1599,6 +1599,8 @@ var iterflow = class _iterflow {
|
|
|
1599
1599
|
/**
|
|
1600
1600
|
* Alias for stdDev() method for compatibility.
|
|
1601
1601
|
* Calculates the standard deviation of all numeric elements.
|
|
1602
|
+
*
|
|
1603
|
+
* @deprecated Use stdDev() instead. This alias will be removed in v1.0.0.
|
|
1602
1604
|
*/
|
|
1603
1605
|
stddev() {
|
|
1604
1606
|
return this.stdDev();
|
|
@@ -1606,6 +1608,8 @@ var iterflow = class _iterflow {
|
|
|
1606
1608
|
/**
|
|
1607
1609
|
* Alias for drop() method for compatibility.
|
|
1608
1610
|
* Skips the first `count` elements from the iterator.
|
|
1611
|
+
*
|
|
1612
|
+
* @deprecated Use drop() instead. This alias will be removed in v1.0.0.
|
|
1609
1613
|
*/
|
|
1610
1614
|
skip(count) {
|
|
1611
1615
|
return this.drop(count);
|
|
@@ -1842,6 +1846,45 @@ var Asynciterflow = class _Asynciterflow {
|
|
|
1842
1846
|
}
|
|
1843
1847
|
});
|
|
1844
1848
|
}
|
|
1849
|
+
/**
|
|
1850
|
+
* Limits the maximum number of iterations to prevent infinite loops.
|
|
1851
|
+
* Unlike `take()` which silently stops at the limit, this method throws
|
|
1852
|
+
* an OperationError if the limit is exceeded, making infinite loops explicit.
|
|
1853
|
+
*
|
|
1854
|
+
* @param maxIterations - Maximum number of iterations allowed (must be at least 1)
|
|
1855
|
+
* @returns A new async iterflow that will throw if limit is exceeded
|
|
1856
|
+
* @throws {ValidationError} If maxIterations is not a positive integer
|
|
1857
|
+
* @throws {OperationError} If iteration count exceeds maxIterations
|
|
1858
|
+
* @example
|
|
1859
|
+
* ```typescript
|
|
1860
|
+
* // Safely process potentially infinite async iterator
|
|
1861
|
+
* await asyncIter.range(Infinity).limit(1000).toArray(); // Throws after 1000 iterations
|
|
1862
|
+
*
|
|
1863
|
+
* // Regular finite iterator works normally
|
|
1864
|
+
* await asyncIter([1, 2, 3]).limit(10).toArray(); // [1, 2, 3]
|
|
1865
|
+
* ```
|
|
1866
|
+
*/
|
|
1867
|
+
limit(maxIterations) {
|
|
1868
|
+
validatePositiveInteger(maxIterations, "maxIterations", "limit");
|
|
1869
|
+
const self = this;
|
|
1870
|
+
return new _Asynciterflow({
|
|
1871
|
+
async *[Symbol.asyncIterator]() {
|
|
1872
|
+
let count = 0;
|
|
1873
|
+
for await (const value of self) {
|
|
1874
|
+
if (count >= maxIterations) {
|
|
1875
|
+
throw new OperationError(
|
|
1876
|
+
`Iterator exceeded limit of ${maxIterations} iterations`,
|
|
1877
|
+
"limit",
|
|
1878
|
+
void 0,
|
|
1879
|
+
{ maxIterations }
|
|
1880
|
+
);
|
|
1881
|
+
}
|
|
1882
|
+
yield value;
|
|
1883
|
+
count++;
|
|
1884
|
+
}
|
|
1885
|
+
}
|
|
1886
|
+
});
|
|
1887
|
+
}
|
|
1845
1888
|
/**
|
|
1846
1889
|
* Maps each element to an async iterable and flattens the results.
|
|
1847
1890
|
*
|
|
@@ -3422,6 +3465,116 @@ var Asynciterflow = class _Asynciterflow {
|
|
|
3422
3465
|
}
|
|
3423
3466
|
});
|
|
3424
3467
|
}
|
|
3468
|
+
/**
|
|
3469
|
+
* Interleaves elements from this async iterator with elements from other async/sync iterables.
|
|
3470
|
+
* Takes one element from each iterable in round-robin fashion.
|
|
3471
|
+
*
|
|
3472
|
+
* @param others - Variable number of async/sync iterables to interleave with
|
|
3473
|
+
* @returns A new async iterflow with elements from all iterables interleaved
|
|
3474
|
+
* @example
|
|
3475
|
+
* ```typescript
|
|
3476
|
+
* await asyncIter([1, 2, 3]).interleave([4, 5, 6]).toArray(); // [1, 4, 2, 5, 3, 6]
|
|
3477
|
+
* ```
|
|
3478
|
+
*/
|
|
3479
|
+
interleave(...others) {
|
|
3480
|
+
const self = this;
|
|
3481
|
+
return new _Asynciterflow({
|
|
3482
|
+
async *[Symbol.asyncIterator]() {
|
|
3483
|
+
const allIterables = [self, ...others];
|
|
3484
|
+
if (allIterables.length === 0) return;
|
|
3485
|
+
const iterators = allIterables.map(
|
|
3486
|
+
(it) => Symbol.asyncIterator in it ? it[Symbol.asyncIterator]() : (async function* () {
|
|
3487
|
+
yield* it;
|
|
3488
|
+
})()
|
|
3489
|
+
);
|
|
3490
|
+
const active = new Set(iterators);
|
|
3491
|
+
while (active.size > 0) {
|
|
3492
|
+
for (const iterator of iterators) {
|
|
3493
|
+
if (!active.has(iterator)) continue;
|
|
3494
|
+
const result = await iterator.next();
|
|
3495
|
+
if (result.done) {
|
|
3496
|
+
active.delete(iterator);
|
|
3497
|
+
} else {
|
|
3498
|
+
yield result.value;
|
|
3499
|
+
}
|
|
3500
|
+
}
|
|
3501
|
+
}
|
|
3502
|
+
}
|
|
3503
|
+
});
|
|
3504
|
+
}
|
|
3505
|
+
/**
|
|
3506
|
+
* Merges this async iterator with other sorted async/sync iterables into a single sorted async iterator.
|
|
3507
|
+
* Assumes all input iterables are already sorted in ascending order.
|
|
3508
|
+
*
|
|
3509
|
+
* @param others - Variable number of sorted async/sync iterables to merge with
|
|
3510
|
+
* @returns A new async iterflow with all elements merged in sorted order
|
|
3511
|
+
* @example
|
|
3512
|
+
* ```typescript
|
|
3513
|
+
* await asyncIter([1, 3, 5]).merge([2, 4, 6]).toArray(); // [1, 2, 3, 4, 5, 6]
|
|
3514
|
+
* ```
|
|
3515
|
+
*/
|
|
3516
|
+
merge(...others) {
|
|
3517
|
+
const self = this;
|
|
3518
|
+
return new _Asynciterflow({
|
|
3519
|
+
async *[Symbol.asyncIterator]() {
|
|
3520
|
+
const allIterables = [self, ...others];
|
|
3521
|
+
if (allIterables.length === 0) return;
|
|
3522
|
+
const compareFn = (a, b) => {
|
|
3523
|
+
if (a < b) return -1;
|
|
3524
|
+
if (a > b) return 1;
|
|
3525
|
+
return 0;
|
|
3526
|
+
};
|
|
3527
|
+
const heap = [];
|
|
3528
|
+
for (let i = 0; i < allIterables.length; i++) {
|
|
3529
|
+
const iterable = allIterables[i];
|
|
3530
|
+
const iterator = Symbol.asyncIterator in iterable ? iterable[Symbol.asyncIterator]() : (async function* () {
|
|
3531
|
+
yield* iterable;
|
|
3532
|
+
})();
|
|
3533
|
+
const result = await iterator.next();
|
|
3534
|
+
if (!result.done) {
|
|
3535
|
+
heap.push({ value: result.value, iterator, index: i });
|
|
3536
|
+
}
|
|
3537
|
+
}
|
|
3538
|
+
const bubbleDown = (index) => {
|
|
3539
|
+
const length = heap.length;
|
|
3540
|
+
while (true) {
|
|
3541
|
+
let smallest = index;
|
|
3542
|
+
const leftChild = 2 * index + 1;
|
|
3543
|
+
const rightChild = 2 * index + 2;
|
|
3544
|
+
if (leftChild < length && compareFn(heap[leftChild].value, heap[smallest].value) < 0) {
|
|
3545
|
+
smallest = leftChild;
|
|
3546
|
+
}
|
|
3547
|
+
if (rightChild < length && compareFn(heap[rightChild].value, heap[smallest].value) < 0) {
|
|
3548
|
+
smallest = rightChild;
|
|
3549
|
+
}
|
|
3550
|
+
if (smallest === index) break;
|
|
3551
|
+
const temp = heap[index];
|
|
3552
|
+
heap[index] = heap[smallest];
|
|
3553
|
+
heap[smallest] = temp;
|
|
3554
|
+
index = smallest;
|
|
3555
|
+
}
|
|
3556
|
+
};
|
|
3557
|
+
for (let i = Math.floor(heap.length / 2) - 1; i >= 0; i--) {
|
|
3558
|
+
bubbleDown(i);
|
|
3559
|
+
}
|
|
3560
|
+
while (heap.length > 0) {
|
|
3561
|
+
const min = heap[0];
|
|
3562
|
+
yield min.value;
|
|
3563
|
+
const nextResult = await min.iterator.next();
|
|
3564
|
+
if (nextResult.done) {
|
|
3565
|
+
heap[0] = heap[heap.length - 1];
|
|
3566
|
+
heap.pop();
|
|
3567
|
+
if (heap.length > 0) {
|
|
3568
|
+
bubbleDown(0);
|
|
3569
|
+
}
|
|
3570
|
+
} else {
|
|
3571
|
+
heap[0] = { value: nextResult.value, iterator: min.iterator, index: min.index };
|
|
3572
|
+
bubbleDown(0);
|
|
3573
|
+
}
|
|
3574
|
+
}
|
|
3575
|
+
}
|
|
3576
|
+
});
|
|
3577
|
+
}
|
|
3425
3578
|
};
|
|
3426
3579
|
function asyncIter(source) {
|
|
3427
3580
|
if (Symbol.asyncIterator in source) {
|