Skip to content

Commit

Permalink
feat: fromRange
Browse files Browse the repository at this point in the history
  • Loading branch information
ilteoood committed Jan 15, 2024
1 parent a9b9a9c commit 47979fd
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,21 @@ const errorToThrow = new Error('Stream is empty');
```
</details>
<details>
<summary>numbers</summary>
<details>
<summary>&nbsp;&nbsp;&nbsp;&nbsp;fromRange</summary>
```javascript
import { fromRange } from '@ilteoood/re-flusso/numbers/fromRange';
fromRange(1, 3)
```
</details>
</details>
</details>
<details>
<summary>pipeline</summary>
Expand Down
20 changes: 20 additions & 0 deletions src/numbers/fromRange.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export const fromRange = (
min: number,
max: number,
strategy?: QueuingStrategy,
): ReadableStream<number> => {
let currentValue = min;
return new ReadableStream<number>(
{
pull(controller) {
if (currentValue <= max) {
controller.enqueue(currentValue);
currentValue++;
} else {
controller.close();
}
},
},
strategy,
);
};
22 changes: 22 additions & 0 deletions test/numbers/fromRange.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { describe, expect, test } from "vitest";
import { fromRange } from "../../src/numbers/fromRange";
import { pipeline } from "../../src/pipeline";
import { toArray } from "../../src/toArray";

describe("fromRange", () => {
test("should generate range", async () => {
const destinationArray = [];

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

expect(destinationArray).toEqual([1, 2, 3]);
});

test("should generate empty range when min > max", async () => {
const destinationArray = [];

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

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

0 comments on commit 47979fd

Please sign in to comment.