Tests
This commit is contained in:
parent
d097fa6aa5
commit
1d0e15890c
@ -19,4 +19,4 @@ export { replace } from "./replace";
|
|||||||
export { split } from "./split";
|
export { split } from "./split";
|
||||||
export { stringify } from "./stringify";
|
export { stringify } from "./stringify";
|
||||||
export { unbatch } from "./unbatch";
|
export { unbatch } from "./unbatch";
|
||||||
export { compose, composeDuplex } from "./compose";
|
export { compose } from "./compose";
|
||||||
|
@ -2,7 +2,6 @@ import {
|
|||||||
pipeline,
|
pipeline,
|
||||||
Transform,
|
Transform,
|
||||||
Writable,
|
Writable,
|
||||||
Pipe,
|
|
||||||
WritableOptions,
|
WritableOptions,
|
||||||
Readable,
|
Readable,
|
||||||
Duplex,
|
Duplex,
|
||||||
@ -12,7 +11,6 @@ import {
|
|||||||
* Return a Readable stream of readable streams concatenated together
|
* Return a Readable stream of readable streams concatenated together
|
||||||
* @param streams Readable streams to concatenate
|
* @param streams Readable streams to concatenate
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// First Readable --> Readable
|
// First Readable --> Readable
|
||||||
// First Transform | Duplex, Last Writable --> Writable
|
// First Transform | Duplex, Last Writable --> Writable
|
||||||
//
|
//
|
||||||
@ -22,26 +20,23 @@ export function compose(
|
|||||||
): Duplex {
|
): Duplex {
|
||||||
// Maybe just return a new stream here
|
// Maybe just return a new stream here
|
||||||
if (streams.length < 2) {
|
if (streams.length < 2) {
|
||||||
throw new Error("Not enough");
|
throw new Error("At least two streams are required to compose");
|
||||||
}
|
}
|
||||||
|
|
||||||
const duplex = new Duplex({
|
|
||||||
objectMode: true,
|
|
||||||
write(chunk, enc, cb) {
|
|
||||||
const first = streams[0] as Writable;
|
const first = streams[0] as Writable;
|
||||||
|
const last = streams[streams.length - 1] as Readable;
|
||||||
|
const duplex = new Duplex({
|
||||||
|
...options,
|
||||||
|
write(chunk, enc, cb) {
|
||||||
if (!first.write(chunk)) {
|
if (!first.write(chunk)) {
|
||||||
first.on("drain", cb);
|
first.once("drain", cb);
|
||||||
} else {
|
} else {
|
||||||
cb();
|
cb();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
read(size) {
|
read(size) {
|
||||||
let chunk;
|
if (last.readable) {
|
||||||
while (
|
this.push(last.read(size));
|
||||||
null !==
|
|
||||||
(chunk = (streams[streams.length - 1] as Readable).read())
|
|
||||||
) {
|
|
||||||
this.push(chunk);
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
@ -297,9 +297,3 @@ export function compose(
|
|||||||
options,
|
options,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
export function composeDuplex(
|
|
||||||
streams: Array<Writable | Transform>,
|
|
||||||
options?: WritableOptions,
|
|
||||||
) {
|
|
||||||
return baseFunctions.composeDuplex(streams, options);
|
|
||||||
}
|
|
||||||
|
@ -22,5 +22,4 @@ export {
|
|||||||
accumulator,
|
accumulator,
|
||||||
accumulatorBy,
|
accumulatorBy,
|
||||||
compose,
|
compose,
|
||||||
composeDuplex,
|
|
||||||
} from "./functions";
|
} from "./functions";
|
||||||
|
@ -1,54 +1,68 @@
|
|||||||
const test = require("ava");
|
const test = require("ava");
|
||||||
|
const { expect } = require("chai");
|
||||||
const { compose, composeDuplex, map } = require("../src");
|
const { compose, composeDuplex, map } = require("../src");
|
||||||
|
|
||||||
test.cb("compose()", t => {
|
test.cb("compose() chains two streams together in the correct order", t => {
|
||||||
const first = map((chunk: number) => chunk + "x");
|
t.plan(3);
|
||||||
const second = map((chunk: number) => chunk + "y");
|
let i = 0;
|
||||||
|
const first = map((chunk: number) => chunk + 1);
|
||||||
|
const second = map((chunk: number) => chunk * 2);
|
||||||
|
|
||||||
const composed = compose(
|
const composed = compose(
|
||||||
[first, second],
|
[first, second],
|
||||||
{ objectMode: true },
|
{ objectMode: true },
|
||||||
);
|
);
|
||||||
const third = map((chunk: number) => chunk + "z");
|
|
||||||
composed
|
|
||||||
.pipe(third)
|
|
||||||
.on("data", data => console.log("Piped composed: ", data));
|
|
||||||
|
|
||||||
composed.on("data", data => {
|
composed.on("data", data => {
|
||||||
console.log("data on composed", data);
|
expect(data).to.equal(result[i]);
|
||||||
|
t.pass();
|
||||||
|
i++;
|
||||||
|
if (i === 3) {
|
||||||
|
t.end();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
composed.on("error", err => {
|
||||||
|
t.end(err);
|
||||||
|
});
|
||||||
|
composed.on("end", () => {
|
||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
composed.on("error", data => {
|
|
||||||
console.log("ERROR", data);
|
const input = [1, 2, 3];
|
||||||
});
|
const result = [4, 6, 8];
|
||||||
composed.on("end", data => {
|
|
||||||
console.log("end", data);
|
input.forEach(item => composed.write(item));
|
||||||
});
|
});
|
||||||
|
|
||||||
composed.write(1);
|
test.cb(
|
||||||
composed.write(2);
|
"compose() followed by pipe chains streams together in the correct order",
|
||||||
});
|
t => {
|
||||||
test.cb.only("composeDuplex()", t => {
|
t.plan(3);
|
||||||
const first = map((chunk: number) => chunk + "x");
|
let i = 0;
|
||||||
const second = map((chunk: number) => chunk + "y");
|
const first = map((chunk: number) => chunk + 1);
|
||||||
|
const second = map((chunk: number) => chunk * 2);
|
||||||
|
|
||||||
const composed = composeDuplex([first, second], { objectMode: true });
|
const composed = compose(
|
||||||
const third = map((chunk: number) => chunk + "z");
|
[first, second],
|
||||||
// composed
|
{ objectMode: true },
|
||||||
// .pipe(third)
|
);
|
||||||
// .on("data", data => console.log("Piped composed: ", data));
|
const third = map((chunk: number) => chunk + 1);
|
||||||
|
composed.pipe(third).on("data", data => {
|
||||||
composed.on("data", data => {
|
expect(data).to.equal(result[i]);
|
||||||
console.log("data on composed", data);
|
t.pass();
|
||||||
|
i++;
|
||||||
|
if (i === 3) {
|
||||||
t.end();
|
t.end();
|
||||||
});
|
}
|
||||||
composed.on("error", data => {
|
|
||||||
console.log("ERROR", data);
|
|
||||||
});
|
|
||||||
composed.on("end", data => {
|
|
||||||
console.log("end", data);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
composed.write(1);
|
composed.on("error", err => {
|
||||||
composed.write(2);
|
t.end(err);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const input = [1, 2, 3];
|
||||||
|
const result = [5, 7, 9];
|
||||||
|
|
||||||
|
input.forEach(item => composed.write(item));
|
||||||
|
},
|
||||||
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user