Skip to content

Commit

Permalink
feat: allow for error/exception atoms to be rejected from toArray (#37
Browse files Browse the repository at this point in the history
)
  • Loading branch information
andogq committed Jul 16, 2024
2 parents 14879f7 + 70fe273 commit 0af6fd4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-balloons-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"windpipe": patch
---

feat: reject error atom option for `toArray`
7 changes: 5 additions & 2 deletions src/stream/consumption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,22 @@ export class StreamConsumption<T, E> extends StreamBase {
* Iterate through each atom in the stream, and return them as a single array.
*
* @param options.atoms - Return every atom on the stream.
* @param options.reject - If an error or exception is encountered, reject the promise with it.
*
* @group Consumption
*/
async toArray(options?: { atoms: false }): Promise<T[]>;
async toArray(options?: { atoms?: false; reject?: boolean }): Promise<T[]>;
async toArray(options?: { atoms: true }): Promise<Atom<T, E>[]>;
async toArray(options?: { atoms?: boolean }): Promise<(Atom<T, E> | T)[]> {
async toArray(options?: { atoms?: boolean; reject?: boolean }): Promise<(Atom<T, E> | T)[]> {
const array: (Atom<T, E> | T)[] = [];

for await (const atom of this) {
if (options?.atoms) {
array.push(atom);
} else if (isOk(atom)) {
array.push(atom.value);
} else if (options?.reject) {
throw atom.value;
}
}

Expand Down
8 changes: 8 additions & 0 deletions test/consumption.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ describe.concurrent("stream consumption", () => {

expect(array).toEqual([]);
});

test("reject when error on stream", async ({ expect }) => {
expect.assertions(1);

const arrayPromise = $.from([$.ok(1), $.error("some error")]).toArray({ reject: true });

expect(arrayPromise).rejects.toThrow("some error");
});
});

describe.concurrent("toReadable", () => {
Expand Down

0 comments on commit 0af6fd4

Please sign in to comment.