Allow using a `final` function without a `transform` (#916)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 666f7f9 · Browse files · 5 files·+36 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+36 added−8 removed
- tests:+27 added−3 removed
- other:+9 added−5 removed
Provenance
- Commit
f8af153- Tree
339d20a- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
666f7f9
5 files changed+36 added−8 removed
modified:docs/transform.md+1 added-1 removed
93 unmodified lines9495969798989910010110226 unmodified lines93 unmodified lines```
## Finalizing
To create additional lines after the last one, a `final` generator function can be used by passing a `{transform, final}` plain object.To create additional lines after the last one, a `final` generator function can be used by passing a `{final}` or `{transform, final}` plain object.
```jslet count = 0;
26 unmodified linesmodified:lib/stdio/transform.js+6 added-2 removed
47 unmodified lines48 yield chunk;49 return;50 }51 52- const {transform} = generators[index];52+ const {transform = identityGenerator} = generators[index];53 for await (const transformedChunk of transform(chunk)) {54 yield * transformChunk(transformedChunk, generators, index + 1);55 }56 };46 unmodified lines103 yield chunk;104 return;105 }106 107- const {transform} = generators[index];107+ const {transform = identityGenerator} = generators[index];108 for (const transformedChunk of transform(chunk)) {109 yield * transformChunkSync(transformedChunk, generators, index + 1);110 }111 };12 unmodified lines124 for (const finalChunk of final()) {125 yield * transformChunkSync(finalChunk, generators, index + 1);126 }127 };128+129+const identityGenerator = function * (chunk) {130+ yield chunk;131+};modified:lib/stdio/type.js+2 added-2 removed
37 unmodified lines38 return 'native';39 };40 41 const getGeneratorObjectType = ({transform, final, binary, objectMode}, optionName) => {42- if (!isGenerator(transform)) {42+ if (transform !== undefined && !isGenerator(transform)) {43 throw new TypeError(`The \`${optionName}.transform\` option must be a generator.`);44 }45 46 if (final !== undefined && !isGenerator(final)) {16 unmodified lines63 export const isAsyncGenerator = stdioOption => Object.prototype.toString.call(stdioOption) === '[object AsyncGeneratorFunction]';64 const isSyncGenerator = stdioOption => Object.prototype.toString.call(stdioOption) === '[object GeneratorFunction]';65 export const isGeneratorOptions = stdioOption => typeof stdioOption === 'object'66 && stdioOption !== null67- && stdioOption.transform !== undefined;67+ && (stdioOption.transform !== undefined || stdioOption.final !== undefined);68 69 export const isUrl = stdioOption => Object.prototype.toString.call(stdioOption) === '[object URL]';70 export const isRegularUrl = stdioOption => isUrl(stdioOption) && stdioOption.protocol !== 'file:';71 29 unmodified linesmodified:test/helpers/generator.js+9 added-3 removed
31 unmodified lines32 },33 objectMode,34 });35 36+export const identityGenerator = input => function * () {37+ yield input;38+};39+40+export const identityAsyncGenerator = input => async function * () {41+ yield input;42+};43+44 export const getOutputGenerator = (input, objectMode) => ({37- * transform() {38- yield input;39- },45+ transform: identityGenerator(input),46 objectMode,47 });48 49 export const outputObjectGenerator = getOutputGenerator(foobarObject, true);45 unmodified linesmodified:test/stdio/transform.js+18 added
5 unmodified lines6 import {execa} from '../../index.js';7 import {foobarString} from '../helpers/input.js';8 import {9 noopGenerator,10+ identityGenerator,11+ identityAsyncGenerator,12 getOutputsGenerator,13 getOutputGenerator,14 infiniteGenerator,15 outputObjectGenerator,14 unmodified lines30 31 test('Generators "final" can be used', testGeneratorFinal, 'noop.js');32 test('Generators "final" is used even on empty streams', testGeneratorFinal, 'empty.js');33 34+const testFinalAlone = async (t, final) => {35+ const {stdout} = await execa('noop-fd.js', ['1', '.'], {stdout: {final: final(foobarString)}});36+ t.is(stdout, `.${foobarString}`);37+};38+39+test('Generators "final" can be used without "transform"', testFinalAlone, identityGenerator);40+test('Generators "final" can be used without "transform", async', testFinalAlone, identityAsyncGenerator);41+42+const testFinalNoOutput = async (t, final) => {43+ const {stdout} = await execa('empty.js', {stdout: {final: final(foobarString)}});44+ t.is(stdout, foobarString);45+};46+47+test('Generators "final" can be used without "transform" nor output', testFinalNoOutput, identityGenerator);48+test('Generators "final" can be used without "transform" nor output, async', testFinalNoOutput, identityAsyncGenerator);49+50 const repeatCount = defaultHighWaterMark * 3;51 52 const writerGenerator = function * () {53 for (let index = 0; index < repeatCount; index += 1) {165 unmodified lines