Skip to content

Commit d6a84cf

Browse files
mfdebianruyadorno
authored andcommitted
doc: add esm examples to node:readline
PR-URL: #55335 Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 4a1b51b commit d6a84cf

File tree

1 file changed

+127
-20
lines changed

1 file changed

+127
-20
lines changed

doc/api/readline.md

Lines changed: 127 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -703,9 +703,18 @@ added: v17.0.0
703703
The `readlinePromises.createInterface()` method creates a new `readlinePromises.Interface`
704704
instance.
705705

706-
```js
707-
const readlinePromises = require('node:readline/promises');
708-
const rl = readlinePromises.createInterface({
706+
```mjs
707+
import { createInterface } from 'node:readline/promises';
708+
import { stdin, stdout } from 'node:process';
709+
const rl = createInterface({
710+
input: stdin,
711+
output: stdout,
712+
});
713+
```
714+
715+
```cjs
716+
const { createInterface } = require('node:readline/promises');
717+
const rl = createInterface({
709718
input: process.stdin,
710719
output: process.stdout,
711720
});
@@ -960,9 +969,18 @@ changes:
960969
The `readline.createInterface()` method creates a new `readline.Interface`
961970
instance.
962971

963-
```js
964-
const readline = require('node:readline');
965-
const rl = readline.createInterface({
972+
```mjs
973+
import { createInterface } from 'node:readline';
974+
import { stdin, stdout } from 'node:process';
975+
const rl = createInterface({
976+
input: stdin,
977+
output: stdout,
978+
});
979+
```
980+
981+
```cjs
982+
const { createInterface } = require('node:readline');
983+
const rl = createInterface({
966984
input: process.stdin,
967985
output: process.stdout,
968986
});
@@ -1098,9 +1116,36 @@ if (process.stdin.isTTY)
10981116
The following example illustrates the use of `readline.Interface` class to
10991117
implement a small command-line interface:
11001118

1101-
```js
1102-
const readline = require('node:readline');
1103-
const rl = readline.createInterface({
1119+
```mjs
1120+
import { createInterface } from 'node:readline';
1121+
import { exit, stdin, stdout } from 'node:process';
1122+
const rl = createInterface({
1123+
input: stdin,
1124+
output: stdout,
1125+
prompt: 'OHAI> ',
1126+
});
1127+
1128+
rl.prompt();
1129+
1130+
rl.on('line', (line) => {
1131+
switch (line.trim()) {
1132+
case 'hello':
1133+
console.log('world!');
1134+
break;
1135+
default:
1136+
console.log(`Say what? I might have heard '${line.trim()}'`);
1137+
break;
1138+
}
1139+
rl.prompt();
1140+
}).on('close', () => {
1141+
console.log('Have a great day!');
1142+
exit(0);
1143+
});
1144+
```
1145+
1146+
```cjs
1147+
const { createInterface } = require('node:readline');
1148+
const rl = createInterface({
11041149
input: process.stdin,
11051150
output: process.stdout,
11061151
prompt: 'OHAI> ',
@@ -1130,14 +1175,37 @@ A common use case for `readline` is to consume an input file one line at a
11301175
time. The easiest way to do so is leveraging the [`fs.ReadStream`][] API as
11311176
well as a `for await...of` loop:
11321177

1133-
```js
1134-
const fs = require('node:fs');
1135-
const readline = require('node:readline');
1178+
```mjs
1179+
import { createReadStream } from 'node:fs';
1180+
import { createInterface } from 'node:readline';
11361181

11371182
async function processLineByLine() {
1138-
const fileStream = fs.createReadStream('input.txt');
1183+
const fileStream = createReadStream('input.txt');
11391184

1140-
const rl = readline.createInterface({
1185+
const rl = createInterface({
1186+
input: fileStream,
1187+
crlfDelay: Infinity,
1188+
});
1189+
// Note: we use the crlfDelay option to recognize all instances of CR LF
1190+
// ('\r\n') in input.txt as a single line break.
1191+
1192+
for await (const line of rl) {
1193+
// Each line in input.txt will be successively available here as `line`.
1194+
console.log(`Line from file: ${line}`);
1195+
}
1196+
}
1197+
1198+
processLineByLine();
1199+
```
1200+
1201+
```cjs
1202+
const { createReadStream } = require('node:fs');
1203+
const { createInterface } = require('node:readline');
1204+
1205+
async function processLineByLine() {
1206+
const fileStream = createReadStream('input.txt');
1207+
1208+
const rl = createInterface({
11411209
input: fileStream,
11421210
crlfDelay: Infinity,
11431211
});
@@ -1155,12 +1223,26 @@ processLineByLine();
11551223

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

1158-
```js
1159-
const fs = require('node:fs');
1160-
const readline = require('node:readline');
1226+
```mjs
1227+
import { createReadStream } from 'node:fs';
1228+
import { createInterface } from 'node:readline';
11611229

1162-
const rl = readline.createInterface({
1163-
input: fs.createReadStream('sample.txt'),
1230+
const rl = createInterface({
1231+
input: createReadStream('sample.txt'),
1232+
crlfDelay: Infinity,
1233+
});
1234+
1235+
rl.on('line', (line) => {
1236+
console.log(`Line from file: ${line}`);
1237+
});
1238+
```
1239+
1240+
```cjs
1241+
const { createReadStream } = require('node:fs');
1242+
const { createInterface } = require('node:readline');
1243+
1244+
const rl = createInterface({
1245+
input: createReadStream('sample.txt'),
11641246
crlfDelay: Infinity,
11651247
});
11661248

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

1175-
```js
1257+
```mjs
1258+
import { once } from 'node:events';
1259+
import { createReadStream } from 'node:fs';
1260+
import { createInterface } from 'node:readline';
1261+
1262+
(async function processLineByLine() {
1263+
try {
1264+
const rl = createInterface({
1265+
input: createReadStream('big-file.txt'),
1266+
crlfDelay: Infinity,
1267+
});
1268+
1269+
rl.on('line', (line) => {
1270+
// Process the line.
1271+
});
1272+
1273+
await once(rl, 'close');
1274+
1275+
console.log('File processed.');
1276+
} catch (err) {
1277+
console.error(err);
1278+
}
1279+
})();
1280+
```
1281+
1282+
```cjs
11761283
const { once } = require('node:events');
11771284
const { createReadStream } = require('node:fs');
11781285
const { createInterface } = require('node:readline');

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy