Skip to content
Myr

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

2 files+83 added−10 removed

modified:docs/transform.md+59 added-10 removed
1# Transforms23## Summary45Transforms 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*).5Transforms 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*).67```js8import {execa} from 'execa';9
8 unmodified lines
1819## Encoding2021The `line` argument passed to the transform is a string by default.\22However, 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.22However, if the [`binary`](#transformoptionsbinary) transform option is `true` or if the [`encoding`](../readme.md#optionsencoding) subprocess option is binary, it is an `Uint8Array` instead.2324The transform can `yield` either a `string` or an `Uint8Array`, regardless of the `line` argument's type.2526## Filtering
15 unmodified lines
4243## Binary data4445The transform iterates over lines by default.\46However, 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.46However, 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.4748```js49await execa('./binary.js', {stdout: {transform, binary: true}});50```
3 unmodified lines
54- Text: But the transform works even if a line or word is split across multiple chunks.5556## Newlines5758Unless [`{transform, binary: true}`](#binary-data) is used, the transform iterates over lines.58Unless the [`binary`](#transformoptionsbinary) transform option is `true`, the transform iterates over lines.59By default, newlines are stripped from each `line` argument.6061```js62// `line`'s value never ends with '\n'.
1 unmodified line
6465await execa('./run.js', {stdout: transform});66```6768However, if a `{transform, preserveNewlines: true}` plain object is passed, newlines are kept.68However, if the [`preserveNewlines`](#transformoptionspreservenewlines) transform option is `true`, newlines are kept.6970```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 lines
9697await execa('./run.js', {stdout: transform});98```99100However, if a `{transform, preserveNewlines: true}` plain object is passed, multiple `yield`s produce a single line instead.100However, if the [`preserveNewlines`](#transformoptionspreservenewlines) transform option is `true`, multiple `yield`s produce a single line instead.101102```js103const transform = function * (line) {104 yield 'Important note: ';
10 unmodified lines
115116## Object mode117118By default, `stdout` and `stderr`'s transforms must return a string or an `Uint8Array`.\119However, 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.119However, 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.120121```js122const transform = function * (line) {123 yield JSON.parse(line);
4 unmodified lines
128 console.log(stdout); // {...}129}130```131132`stdin` can also use `objectMode: true`.132[`stdin`](../readme.md#optionsstdin) can also use `objectMode: true`.133134```js135const transform = function * (line) {136 yield JSON.stringify(line);
4 unmodified lines
141```142143## Sharing state144145State can be shared between calls of the `transform` and [`final`](#finalizing) functions.145State can be shared between calls of the [`transform`](#transformoptionstransform) and [`final`](#transformoptionsfinal) functions.146147```js148let count = 0149
4 unmodified lines
154```155156## Finalizing157158To create additional lines after the last one, a `final` generator function can be used by passing a `{final}` or `{transform, final}` plain object.158To create additional lines after the last one, a [`final`](#transformoptionsfinal) generator function can be used.159160```js161let count = 0;162
64 unmodified lines
227 const prefix = line.includes('error') ? 'ERROR' : 'INFO';228 console.log(`${prefix}: ${line}`);229}230```231232## Transform options233234A 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.235236A transform is either a [generator function](#transformoptionstransform) or a plain object with the following members.237238### transformOptions.transform239240Type: `GeneratorFunction<string | Uint8Array | unknown>` | `AsyncGeneratorFunction<string | Uint8Array | unknown>`241242Map or [filter](#filtering) the input or output of the subprocess.243244More info [here](#summary) and [there](#sharing-state).245246### transformOptions.final247248Type: `GeneratorFunction<string | Uint8Array | unknown>` | `AsyncGeneratorFunction<string | Uint8Array | unknown>`249250Create additional lines after the last one.251252[More info.](#finalizing)253254### transformOptions.binary255256Type: `boolean`\257Default: `false`258259If `true`, iterate over arbitrary chunks of `Uint8Array`s instead of line `string`s.260261More info [here](#encoding) and [there](#binary-data).262263### transformOptions.preserveNewlines264265Type: `boolean`\266Default: `false`267268If `true`, keep newlines in each `line` argument. Also, this allows multiple `yield`s to produces a single line.269270[More info.](#newlines)271272### transformOptions.objectMode273274Type: `boolean`\275Default: `false`276277If `true`, allow [`transformOptions.transform`](#transformoptionstransform) and [`transformOptions.final`](#transformoptionsfinal) to return any type, not just `string` or `Uint8Array`.278279[More info.](#object-mode)
modified:index.d.ts+24 added
42 unmodified lines
43type GeneratorFinal<IsSync extends boolean> = () =>44| Unless<IsSync, AsyncGenerator<unknown, void, void>>45| Generator<unknown, void, void>;4647/**48A transform or an array of transforms can be passed to the `stdin`, `stdout`, `stderr` or `stdio` option.4950A transform is either a generator function or a plain object with the following members.51*/52type GeneratorTransformFull<IsSync extends boolean> = {53 /**54 Map or filter the input or output of the subprocess.55 */56 transform: GeneratorTransform<IsSync>;5758 /**59 Create additional lines after the last one.60 */61 final?: GeneratorFinal<IsSync>;6263 /**64 If `true`, iterate over arbitrary chunks of `Uint8Array`s instead of line `string`s.65 */66 binary?: boolean;6768 /**69 If `true`, keep newlines in each `line` argument. Also, this allows multiple `yield`s to produces a single line.70 */71 preserveNewlines?: boolean;7273 /**74 If `true`, allow `transformOptions.transform` and `transformOptions.final` to return any type, not just `string` or `Uint8Array`.75 */76 objectMode?: boolean;77};7879type DuplexTransform = {
1671 unmodified lines