Skip to content

Commit

Permalink
Merge pull request #15 from ilteoood/feat/sum
Browse files Browse the repository at this point in the history
Feat/sum
  • Loading branch information
ilteoood authored Jun 21, 2024
2 parents 3dbbb11 + ac3ffc2 commit eee3cff
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,9 @@ fromRange(1, 3)
```javascript
import { greaterThan } from '@ilteoood/re-flusso/numbers/greaterThan';
greaterThan(3)
.pipeThrough(
greaterThan(3)
)
```
</details>
<details>
Expand All @@ -279,7 +281,9 @@ greaterThan(3)
```javascript
import { greaterThanEqual } from '@ilteoood/re-flusso/numbers/greaterThanEqual';
greaterThanEqual(3)
.pipeThrough(
greaterThanEqual(3)
)
```
</details>
<details>
Expand All @@ -288,7 +292,21 @@ greaterThanEqual(3)
```javascript
import { lessThan } from '@ilteoood/re-flusso/numbers/lessThan';
lessThan(3)
.pipeThrough(
lessThan(3)
)
```
</details>
<details>
<summary>&nbsp;&nbsp;&nbsp;&nbsp;sum</summary>
```javascript
import { sum } from '@ilteoood/re-flusso/numbers/sum';
.pipeThrough(
sum()
)
```
</details>
<details>
Expand Down
13 changes: 13 additions & 0 deletions src/numbers/sum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { reduce } from "../reduce";

export const sum = (
writableStrategy?: QueuingStrategy,
readableStrategy?: QueuingStrategy,
) => {
return reduce<number, number>(
(accumulator: number, currentValue: number) => accumulator + currentValue,
0,
writableStrategy,
readableStrategy,
);
};
4 changes: 2 additions & 2 deletions src/reduce.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const reduce = <T, A>(
reducer: (accumulator: A | undefined, currentValue: T) => A | Promise<A>,
initialValue: A | undefined,
reducer: (accumulator: A, currentValue: T) => A | Promise<A>,
initialValue: A,
writableStrategy?: QueuingStrategy,
readableStrategy?: QueuingStrategy,
) => {
Expand Down
24 changes: 24 additions & 0 deletions test/numbers/sum.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { describe, expect, test } from "vitest";
import { fromRange } from "../../src/numbers/fromRange";
import { pipeline } from "../../src/pipeline";
import { toArray } from "../../src/toArray";
import { fromIterable } from "../../src/fromIterable";
import { sum } from "../../src/numbers/sum";

describe("sum", () => {
test("should work with empty list", async () => {
const destinationArray = [];

await pipeline(fromIterable([]), sum(), toArray(destinationArray));

expect(destinationArray).toEqual([0]);
});

test("should correctly sum numbers", async () => {
const destinationArray = [];

await pipeline(fromRange(1, 3), sum(), toArray(destinationArray));

expect(destinationArray).toEqual([6]);
});
});

0 comments on commit eee3cff

Please sign in to comment.