Skip to content

Commit

Permalink
feat: concat
Browse files Browse the repository at this point in the history
  • Loading branch information
ilteoood committed Nov 27, 2023
1 parent 57d7bf5 commit 61c4ef2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ const chunkSize = 3;
```
</details>
<details>
<summary>concat</summary>
```javascript
import { concat } from '@ilteoood/re-flusso/concat';

concat(
fromIterable([1]),
fromIterable([2]),
fromIterable([3])
)
```
</details>
<details>
<summary>filter</summary>
Expand Down
24 changes: 24 additions & 0 deletions src/concat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export const concat = (...readableStreams: ReadableStream[]) => {
const fallbackedStreams = readableStreams ?? [];
const readableStreamsLength = fallbackedStreams.length;

return new ReadableStream({
async start(controller) {
for (let i = 0; i < readableStreamsLength; i++) {
const reader = fallbackedStreams[i].getReader();

while (true) {
const readResult = await reader.read();

if (readResult.done) {
break;
}

controller.enqueue(readResult.value);
}
}

controller.close();
},
});
};
30 changes: 30 additions & 0 deletions test/concat.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { describe, expect, test } from "vitest";

import { concat } from "../src/concat";
import { fromIterable } from "../src/fromIterable";
import { pipeline } from "../src/pipeline";
import { toArray } from "../src/toArray";

describe("concat", () => {
test("should concat streams", async () => {
const destinationArray = [];

await pipeline(
concat(fromIterable([1]), fromIterable([2]), fromIterable([3])),
toArray(destinationArray),
);

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

test("should concat streams even if first stream is empty", async () => {
const destinationArray = [];

await pipeline(
concat(fromIterable([]), fromIterable([2]), fromIterable([3])),
toArray(destinationArray),
);

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

0 comments on commit 61c4ef2

Please sign in to comment.