Skip to content

Commit

Permalink
feat: join
Browse files Browse the repository at this point in the history
  • Loading branch information
ilteoood committed Nov 29, 2023
1 parent c4023c0 commit 0296cbb
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,19 @@ fromIterable(new Set([1, 2, 3]))
```
</details>
<details>
<summary>join</summary>
```javascript
import { join } from '@ilteoood/re-flusso/join';

.pipeThrough(
join('-')
)

```
</details>
<details>
<summary>last</summary>
Expand Down
31 changes: 31 additions & 0 deletions src/join.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const joinNewItem =
(separator: string) =>
<T>(chunk: T) =>
`${separator}${chunk}`;

export const join = (
separator = ",",
writableStrategy?: QueuingStrategy,
readableStrategy?: QueuingStrategy,
) => {
let joinChunk = (chunk: string) => {
joinChunk = joinNewItem(separator);
return chunk;
};

let buffer = "";

return new TransformStream<string, string>(
{
transform(chunk, controller) {
buffer += joinChunk(chunk);
},
flush(controller) {
controller.enqueue(buffer);
buffer = "";
},
},
writableStrategy,
readableStrategy,
);
};
41 changes: 41 additions & 0 deletions test/join.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { describe, expect, test } from "vitest";
import { fromIterable } from "../src/fromIterable";
import { join } from "../src/join";
import { pipeline } from "../src/pipeline";
import { toArray } from "../src/toArray";

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

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

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

test("should join strings with custom separator", async () => {
const destinationArray = [];

await pipeline(
fromIterable([1, 2, 3]),
join("-"),
toArray(destinationArray),
);

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

test("should not throw with objects", async () => {
const destinationArray = [];

await pipeline(
fromIterable([{ a: 1 }, { a: 2 }, { a: 3 }]),
join("-"),
toArray(destinationArray),
);

expect(destinationArray).toEqual([
"[object Object]-[object Object]-[object Object]",
]);
});
});
2 changes: 1 addition & 1 deletion test/ndjson.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe("ndJson", () => {
it("should parse an ndJson string", async () => {
const destinationArray = [];
const response = await fetch(
"https://gist.githubusercontent.com/rfmcnally/0a5a16e09374da7dd478ffbe6ba52503/raw/095e75121f31a8b7dc88aa89dbd637a944ce264a/ndJson-sample.json",
"https://gist.githubusercontent.com/rfmcnally/0a5a16e09374da7dd478ffbe6ba52503/raw/095e75121f31a8b7dc88aa89dbd637a944ce264a/ndjson-sample.json",
);

await pipeline(
Expand Down

0 comments on commit 0296cbb

Please sign in to comment.