An easy interactive way to use Readable/Writable streams (I/O streams) with yield in a generator function.
npm install github:accup/node-yieldin
or
npm install accup/node-yieldin
will be available.
The keyword yield works as a keyword of interactive I/O.
maingenerator can obtain a parameter which is a function to write chunk to stream.yieldreturnsnullwhen theistreamhas ended.
var yieldin = require('yieldin');
/**
* any I/O streams
*/
var readable, writable;
/**
* The first argument should be readable stream.
* The second argument should be writable stream.
* The third argument should be generator function (function *).
*/
yieldin.run(readable, writable, function * (write) {
// Write "xxx" to writable stream.
write("xxx");
// Read a chunk from readable stream.
var chunk = yield;
// --------------------------------------
// Next statement is the same as previous statements.
var chunk = yield "xxx";
});yieldin.stdrun(main) is similar with yieldin.run(stdin, stdout, main), but, in the yieldin.stdrun,
- always call
stdin.setEncoding('utf8'). yieldreturns a word (for example, when getting chunk "a b c",yieldfirst returns "a" and secondly returns "b", ...).writefunction has 2 parameters. The second parameter is line-terminator (like '\r\n') (default isos.EOL).
var yieldin = require('yieldin');
yieldin.stdrun(function * (write) {
// Question and Answer
// For no line-terminator writing, assigning "" to the second param.
write("What's your favorite food? ", "");
var food = yield;
write(`Oh, '${food}', that's nice!`);
// Shorthand of Q&A
// In contrast, `yield` writing has no line-terminator.
var food = yield "What's your favorite food? ";
write(`Oh, '${food}', that's nice!`);
// Very-shorthand of Q&A
write(`Oh, '${yield "What's your favorite food? "}', that's nice!`);
});
// [result (maybe)]
// What's your favorite food? sushi
// Oh, 'sushi', that's nice!
// What's your favorite food? 寿司
// Oh, '寿司', that's nice!
// What's your favorite food? 🍣
// Oh, '🍣', that's nice!- Restore this repository 2018/05/29
- Ver1.0.2
- Update
yieldoperation.yieldreturnsnullwhen aistream(readable stream) has ended. - Fix a small bug. The older version of
stdrun(main)referedrun(istream, ostream, main)asthis.run, then ifrunwas assigned to another variable,thisdidn't refersyieldinandrunthrew an error.
- Update
- Ver1.0.1
- The second parameter of
write(chunk, end)instdrunsupported, thenrun(stdin, stdout, main)&stdrun(main)became different.
- The second parameter of
- Ver1.0.0
- Both
run(stdin, stdout, main)&stdrun(main)are the same operation.
- Both
- This is a repository to consider making an npm package.