Skip to content

Commit

Permalink
new example tests and updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed Mar 3, 2024
1 parent 0bb6fb8 commit a7715d2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
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);
});

0 comments on commit a7715d2

Please sign in to comment.