From a7715d2c76b51e4c4fdb2f0d1e86baea224845c6 Mon Sep 17 00:00:00 2001 From: Smoren Date: Sun, 3 Mar 2024 16:47:19 +0300 Subject: [PATCH] new example tests and updated readme --- README.md | 11 +++++--- tests/examples/examples.test.ts | 48 +++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 tests/examples/examples.test.ts diff --git a/README.md b/README.md index 232b0a5..496c4d6 100644 --- a/README.md +++ b/README.md @@ -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 } ``` @@ -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] @@ -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] @@ -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] diff --git a/tests/examples/examples.test.ts b/tests/examples/examples.test.ts new file mode 100644 index 0000000..e175dd9 --- /dev/null +++ b/tests/examples/examples.test.ts @@ -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); +});