Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: add esm examples to node:readline #55335

Merged
merged 3 commits into from
Dec 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 127 additions & 20 deletions doc/api/readline.md
Original file line number Diff line number Diff line change
Expand Up @@ -703,9 +703,18 @@ added: v17.0.0
The `readlinePromises.createInterface()` method creates a new `readlinePromises.Interface`
instance.

```js
const readlinePromises = require('node:readline/promises');
const rl = readlinePromises.createInterface({
```mjs
import { createInterface } from 'node:readline/promises';
import { stdin, stdout } from 'node:process';
const rl = createInterface({
input: stdin,
output: stdout,
});
```

```cjs
const { createInterface } = require('node:readline/promises');
const rl = createInterface({
input: process.stdin,
output: process.stdout,
});
Expand Down Expand Up @@ -960,9 +969,18 @@ changes:
The `readline.createInterface()` method creates a new `readline.Interface`
instance.

```js
const readline = require('node:readline');
const rl = readline.createInterface({
```mjs
import { createInterface } from 'node:readline';
import { stdin, stdout } from 'node:process';
const rl = createInterface({
input: stdin,
output: stdout,
});
```

```cjs
const { createInterface } = require('node:readline');
const rl = createInterface({
input: process.stdin,
output: process.stdout,
});
Expand Down Expand Up @@ -1098,9 +1116,36 @@ if (process.stdin.isTTY)
The following example illustrates the use of `readline.Interface` class to
implement a small command-line interface:

```js
const readline = require('node:readline');
const rl = readline.createInterface({
```mjs
import { createInterface } from 'node:readline';
import { exit, stdin, stdout } from 'node:process';
const rl = createInterface({
input: stdin,
output: stdout,
prompt: 'OHAI> ',
});

rl.prompt();

rl.on('line', (line) => {
switch (line.trim()) {
case 'hello':
console.log('world!');
break;
default:
console.log(`Say what? I might have heard '${line.trim()}'`);
break;
}
rl.prompt();
}).on('close', () => {
console.log('Have a great day!');
exit(0);
});
```

```cjs
const { createInterface } = require('node:readline');
const rl = createInterface({
input: process.stdin,
output: process.stdout,
prompt: 'OHAI> ',
Expand Down Expand Up @@ -1130,14 +1175,37 @@ A common use case for `readline` is to consume an input file one line at a
time. The easiest way to do so is leveraging the [`fs.ReadStream`][] API as
well as a `for await...of` loop:

```js
const fs = require('node:fs');
const readline = require('node:readline');
```mjs
import { createReadStream } from 'node:fs';
import { createInterface } from 'node:readline';

async function processLineByLine() {
const fileStream = fs.createReadStream('input.txt');
const fileStream = createReadStream('input.txt');

const rl = readline.createInterface({
const rl = createInterface({
input: fileStream,
crlfDelay: Infinity,
});
// Note: we use the crlfDelay option to recognize all instances of CR LF
// ('\r\n') in input.txt as a single line break.

for await (const line of rl) {
// Each line in input.txt will be successively available here as `line`.
console.log(`Line from file: ${line}`);
}
}

processLineByLine();
```

```cjs
const { createReadStream } = require('node:fs');
const { createInterface } = require('node:readline');

async function processLineByLine() {
const fileStream = createReadStream('input.txt');

const rl = createInterface({
input: fileStream,
crlfDelay: Infinity,
});
Expand All @@ -1155,12 +1223,26 @@ processLineByLine();

Alternatively, one could use the [`'line'`][] event:

```js
const fs = require('node:fs');
const readline = require('node:readline');
```mjs
import { createReadStream } from 'node:fs';
import { createInterface } from 'node:readline';

const rl = readline.createInterface({
input: fs.createReadStream('sample.txt'),
const rl = createInterface({
input: createReadStream('sample.txt'),
crlfDelay: Infinity,
});

rl.on('line', (line) => {
console.log(`Line from file: ${line}`);
});
```

```cjs
const { createReadStream } = require('node:fs');
const { createInterface } = require('node:readline');

const rl = createInterface({
input: createReadStream('sample.txt'),
crlfDelay: Infinity,
});

Expand All @@ -1172,7 +1254,32 @@ rl.on('line', (line) => {
Currently, `for await...of` loop can be a bit slower. If `async` / `await`
flow and speed are both essential, a mixed approach can be applied:

```js
```mjs
import { once } from 'node:events';
import { createReadStream } from 'node:fs';
import { createInterface } from 'node:readline';

(async function processLineByLine() {
try {
const rl = createInterface({
input: createReadStream('big-file.txt'),
crlfDelay: Infinity,
});

rl.on('line', (line) => {
// Process the line.
});

await once(rl, 'close');

console.log('File processed.');
} catch (err) {
console.error(err);
}
})();
```

```cjs
const { once } = require('node:events');
const { createReadStream } = require('node:fs');
const { createInterface } = require('node:readline');
Expand Down
Loading