2019-08-15 15:54:50 +00:00
|
|
|
import test from "ava";
|
|
|
|
import { expect } from "chai";
|
|
|
|
import { Readable } from "stream";
|
2019-08-16 13:02:54 +00:00
|
|
|
import { filter } from "../src";
|
2019-08-15 15:54:50 +00:00
|
|
|
|
|
|
|
test.cb("filter() filters elements synchronously", t => {
|
|
|
|
t.plan(2);
|
|
|
|
const source = new Readable({ objectMode: true });
|
|
|
|
const expectedElements = ["a", "c"];
|
|
|
|
let i = 0;
|
|
|
|
source
|
2019-09-07 15:04:33 +00:00
|
|
|
.pipe(
|
|
|
|
filter((element: string) => element !== "b", {
|
|
|
|
readableObjectMode: true,
|
|
|
|
writableObjectMode: true,
|
|
|
|
}),
|
|
|
|
)
|
2019-08-15 15:54:50 +00:00
|
|
|
.on("data", (element: string) => {
|
|
|
|
expect(element).to.equal(expectedElements[i]);
|
|
|
|
t.pass();
|
|
|
|
i++;
|
|
|
|
})
|
|
|
|
.on("error", t.end)
|
|
|
|
.on("end", t.end);
|
|
|
|
|
|
|
|
source.push("a");
|
|
|
|
source.push("b");
|
|
|
|
source.push("c");
|
|
|
|
source.push(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
test.cb("filter() filters elements asynchronously", t => {
|
|
|
|
t.plan(2);
|
|
|
|
const source = new Readable({ objectMode: true });
|
|
|
|
const expectedElements = ["a", "c"];
|
|
|
|
let i = 0;
|
|
|
|
source
|
|
|
|
.pipe(
|
2019-09-07 15:04:33 +00:00
|
|
|
filter(
|
|
|
|
async (element: string) => {
|
|
|
|
await Promise.resolve();
|
|
|
|
return element !== "b";
|
|
|
|
},
|
|
|
|
{ readableObjectMode: true, writableObjectMode: true },
|
|
|
|
),
|
2019-08-15 15:54:50 +00:00
|
|
|
)
|
|
|
|
.on("data", (element: string) => {
|
|
|
|
expect(element).to.equal(expectedElements[i]);
|
|
|
|
t.pass();
|
|
|
|
i++;
|
|
|
|
})
|
|
|
|
.on("error", t.end)
|
|
|
|
.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("filter() emits errors during synchronous filtering", t => {
|
2019-08-15 15:54:50 +00:00
|
|
|
t.plan(2);
|
|
|
|
const source = new Readable({ objectMode: true });
|
|
|
|
source
|
|
|
|
.pipe(
|
2019-09-07 15:04:33 +00:00
|
|
|
filter(
|
|
|
|
(element: string) => {
|
|
|
|
if (element !== "a") {
|
|
|
|
throw new Error("Failed filtering");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
{ readableObjectMode: true, writableObjectMode: true },
|
|
|
|
),
|
2019-08-15 15:54:50 +00:00
|
|
|
)
|
|
|
|
.resume()
|
|
|
|
.on("error", err => {
|
|
|
|
expect(err.message).to.equal("Failed filtering");
|
|
|
|
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("filter() emits errors during asynchronous filtering", t => {
|
2019-08-15 15:54:50 +00:00
|
|
|
t.plan(2);
|
|
|
|
const source = new Readable({ objectMode: true });
|
|
|
|
source
|
|
|
|
.pipe(
|
2019-09-07 15:04:33 +00:00
|
|
|
filter(
|
|
|
|
async (element: string) => {
|
|
|
|
await Promise.resolve();
|
|
|
|
if (element !== "a") {
|
|
|
|
throw new Error("Failed filtering");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
{ readableObjectMode: true, writableObjectMode: true },
|
|
|
|
),
|
2019-08-15 15:54:50 +00:00
|
|
|
)
|
|
|
|
.resume()
|
|
|
|
.on("error", err => {
|
|
|
|
expect(err.message).to.equal("Failed filtering");
|
|
|
|
t.pass();
|
|
|
|
})
|
|
|
|
.on("end", t.end);
|
|
|
|
|
|
|
|
source.push("a");
|
|
|
|
source.push("b");
|
|
|
|
source.push("c");
|
|
|
|
source.push(null);
|
|
|
|
});
|