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 1 commit
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
119 changes: 113 additions & 6 deletions doc/api/readline.md
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,16 @@ added: v17.0.0
The `readlinePromises.createInterface()` method creates a new `readlinePromises.Interface`
instance.

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

```cjs
const readlinePromises = require('node:readline/promises');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you use destructuring in the ESM, it should probably also be used here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok! I generally try not to touch the examples that are already there, but if you think that'd be better I'll do it!

Should I do it for that example in particular or for the rest of the examples as well?

Thanks for the feedback! 🙌

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO all of them

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

You were right, now it looks more concise with the first example as well.

Thank you! 😊

const rl = readlinePromises.createInterface({
input: process.stdin,
Expand Down Expand Up @@ -960,7 +969,16 @@ changes:
The `readline.createInterface()` method creates a new `readline.Interface`
instance.

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

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

```js
```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 readline = require('node:readline');
const rl = readline.createInterface({
input: process.stdin,
Expand Down Expand Up @@ -1130,7 +1175,30 @@ 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
```mjs
import { createReadStream } from 'node:fs';
import { createInterface } from 'node:readline';

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

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 fs = require('node:fs');
const readline = require('node:readline');

Expand All @@ -1155,7 +1223,21 @@ processLineByLine();

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

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

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

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

```cjs
const fs = require('node:fs');
const readline = require('node:readline');

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