Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New example tests and updated readme #49

Merged
merged 1 commit into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ for (const [letter, number] of multi.zip(['a', 'b'], [1, 2])) {
}

// Async example
for await (const [letter, number] of multi.zipAsync(['a', 'b'], [1, 2])) {
const letters = ['a', 'b'].map((x) => Promise.resolve(x));
const numbers = [1, 2].map((x) => Promise.resolve(x));

for await (const [letter, number] of multi.zipAsync(letters, numbers)) {
console.log(`${letter}${number}`); // a1, b2
}
```
Expand All @@ -46,7 +49,7 @@ const result1 = Stream.of([1, 1, 2, 2, 3, 4, 5])
.toSum(); // 14

// Async example
const result2 = await AsyncStream.of([1, 1, 2, 2, 3, 4, 5])
const result2 = await AsyncStream.of([1, 1, 2, 2, 3, 4, 5].map((x) => Promise.resolve(x)))
.distinct() // [1, 2, 3, 4, 5]
.map((x) => x**2) // [1, 4, 9, 16, 25]
.filter((x) => x < 10) // [1, 4, 9]
Expand Down Expand Up @@ -2076,7 +2079,7 @@ Streams are made up of:
.toSum(); // 14

// Async example
const result2 = await AsyncStream.of([1, 1, 2, 2, 3, 4, 5])
const result2 = await AsyncStream.of([1, 1, 2, 2, 3, 4, 5].map((x) => Promise.resolve(x)))
.distinct() // [1, 2, 3, 4, 5]
.map((x) => x**2) // [1, 4, 9, 16, 25]
.filter((x) => x < 10) // [1, 4, 9]
Expand All @@ -2094,7 +2097,7 @@ Streams are made up of:
}

// Async example
const result2 = AsyncStream.of([1, 1, 2, 2, 3, 4, 5])
const result2 = AsyncStream.of([1, 1, 2, 2, 3, 4, 5].map((x) => Promise.resolve(x)))
.distinct() // [1, 2, 3, 4, 5]
.map((x) => x**2) // [1, 4, 9, 16, 25]
.filter((x) => x < 10); // [1, 4, 9]
Expand Down
48 changes: 48 additions & 0 deletions tests/examples/examples.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { multi, Stream, AsyncStream } from "../../src";

it("Loop example", () => {
// When
const result = [];

for (const [letter, number] of multi.zip(['a', 'b'], [1, 2])) {
result.push(`${letter}${number}`);
}

expect(result).toStrictEqual(['a1', 'b2']);
});

it("Loop async example", async () => {
// When
const result = [];

const letters = ['a', 'b'].map((x) => Promise.resolve(x));
const numbers = [1, 2].map((x) => Promise.resolve(x))

for await (const [letter, number] of multi.zipAsync(letters, numbers)) {
result.push(`${letter}${number}`);
}

expect(result).toStrictEqual(['a1', 'b2']);
});

it("Stream example", () => {
// When
const result = Stream.of([1, 1, 2, 2, 3, 4, 5])
.distinct()
.map((x) => Number(x)**2)
.filter((x) => Number(x) < 10)
.toSum();

expect(result).toEqual(14);
});

it("Stream async example", async () => {
// When
const result = await AsyncStream.of([1, 1, 2, 2, 3, 4, 5].map((x) => Promise.resolve(x)))
.distinct()
.map((x) => Number(x)**2)
.filter((x) => Number(x) < 10)
.toSum();

expect(result).toEqual(14);
});
Loading