mirror of
https://github.com/nodejs/node.git
synced 2025-12-28 07:50:41 +00:00
doc: add esm examples to node:readline
PR-URL: https://github.com/nodejs/node/pull/55335 Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
parent
e7a397a773
commit
a7c9f85f2f
@ -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,
|
||||
});
|
||||
@ -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,
|
||||
});
|
||||
@ -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> ',
|
||||
@ -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,
|
||||
});
|
||||
@ -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,
|
||||
});
|
||||
|
||||
@ -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');
|
||||
|
||||
Loading…
Reference in New Issue
Block a user