-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from clear/feat/stream-flatten
- Loading branch information
Showing
9 changed files
with
603 additions
and
517 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"windpipe": minor | ||
--- | ||
|
||
Implement .flatten() method on streams |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { describe, test } from "vitest"; | ||
import $ from "../src"; | ||
|
||
describe.concurrent("stream consumption", () => { | ||
describe.concurrent("to array", () => { | ||
test("values", async ({ expect }) => { | ||
expect.assertions(1); | ||
|
||
const array = await $.from([1, 2, 3]).toArray(); | ||
|
||
expect(array).toEqual([1, 2, 3]); | ||
}); | ||
|
||
test("values with errors on stream", async ({ expect }) => { | ||
expect.assertions(1); | ||
|
||
const array = await $.from([ | ||
1, | ||
$.error("known"), | ||
2, | ||
3, | ||
$.unknown("$.error", []), | ||
]).toArray(); | ||
|
||
expect(array).toEqual([1, 2, 3]); | ||
}); | ||
|
||
test("values with no items on stream", async ({ expect }) => { | ||
expect.assertions(1); | ||
|
||
const array = await $.from([]).toArray(); | ||
|
||
expect(array).toEqual([]); | ||
}); | ||
|
||
test("atoms", async ({ expect }) => { | ||
expect.assertions(1); | ||
|
||
const array = await $.from([1, 2, 3]).toArray({ atoms: true }); | ||
|
||
expect(array).toEqual([$.ok(1), $.ok(2), $.ok(3)]); | ||
}); | ||
|
||
test("atoms with errors on stream", async ({ expect }) => { | ||
expect.assertions(1); | ||
|
||
const array = await $.from([ | ||
1, | ||
$.error("known"), | ||
2, | ||
3, | ||
$.unknown("$.error", []), | ||
]).toArray({ | ||
atoms: true, | ||
}); | ||
|
||
expect(array).toEqual([ | ||
$.ok(1), | ||
$.error("known"), | ||
$.ok(2), | ||
$.ok(3), | ||
$.unknown("$.error", []), | ||
]); | ||
}); | ||
|
||
test("atoms with no items on stream", async ({ expect }) => { | ||
expect.assertions(1); | ||
|
||
const array = await $.from([]).toArray({ atoms: true }); | ||
|
||
expect(array).toEqual([]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { describe, test } from "vitest"; | ||
import $ from "../src"; | ||
import { Readable } from "stream"; | ||
|
||
describe.concurrent("stream creation", () => { | ||
describe.concurrent("from promise", () => { | ||
test("resolving promise to emit value", async ({ expect }) => { | ||
expect.assertions(1); | ||
|
||
const s = $.fromPromise(Promise.resolve(10)); | ||
|
||
expect(await s.toArray({ atoms: true })).toEqual([$.ok(10)]); | ||
}); | ||
}); | ||
|
||
describe.concurrent("from iterator", () => { | ||
test("multi-value generator", async ({ expect }) => { | ||
expect.assertions(1); | ||
|
||
const s = $.fromIterator( | ||
(function* () { | ||
yield 1; | ||
yield 2; | ||
yield 3; | ||
})(), | ||
); | ||
|
||
expect(await s.toArray({ atoms: true })).toEqual([$.ok(1), $.ok(2), $.ok(3)]); | ||
}); | ||
|
||
test("multi-value async generator", async ({ expect }) => { | ||
expect.assertions(1); | ||
|
||
const s = $.fromIterator( | ||
(async function* () { | ||
yield 1; | ||
yield 2; | ||
yield 3; | ||
})(), | ||
); | ||
|
||
expect(await s.toArray({ atoms: true })).toEqual([$.ok(1), $.ok(2), $.ok(3)]); | ||
}); | ||
}); | ||
|
||
describe.concurrent("from iterable", () => { | ||
test("array iterable", async ({ expect }) => { | ||
expect.assertions(1); | ||
|
||
const s = $.fromIterable([1, 2, 3]); | ||
|
||
expect(await s.toArray({ atoms: true })).toEqual([$.ok(1), $.ok(2), $.ok(3)]); | ||
}); | ||
|
||
test("readable stream", async ({ expect }) => { | ||
expect.assertions(1); | ||
|
||
const s = $.fromIterable(Readable.from([1, 2, 3])); | ||
|
||
expect(await s.toArray({ atoms: true })).toEqual([$.ok(1), $.ok(2), $.ok(3)]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { describe, test } from "vitest"; | ||
import $ from "../src"; | ||
|
||
describe.concurrent("error handling", () => { | ||
test("throw in map", async ({ expect }) => { | ||
expect.assertions(1); | ||
|
||
const s = $.from([1, 2, 3]).map((n) => { | ||
if (n === 2) { | ||
// Unhandled error | ||
throw new Error("bad number"); | ||
} else { | ||
return n; | ||
} | ||
}); | ||
|
||
expect(await s.toArray({ atoms: true })).toEqual([ | ||
$.ok(1), | ||
$.unknown(new Error("bad number"), ["map"]), | ||
$.ok(3), | ||
]); | ||
}); | ||
|
||
test("promise rejection in map", async ({ expect }) => { | ||
expect.assertions(1); | ||
|
||
async function process(n: number) { | ||
if (n === 2) { | ||
throw new Error("bad number"); | ||
} else { | ||
return n; | ||
} | ||
} | ||
|
||
const s = $.from([1, 2, 3]).map(process); | ||
|
||
expect(await s.toArray({ atoms: true })).toEqual([ | ||
$.ok(1), | ||
$.unknown(new Error("bad number"), ["map"]), | ||
$.ok(3), | ||
]); | ||
}); | ||
|
||
test("track multiple transforms", async ({ expect }) => { | ||
expect.assertions(1); | ||
|
||
const s = $.from([1, 2, 3, 4, 5]) | ||
.map((n) => { | ||
if (n === 2) { | ||
// Unhandled error | ||
throw new Error("bad number"); | ||
} else { | ||
return n; | ||
} | ||
}) | ||
.filter((n) => n % 2 === 0); | ||
|
||
expect(await s.toArray({ atoms: true })).toEqual([ | ||
$.unknown(new Error("bad number"), ["map"]), | ||
$.ok(4), | ||
]); | ||
}); | ||
|
||
test("error thrown in later transform", async ({ expect }) => { | ||
expect.assertions(1); | ||
|
||
const s = $.from([1, 2, 3, 4, 5]) | ||
.filter((n) => n > 1) | ||
.map((n) => { | ||
if (n % 2 === 1) { | ||
return n * 10; | ||
} else { | ||
return n; | ||
} | ||
}) | ||
.map((n) => { | ||
if (n === 2) { | ||
// Unhandled error | ||
throw new Error("bad number"); | ||
} else { | ||
return n; | ||
} | ||
}) | ||
.filter((n) => n % 2 === 0); | ||
|
||
expect(await s.toArray({ atoms: true })).toEqual([ | ||
$.unknown(new Error("bad number"), ["filter", "map", "map"]), | ||
$.ok(30), | ||
$.ok(4), | ||
$.ok(50), | ||
]); | ||
}); | ||
}); |
Oops, something went wrong.