Skip to content

Commit

Permalink
function reduce toRange added and covered by tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anastansa authored and Smoren committed May 29, 2023
1 parent 638e07f commit 6a58dc4
Show file tree
Hide file tree
Showing 7 changed files with 667 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/async-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import {
toMinAsync,
toMinMaxAsync,
toProductAsync,
toRangeAsync,
toSumAsync,
toValueAsync,
} from "./reduce";
Expand Down Expand Up @@ -851,6 +852,17 @@ export class AsyncStream {
return await toProductAsync(this as AsyncIterable<number>);
}

/**
* Reduces given collection to its range.
*
* Returns 0 if given collection is empty.
*
* @see reduce.toRangeAsync
*/
async toRange(): Promise<number> {
return await toRangeAsync(this as AsyncIterable<number>);
}

/**
* Returns true if all elements of stream match the predicate function.
*
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ import {
toMin,
toMinMax,
toProduct,
toRange,
toSum,
toValue,
toFirst,
Expand All @@ -101,6 +102,7 @@ import {
toMinAsync,
toMinMaxAsync,
toProductAsync,
toRangeAsync,
toSumAsync,
toValueAsync,
} from "./reduce";
Expand Down Expand Up @@ -255,6 +257,7 @@ export const reduce = {
toMin,
toMinMax,
toProduct,
toRange,
toSum,
toValue,
toAverageAsync,
Expand All @@ -266,6 +269,7 @@ export const reduce = {
toMinAsync,
toMinMaxAsync,
toProductAsync,
toRangeAsync,
toSumAsync,
toValueAsync,
};
Expand Down
32 changes: 32 additions & 0 deletions src/reduce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,38 @@ export async function toMinMaxAsync<T>(
)) as [T?, T?];
}

/**
* Reduces given collection to its range.
*
* Returns 0 if given collection is empty.
*
* @param numbers
*/
export function toRange(numbers: Iterable<number> | Iterator<number>): number {
const [min, max] = toMinMax(numbers);

return (max ?? 0) - (min ?? 0);
}

/**
* Reduces given async collection to its range.
*
* Returns 0 if given async collection is empty.
*
* @param numbers
*/
export async function toRangeAsync(
numbers:
| AsyncIterable<number>
| AsyncIterator<number>
| Iterable<number>
| Iterator<number>
): Promise<number> {
const [min, max] = await toMinMaxAsync(numbers);

return (max ?? 0) - (min ?? 0);
}

/**
* Reduces given collection to the sum of its items.
*
Expand Down
12 changes: 12 additions & 0 deletions src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
toMin,
toMinMax,
toProduct,
toRange,
toSum,
toValue,
} from "./reduce";
Expand Down Expand Up @@ -758,6 +759,17 @@ export class Stream {
return toProduct(this as Iterable<number>);
}

/**
* Reduces given collection to its range.
*
* Returns 0 if given collection is empty.
*
* @see reduce.toRange
*/
toRange(): number {
return toRange(this as Iterable<number>);
}

/**
* Returns true if all elements of stream match the predicate function.
*
Expand Down
122 changes: 122 additions & 0 deletions tests/async-stream/reduce.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,18 @@ function dataProviderForArrays(): Array<unknown> {
.toAverage(),
0,
],
[
[],
(iterable: Iterable<unknown>) => AsyncStream.of(iterable)
.toRange(),
0,
],
[
[1, -1, 2, -2, 3, -3],
(iterable: Iterable<unknown>) => AsyncStream.of(iterable)
.toRange(),
6,
],
[
[],
(iterable: Iterable<unknown>) => AsyncStream.of(iterable)
Expand Down Expand Up @@ -657,6 +669,18 @@ function dataProviderForGenerators(): Array<unknown> {
.toAverage(),
0,
],
[
createGeneratorFixture([]),
(iterable: Iterable<unknown>) => AsyncStream.of(iterable)
.toRange(),
0,
],
[
createGeneratorFixture([1, -1, 2, -2, 3, -3]),
(iterable: Iterable<unknown>) => AsyncStream.of(iterable)
.toRange(),
6,
],
[
createGeneratorFixture([]),
(iterable: Iterable<unknown>) => AsyncStream.of(iterable)
Expand Down Expand Up @@ -1092,6 +1116,18 @@ function dataProviderForIterables(): Array<unknown> {
.toAverage(),
0,
],
[
createIterableFixture([]),
(iterable: Iterable<unknown>) => AsyncStream.of(iterable)
.toRange(),
0,
],
[
createIterableFixture([1, -1, 2, -2, 3, -3]),
(iterable: Iterable<unknown>) => AsyncStream.of(iterable)
.toRange(),
6,
],
[
createIterableFixture([]),
(iterable: Iterable<unknown>) => AsyncStream.of(iterable)
Expand Down Expand Up @@ -1527,6 +1563,18 @@ function dataProviderForIterators(): Array<unknown> {
.toAverage(),
0,
],
[
createIteratorFixture([]),
(iterable: Iterable<unknown>) => AsyncStream.of(iterable)
.toRange(),
0,
],
[
createIteratorFixture([1, -1, 2, -2, 3, -3]),
(iterable: Iterable<unknown>) => AsyncStream.of(iterable)
.toRange(),
6,
],
[
createIteratorFixture([]),
(iterable: Iterable<unknown>) => AsyncStream.of(iterable)
Expand Down Expand Up @@ -1929,6 +1977,18 @@ function dataProviderForStrings(): Array<unknown> {
.toAverage(),
2,
],
[
'',
(iterable: Iterable<unknown>) => AsyncStream.of(iterable)
.toRange(),
0,
],
[
'123',
(iterable: Iterable<unknown>) => AsyncStream.of(iterable)
.toRange(),
2,
],
[
'',
(iterable: Iterable<unknown>) => AsyncStream.of(iterable)
Expand Down Expand Up @@ -2236,6 +2296,18 @@ function dataProviderForSets(): Array<unknown> {
.toAverage(),
0,
],
[
new Set([]),
(iterable: Iterable<unknown>) => AsyncStream.of(iterable)
.toRange(),
0,
],
[
new Set([1, -1, 2, -2, 3, -3]),
(iterable: Iterable<unknown>) => AsyncStream.of(iterable)
.toRange(),
6,
],
[
new Set([1, 2, 3, 4, 5]),
(iterable: Iterable<unknown>) => AsyncStream.of(iterable)
Expand Down Expand Up @@ -2693,6 +2765,20 @@ function dataProviderForMaps(): Array<unknown> {
.toAverage(),
0,
],
[
createMapFixture([]),
(iterable: Iterable<unknown>) => AsyncStream.of(iterable)
.map((item) => (item as [unknown, number])[1])
.toRange(),
0,
],
[
createMapFixture([1, -1, 2, -2, 3, -3]),
(iterable: Iterable<unknown>) => AsyncStream.of(iterable)
.map((item) => (item as [unknown, number])[1])
.toRange(),
6,
],
[
createMapFixture([]),
(iterable: Iterable<unknown>) => AsyncStream.of(iterable)
Expand Down Expand Up @@ -3114,6 +3200,18 @@ function dataProviderForAsyncGenerators(): Array<unknown> {
.toAverage(),
0,
],
[
createAsyncGeneratorFixture([]),
(iterable: Iterable<unknown>) => AsyncStream.of(iterable)
.toRange(),
0,
],
[
createAsyncGeneratorFixture([1, -1, 2, -2, 3, -3]),
(iterable: Iterable<unknown>) => AsyncStream.of(iterable)
.toRange(),
6,
],
[
createAsyncGeneratorFixture([]),
(iterable: Iterable<unknown>) => AsyncStream.of(iterable)
Expand Down Expand Up @@ -3549,6 +3647,18 @@ function dataProviderForAsyncIterables(): Array<unknown> {
.toAverage(),
0,
],
[
createAsyncIterableFixture([]),
(iterable: Iterable<unknown>) => AsyncStream.of(iterable)
.toRange(),
0,
],
[
createAsyncIterableFixture([1, -1, 2, -2, 3, -3]),
(iterable: Iterable<unknown>) => AsyncStream.of(iterable)
.toRange(),
6,
],
[
createAsyncIterableFixture([]),
(iterable: Iterable<unknown>) => AsyncStream.of(iterable)
Expand Down Expand Up @@ -3984,6 +4094,18 @@ function dataProviderForAsyncIterators(): Array<unknown> {
.toAverage(),
0,
],
[
createAsyncIteratorFixture([]),
(iterable: Iterable<unknown>) => AsyncStream.of(iterable)
.toRange(),
0,
],
[
createAsyncIteratorFixture([1, -1, 2, -2, 3, -3]),
(iterable: Iterable<unknown>) => AsyncStream.of(iterable)
.toRange(),
6,
],
[
createAsyncIteratorFixture([]),
(iterable: Iterable<unknown>) => AsyncStream.of(iterable)
Expand Down
Loading

0 comments on commit 6a58dc4

Please sign in to comment.