2019-08-15 15:54:50 +00:00
|
|
|
import { Readable } from "stream";
|
|
|
|
import test from "ava";
|
|
|
|
import { expect } from "chai";
|
2019-12-02 21:02:40 +00:00
|
|
|
import mhysa from "../src";
|
|
|
|
const { flatMap } = mhysa({ objectMode: true });
|
2019-08-15 15:54:50 +00:00
|
|
|
|
|
|
|
test.cb("flatMap() maps elements synchronously", t => {
|
|
|
|
t.plan(6);
|
|
|
|
const source = new Readable({ objectMode: true });
|
|
|
|
const expectedElements = ["a", "A", "b", "B", "c", "C"];
|
|
|
|
let i = 0;
|
|
|
|
source
|
|
|
|
.pipe(flatMap((element: string) => [element, element.toUpperCase()]))
|
|
|
|
.on("data", (element: string) => {
|
|
|
|
expect(element).to.equal(expectedElements[i]);
|
|
|
|
t.pass();
|
|
|
|
i++;
|
|
|
|
})
|
|
|
|
.on("end", t.end);
|
|
|
|
|
|
|
|
source.push("a");
|
|
|
|
source.push("b");
|
|
|
|
source.push("c");
|
|
|
|
source.push(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
test.cb("flatMap() maps elements asynchronously", t => {
|
|
|
|
t.plan(6);
|
|
|
|
const source = new Readable({ objectMode: true });
|
|
|
|
const expectedElements = ["a", "A", "b", "B", "c", "C"];
|
|
|
|
let i = 0;
|
|
|
|
source
|
|
|
|
.pipe(
|
|
|
|
flatMap(async (element: string) => {
|
|
|
|
await Promise.resolve();
|
|
|
|
return [element, element.toUpperCase()];
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.on("data", (element: string) => {
|
|
|
|
expect(element).to.equal(expectedElements[i]);
|
|
|
|
t.pass();
|
|
|
|
i++;
|
|
|
|
})
|
|
|
|
.on("end", t.end);
|
|
|
|
|
|
|
|
source.push("a");
|
|
|
|
source.push("b");
|
|
|
|
source.push("c");
|
|
|
|
source.push(null);
|
|
|
|
});
|
|
|
|
|
2019-09-12 13:41:04 +00:00
|
|
|
test.cb.skip("flatMap() emits errors during synchronous mapping", t => {
|
2019-08-15 15:54:50 +00:00
|
|
|
t.plan(2);
|
|
|
|
const source = new Readable({ objectMode: true });
|
|
|
|
source
|
|
|
|
.pipe(
|
|
|
|
flatMap((element: string) => {
|
|
|
|
if (element !== "a") {
|
|
|
|
throw new Error("Failed mapping");
|
|
|
|
}
|
|
|
|
return [element, element.toUpperCase()];
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.resume()
|
|
|
|
.on("error", err => {
|
|
|
|
expect(err.message).to.equal("Failed mapping");
|
|
|
|
t.pass();
|
|
|
|
})
|
|
|
|
.on("end", t.end);
|
|
|
|
|
|
|
|
source.push("a");
|
|
|
|
source.push("b");
|
|
|
|
source.push("c");
|
|
|
|
source.push(null);
|
|
|
|
});
|
|
|
|
|
2019-09-12 13:41:04 +00:00
|
|
|
test.cb.skip("flatMap() emits errors during asynchronous mapping", t => {
|
2019-08-15 15:54:50 +00:00
|
|
|
t.plan(2);
|
|
|
|
const source = new Readable({ objectMode: true });
|
|
|
|
source
|
|
|
|
.pipe(
|
|
|
|
flatMap(async (element: string) => {
|
|
|
|
await Promise.resolve();
|
|
|
|
if (element !== "a") {
|
|
|
|
throw new Error("Failed mapping");
|
|
|
|
}
|
|
|
|
return [element, element.toUpperCase()];
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.resume()
|
|
|
|
.on("error", err => {
|
|
|
|
expect(err.message).to.equal("Failed mapping");
|
|
|
|
t.pass();
|
|
|
|
})
|
|
|
|
.on("end", t.end);
|
|
|
|
|
|
|
|
source.push("a");
|
|
|
|
source.push("b");
|
|
|
|
source.push("c");
|
|
|
|
source.push(null);
|
|
|
|
});
|