Skip to content
Myr

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

5 files+36 added−8 removed

modified:docs/transform.md+1 added-1 removed
modified:lib/stdio/transform.js+6 added-2 removed
47 unmodified lines
48 yield chunk;49 return;50 }5152 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 lines
103 yield chunk;104 return;105 }106107 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 lines
124 for (const finalChunk of final()) {125 yield * transformChunkSync(finalChunk, generators, index + 1);126 }127};128129const identityGenerator = function * (chunk) {130 yield chunk;131};
modified:lib/stdio/type.js+2 added-2 removed
37 unmodified lines
38 return 'native';39};4041const 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 }4546 if (final !== undefined && !isGenerator(final)) {
16 unmodified lines
63export const isAsyncGenerator = stdioOption => Object.prototype.toString.call(stdioOption) === '[object AsyncGeneratorFunction]';64const isSyncGenerator = stdioOption => Object.prototype.toString.call(stdioOption) === '[object GeneratorFunction]';65export const isGeneratorOptions = stdioOption => typeof stdioOption === 'object'66 && stdioOption !== null67 && stdioOption.transform !== undefined;67 && (stdioOption.transform !== undefined || stdioOption.final !== undefined);6869export const isUrl = stdioOption => Object.prototype.toString.call(stdioOption) === '[object URL]';70export const isRegularUrl = stdioOption => isUrl(stdioOption) && stdioOption.protocol !== 'file:';71
29 unmodified lines
modified:test/helpers/generator.js+9 added-3 removed
31 unmodified lines
32 },33 objectMode,34});3536export const identityGenerator = input => function * () {37 yield input;38};3940export const identityAsyncGenerator = input => async function * () {41 yield input;42};4344export const getOutputGenerator = (input, objectMode) => ({37 * transform() {38 yield input;39 },45 transform: identityGenerator(input),46 objectMode,47});4849export const outputObjectGenerator = getOutputGenerator(foobarObject, true);
45 unmodified lines
modified:test/stdio/transform.js+18 added
5 unmodified lines
6import {execa} from '../../index.js';7import {foobarString} from '../helpers/input.js';8import {9 noopGenerator,10 identityGenerator,11 identityAsyncGenerator,12 getOutputsGenerator,13 getOutputGenerator,14 infiniteGenerator,15 outputObjectGenerator,
14 unmodified lines
3031test('Generators "final" can be used', testGeneratorFinal, 'noop.js');32test('Generators "final" is used even on empty streams', testGeneratorFinal, 'empty.js');3334const testFinalAlone = async (t, final) => {35 const {stdout} = await execa('noop-fd.js', ['1', '.'], {stdout: {final: final(foobarString)}});36 t.is(stdout, `.${foobarString}`);37};3839test('Generators "final" can be used without "transform"', testFinalAlone, identityGenerator);40test('Generators "final" can be used without "transform", async', testFinalAlone, identityAsyncGenerator);4142const testFinalNoOutput = async (t, final) => {43 const {stdout} = await execa('empty.js', {stdout: {final: final(foobarString)}});44 t.is(stdout, foobarString);45};4647test('Generators "final" can be used without "transform" nor output', testFinalNoOutput, identityGenerator);48test('Generators "final" can be used without "transform" nor output, async', testFinalNoOutput, identityAsyncGenerator);4950const repeatCount = defaultHighWaterMark * 3;5152const writerGenerator = function * () {53 for (let index = 0; index < repeatCount; index += 1) {
165 unmodified lines