Skip to content

Commit

Permalink
feat: split
Browse files Browse the repository at this point in the history
  • Loading branch information
ilteoood committed Nov 29, 2023
1 parent 5d0827c commit 59d3cbe
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,10 @@ fromIterable(new Set([1, 2, 3]))
```javascript
import { join } from '@ilteoood/re-flusso/join';

const separator = ',';

.pipeThrough(
join('-')
join(separator)
)

```
Expand Down Expand Up @@ -227,6 +229,20 @@ const itemsToSkip = 2;
```
</details>
<details>
<summary>split</summary>
```javascript
import { split } from '@ilteoood/re-flusso/split';
const separator = ',';
.pipeTo(
split(separator)
)
```
</details>
<details>
<summary>toArray</summary>
Expand Down
15 changes: 15 additions & 0 deletions src/split.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const split = <T>(
separator = ",",
writableStrategy?: QueuingStrategy,
readableStrategy?: QueuingStrategy,
) => {
return new TransformStream<string, string[]>(
{
transform(chunk, controller) {
controller.enqueue(chunk.split(separator));
},
},
writableStrategy,
readableStrategy,
);
};
10 changes: 6 additions & 4 deletions test/join.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ describe("join", () => {
const destinationArray = [];

await pipeline(
fromIterable([{ a: 1, toString: () => "a1" }, { a: 2, toString: () => "a2" }, { a: 3, toString: () => "a3" }]),
fromIterable([
{ a: 1, toString: () => "a1" },
{ a: 2, toString: () => "a2" },
{ a: 3, toString: () => "a3" },
]),
join("-"),
toArray(destinationArray),
);

expect(destinationArray).toEqual([
"a1-a2-a3",
]);
expect(destinationArray).toEqual(["a1-a2-a3"]);
});

test("should not throw with arrays", async () => {
Expand Down
28 changes: 28 additions & 0 deletions test/split.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { describe, expect, test } from "vitest";

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

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

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

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

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

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

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

0 comments on commit 59d3cbe

Please sign in to comment.