Improve documentation of transform options (#969)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent a5dcf42 · Browse files · 2 files·+83 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+83 added−10 removed
- other:+83 added−10 removed
Provenance
- Commit
1e9e47e- Tree
2e5a81d- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
a5dcf42
2 files changed+83 added−10 removed
modified:docs/transform.md+59 added-10 removed
1 # Transforms2 3 ## Summary4 5-Transforms map or filter the input or output of a subprocess. They are defined by passing a [generator function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*) to the [`stdin`](../readme.md#optionsstdin), [`stdout`](../readme.md#optionsstdout), [`stderr`](../readme.md#optionsstderr) or [`stdio`](../readme.md#optionsstdio) option. It can be [`async`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function*).5+Transforms map or filter the input or output of a subprocess. They are defined by passing a [generator function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*) or a [transform options object](#transform-options) to the [`stdin`](../readme.md#optionsstdin), [`stdout`](../readme.md#optionsstdout), [`stderr`](../readme.md#optionsstderr) or [`stdio`](../readme.md#optionsstdio) option. It can be [`async`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function*).6 7 ```js8 import {execa} from 'execa';9 8 unmodified lines18 19 ## Encoding20 21 The `line` argument passed to the transform is a string by default.\22-However, if either a `{transform, binary: true}` plain object is passed, or if the [`encoding`](../readme.md#optionsencoding) option is binary, it is an `Uint8Array` instead.22+However, if the [`binary`](#transformoptionsbinary) transform option is `true` or if the [`encoding`](../readme.md#optionsencoding) subprocess option is binary, it is an `Uint8Array` instead.23 24 The transform can `yield` either a `string` or an `Uint8Array`, regardless of the `line` argument's type.25 26 ## Filtering15 unmodified lines42 43 ## Binary data44 45 The transform iterates over lines by default.\46-However, if either a `{transform, binary: true}` plain object is passed, or if the [`encoding`](../readme.md#optionsencoding) option is binary, it iterates over arbitrary chunks of data instead.46+However, if the [`binary`](#transformoptionsbinary) transform option is `true` or if the [`encoding`](../readme.md#optionsencoding) subprocess option is binary, it iterates over arbitrary chunks of data instead.47 48 ```js49 await execa('./binary.js', {stdout: {transform, binary: true}});50 ```3 unmodified lines54 - Text: But the transform works even if a line or word is split across multiple chunks.55 56 ## Newlines57 58-Unless [`{transform, binary: true}`](#binary-data) is used, the transform iterates over lines.58+Unless the [`binary`](#transformoptionsbinary) transform option is `true`, the transform iterates over lines.59 By default, newlines are stripped from each `line` argument.60 61 ```js62 // `line`'s value never ends with '\n'.1 unmodified line64 65 await execa('./run.js', {stdout: transform});66 ```67 68-However, if a `{transform, preserveNewlines: true}` plain object is passed, newlines are kept.68+However, if the [`preserveNewlines`](#transformoptionspreservenewlines) transform option is `true`, newlines are kept.69 70 ```js71 // `line`'s value ends with '\n'.72 // The output's last `line` might or might not end with '\n', depending on the output.23 unmodified lines96 97 await execa('./run.js', {stdout: transform});98 ```99 100-However, if a `{transform, preserveNewlines: true}` plain object is passed, multiple `yield`s produce a single line instead.100+However, if the [`preserveNewlines`](#transformoptionspreservenewlines) transform option is `true`, multiple `yield`s produce a single line instead.101 102 ```js103 const transform = function * (line) {104 yield 'Important note: ';10 unmodified lines115 116 ## Object mode117 118 By default, `stdout` and `stderr`'s transforms must return a string or an `Uint8Array`.\119-However, if a `{transform, objectMode: true}` plain object is passed, any type can be returned instead, except `null` or `undefined`. The subprocess' [`stdout`](../readme.md#resultstdout)/[`stderr`](../readme.md#resultstderr) will be an array of values.119+However, if the [`objectMode`](#transformoptionsobjectmode) transform option is `true`, any type can be returned instead, except `null` or `undefined`. The subprocess' [`result.stdout`](../readme.md#resultstdout)/[`result.stderr`](../readme.md#resultstderr) will be an array of values.120 121 ```js122 const transform = function * (line) {123 yield JSON.parse(line);4 unmodified lines128 console.log(stdout); // {...}129 }130 ```131 132-`stdin` can also use `objectMode: true`.132+[`stdin`](../readme.md#optionsstdin) can also use `objectMode: true`.133 134 ```js135 const transform = function * (line) {136 yield JSON.stringify(line);4 unmodified lines141 ```142 143 ## Sharing state144 145-State can be shared between calls of the `transform` and [`final`](#finalizing) functions.145+State can be shared between calls of the [`transform`](#transformoptionstransform) and [`final`](#transformoptionsfinal) functions.146 147 ```js148 let count = 0149 4 unmodified lines154 ```155 156 ## Finalizing157 158-To create additional lines after the last one, a `final` generator function can be used by passing a `{final}` or `{transform, final}` plain object.158+To create additional lines after the last one, a [`final`](#transformoptionsfinal) generator function can be used.159 160 ```js161 let count = 0;162 64 unmodified lines227 const prefix = line.includes('error') ? 'ERROR' : 'INFO';228 console.log(`${prefix}: ${line}`);229 }230 ```231+232+## Transform options233+234+A transform or an [array of transforms](#combining) can be passed to the [`stdin`](../readme.md#optionsstdin), [`stdout`](../readme.md#optionsstdout), [`stderr`](../readme.md#optionsstderr) or [`stdio`](../readme.md#optionsstdio) option.235+236+A transform is either a [generator function](#transformoptionstransform) or a plain object with the following members.237+238+### transformOptions.transform239+240+Type: `GeneratorFunction<string | Uint8Array | unknown>` | `AsyncGeneratorFunction<string | Uint8Array | unknown>`241+242+Map or [filter](#filtering) the input or output of the subprocess.243+244+More info [here](#summary) and [there](#sharing-state).245+246+### transformOptions.final247+248+Type: `GeneratorFunction<string | Uint8Array | unknown>` | `AsyncGeneratorFunction<string | Uint8Array | unknown>`249+250+Create additional lines after the last one.251+252+[More info.](#finalizing)253+254+### transformOptions.binary255+256+Type: `boolean`\257+Default: `false`258+259+If `true`, iterate over arbitrary chunks of `Uint8Array`s instead of line `string`s.260+261+More info [here](#encoding) and [there](#binary-data).262+263+### transformOptions.preserveNewlines264+265+Type: `boolean`\266+Default: `false`267+268+If `true`, keep newlines in each `line` argument. Also, this allows multiple `yield`s to produces a single line.269+270+[More info.](#newlines)271+272+### transformOptions.objectMode273+274+Type: `boolean`\275+Default: `false`276+277+If `true`, allow [`transformOptions.transform`](#transformoptionstransform) and [`transformOptions.final`](#transformoptionsfinal) to return any type, not just `string` or `Uint8Array`.278+279+[More info.](#object-mode)modified:index.d.ts+24 added
42 unmodified lines43 type GeneratorFinal<IsSync extends boolean> = () =>44 | Unless<IsSync, AsyncGenerator<unknown, void, void>>45 | Generator<unknown, void, void>;46 47+/**48+A transform or an array of transforms can be passed to the `stdin`, `stdout`, `stderr` or `stdio` option.49+50+A transform is either a generator function or a plain object with the following members.51+*/52 type GeneratorTransformFull<IsSync extends boolean> = {53+ /**54+ Map or filter the input or output of the subprocess.55+ */56 transform: GeneratorTransform<IsSync>;57+58+ /**59+ Create additional lines after the last one.60+ */61 final?: GeneratorFinal<IsSync>;62+63+ /**64+ If `true`, iterate over arbitrary chunks of `Uint8Array`s instead of line `string`s.65+ */66 binary?: boolean;67+68+ /**69+ If `true`, keep newlines in each `line` argument. Also, this allows multiple `yield`s to produces a single line.70+ */71 preserveNewlines?: boolean;72+73+ /**74+ If `true`, allow `transformOptions.transform` and `transformOptions.final` to return any type, not just `string` or `Uint8Array`.75+ */76 objectMode?: boolean;77 };78 79 type DuplexTransform = {1671 unmodified lines