Skip to content
Myr

transform.md

This file

Path

docs/transform.md

128 lines · 3.9 KiB · Markdown

Last changed by

Simplify transforms syntax (#748)ehmickybefore adoptionfd00294

History of this file

Take it

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

lines 1–4why, one step back
1# Transforms
23## Summary4

Simplify transforms syntax (#748)ehmickybefore adoptionfd00294

line 5why, one step back
5Transforms map or filter the input or output of a child process. 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#stdin), [`stdout`](../readme.md#stdout-1), [`stderr`](../readme.md#stderr-1) or [`stdio`](../readme.md#stdio-1) option. It can be [`async`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function*).

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

lines 6–9why, one step back
6
7```js8import {execa} from 'execa';9

Simplify transforms syntax (#748)ehmickybefore adoptionfd00294

lines 10–12why, one step back
10const transform = function * (line) {
11 const prefix = line.includes('error') ? 'ERROR' : 'INFO';12 yield `${prefix}: ${line}`;

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

lines 13–20why, one step back
13};
1415const {stdout} = await execa('echo', ['hello'], {stdout: transform});16console.log(stdout); // HELLO17```1819## Encoding20

Simplify transforms syntax (#748)ehmickybefore adoptionfd00294

line 21why, one step back
21The `line` argument passed to the transform is a string. If the [`encoding`](../readme.md#encoding) option is `buffer`, it is an `Uint8Array` instead.

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

line 22why, one step back
22

Line-wise transforms (#699)ehmickybefore adoption6dd804f

line 23why, one step back
23The transform can `yield` either a `string` or an `Uint8Array`, regardless of the `lines` argument's type.

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

lines 24–26why, one step back
24
25## Filtering26

Line-wise transforms (#699)ehmickybefore adoption6dd804f

line 27why, one step back
27`yield` can be called 0, 1 or multiple times. Not calling `yield` enables filtering a specific line.

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

lines 28–31why, one step back
28
29```js30import {execa} from 'execa';31

Simplify transforms syntax (#748)ehmickybefore adoptionfd00294

lines 32–34why, one step back
32const transform = function * (line) {
33 if (!line.includes('secret')) {34 yield line;

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

lines 35–40why, one step back
35 }
36};3738const {stdout} = await execa('echo', ['This is a secret.'], {stdout: transform});39console.log(stdout); // ''40```

Line-wise transforms (#699)ehmickybefore adoption6dd804f

lines 41–53why, one step back
41
42## Binary data4344The transform iterates over lines by default.\45However, if a `{transform, binary: true}` plain object is passed, it iterates over arbitrary chunks of data instead.4647```js48await execa('./binary.js', {stdout: {transform, binary: true}});49```5051This is more efficient and recommended if the data is either:52 - Binary: Which does not have lines.53 - Text: But the transform works even if a line or word is split across multiple chunks.

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

line 54why, one step back
54

Line-wise standard streams (#741)ehmickybefore adoption479d12e

lines 55–56why, one step back
55Please note the [`lines`](../readme.md#lines) option is unrelated: it has no impact on transforms.
56

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 57–58why, one step back
57## Object mode
58

Document returning `null` in transforms (#740)ehmickybefore adoptiondd9c884

line 59why, one step back
59By default, `stdout` and `stderr`'s transforms must return a string or an `Uint8Array`. However, if a `{transform, objectMode: true}` plain object is passed, any type can be returned instead, except `null`. The process' [`stdout`](../readme.md#stdout)/[`stderr`](../readme.md#stderr) will be an array of values.

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 60–61why, one step back
60
61```js

Simplify transforms syntax (#748)ehmickybefore adoptionfd00294

lines 62–64why, one step back
62const transform = function * (line) {
63 yield JSON.parse(line);64};

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 65–67why, one step back
65
66const {stdout} = await execa('./jsonlines-output.js', {stdout: {transform, objectMode: true}});67for (const data of stdout) {

Simplify transforms syntax (#748)ehmickybefore adoptionfd00294

line 68why, one step back
68 console.log(stdout); // {...}

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 69–74why, one step back
69}
70```7172`stdin` can also use `objectMode: true`.7374```js

Simplify transforms syntax (#748)ehmickybefore adoptionfd00294

lines 75–77why, one step back
75const transform = function * (line) {
76 yield JSON.stringify(line);77};

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

line 78why, one step back
78

Simplify transforms syntax (#748)ehmickybefore adoptionfd00294

line 79why, one step back
79const input = [{event: 'example'}, {event: 'otherExample'}];

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

line 80why, one step back
80await execa('./jsonlines-input.js', {stdin: [input, {transform, objectMode: true}]});

Simplify transforms syntax (#748)ehmickybefore adoptionfd00294

lines 81–113why, one step back
81```
8283## Sharing state8485State can be shared between calls of the `transform` and [`final`](#finalizing) functions.8687```js88let count = 08990// Prefix line number91const transform = function * (line) {92 yield `[${count++}] ${line}`;93};94```9596## Finalizing9798To create additional lines after the last one, a `final` generator function can be used by passing a `{transform, final}` plain object.99100```js101let count = 0;102103const transform = function * (line) {104 count += 1;105 yield line;106};107108const final = function * () {109 yield `Number of lines: ${count}`;110};111112const {stdout} = await execa('./command.js', {stdout: {transform, final}});113console.log(stdout); // Ends with: 'Number of lines: 54'

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 114–115why, one step back
114```
115

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

lines 116–128why, one step back
116## Combining
117118The [`stdin`](../readme.md#stdin), [`stdout`](../readme.md#stdout-1), [`stderr`](../readme.md#stderr-1) and [`stdio`](../readme.md#stdio-1) options can accept an array of values. While this is not specific to transforms, this can be useful with them too. For example, the following transform impacts the value printed by `inherit`.119120```js121await execa('echo', ['hello'], {stdout: [transform, 'inherit']});122```123124This also allows using multiple transforms.125126```js127await execa('echo', ['hello'], {stdout: [transform, otherTransform]});128```