Skip to content

Commit f94f210

Browse files
mfdebianaduh95
authored andcommitted
doc: add esm examples to node:repl
PR-URL: #55432 Reviewed-By: Tierney Cyren <hello@bnb.im> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 7a10ef8 commit f94f210

File tree

1 file changed

+150
-16
lines changed

1 file changed

+150
-16
lines changed

doc/api/repl.md

Lines changed: 150 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ The `node:repl` module provides a Read-Eval-Print-Loop (REPL) implementation
1010
that is available both as a standalone program or includible in other
1111
applications. It can be accessed using:
1212

13-
```js
13+
```mjs
14+
import repl from 'node:repl';
15+
```
16+
17+
```cjs
1418
const repl = require('node:repl');
1519
```
1620

@@ -106,7 +110,14 @@ The default evaluator provides access to any variables that exist in the global
106110
scope. It is possible to expose a variable to the REPL explicitly by assigning
107111
it to the `context` object associated with each `REPLServer`:
108112

109-
```js
113+
```mjs
114+
import repl from 'node:repl';
115+
const msg = 'message';
116+
117+
repl.start('> ').context.m = msg;
118+
```
119+
120+
```cjs
110121
const repl = require('node:repl');
111122
const msg = 'message';
112123

@@ -124,7 +135,19 @@ $ node repl_test.js
124135
Context properties are not read-only by default. To specify read-only globals,
125136
context properties must be defined using `Object.defineProperty()`:
126137

127-
```js
138+
```mjs
139+
import repl from 'node:repl';
140+
const msg = 'message';
141+
142+
const r = repl.start('> ');
143+
Object.defineProperty(r.context, 'm', {
144+
configurable: false,
145+
enumerable: true,
146+
value: msg,
147+
});
148+
```
149+
150+
```cjs
128151
const repl = require('node:repl');
129152
const msg = 'message';
130153

@@ -280,20 +303,34 @@ When a new [`repl.REPLServer`][] is created, a custom evaluation function may be
280303
provided. This can be used, for instance, to implement fully customized REPL
281304
applications.
282305

283-
The following illustrates a hypothetical example of a REPL that performs
284-
translation of text from one language to another:
306+
The following illustrates an example of a REPL that squares a given number:
285307

286-
```js
308+
```mjs
309+
import repl from 'node:repl';
310+
311+
function byThePowerOfTwo(number) {
312+
return number * number;
313+
}
314+
315+
function myEval(cmd, context, filename, callback) {
316+
callback(null, byThePowerOfTwo(cmd));
317+
}
318+
319+
repl.start({ prompt: 'Enter a number: ', eval: myEval });
320+
```
321+
322+
```cjs
287323
const repl = require('node:repl');
288-
const { Translator } = require('translator');
289324

290-
const myTranslator = new Translator('en', 'fr');
325+
function byThePowerOfTwo(number) {
326+
return number * number;
327+
}
291328

292329
function myEval(cmd, context, filename, callback) {
293-
callback(null, myTranslator.translate(cmd));
330+
callback(null, byThePowerOfTwo(cmd));
294331
}
295332

296-
repl.start({ prompt: '> ', eval: myEval });
333+
repl.start({ prompt: 'Enter a number: ', eval: myEval });
297334
```
298335

299336
#### Recoverable errors
@@ -354,7 +391,21 @@ To fully customize the output of a [`repl.REPLServer`][] instance pass in a new
354391
function for the `writer` option on construction. The following example, for
355392
instance, simply converts any input text to upper case:
356393

357-
```js
394+
```mjs
395+
import repl from 'node:repl';
396+
397+
const r = repl.start({ prompt: '> ', eval: myEval, writer: myWriter });
398+
399+
function myEval(cmd, context, filename, callback) {
400+
callback(null, cmd);
401+
}
402+
403+
function myWriter(output) {
404+
return output.toUpperCase();
405+
}
406+
```
407+
408+
```cjs
358409
const repl = require('node:repl');
359410

360411
const r = repl.start({ prompt: '> ', eval: myEval, writer: myWriter });
@@ -380,7 +431,16 @@ added: v0.1.91
380431
Instances of `repl.REPLServer` are created using the [`repl.start()`][] method
381432
or directly using the JavaScript `new` keyword.
382433

383-
```js
434+
```mjs
435+
import repl from 'node:repl';
436+
437+
const options = { useColors: true };
438+
439+
const firstInstance = repl.start(options);
440+
const secondInstance = new repl.REPLServer(options);
441+
```
442+
443+
```cjs
384444
const repl = require('node:repl');
385445

386446
const options = { useColors: true };
@@ -424,7 +484,20 @@ reference to the `context` object as the only argument.
424484
This can be used primarily to re-initialize REPL context to some pre-defined
425485
state:
426486

427-
```js
487+
```mjs
488+
import repl from 'node:repl';
489+
490+
function initializeContext(context) {
491+
context.m = 'test';
492+
}
493+
494+
const r = repl.start({ prompt: '> ' });
495+
initializeContext(r.context);
496+
497+
r.on('reset', initializeContext);
498+
```
499+
500+
```cjs
428501
const repl = require('node:repl');
429502

430503
function initializeContext(context) {
@@ -475,7 +548,25 @@ properties:
475548

476549
The following example shows two new commands added to the REPL instance:
477550

478-
```js
551+
```mjs
552+
import repl from 'node:repl';
553+
554+
const replServer = repl.start({ prompt: '> ' });
555+
replServer.defineCommand('sayhello', {
556+
help: 'Say hello',
557+
action(name) {
558+
this.clearBufferedCommand();
559+
console.log(`Hello, ${name}!`);
560+
this.displayPrompt();
561+
},
562+
});
563+
replServer.defineCommand('saybye', function saybye() {
564+
console.log('Goodbye!');
565+
this.close();
566+
});
567+
```
568+
569+
```cjs
479570
const repl = require('node:repl');
480571

481572
const replServer = repl.start({ prompt: '> ' });
@@ -637,7 +728,14 @@ The `repl.start()` method creates and starts a [`repl.REPLServer`][] instance.
637728

638729
If `options` is a string, then it specifies the input prompt:
639730

640-
```js
731+
```mjs
732+
import repl from 'node:repl';
733+
734+
// a Unix style prompt
735+
repl.start('$ ');
736+
```
737+
738+
```cjs
641739
const repl = require('node:repl');
642740

643741
// a Unix style prompt
@@ -709,7 +807,43 @@ separate I/O interfaces.
709807
The following example, for instance, provides separate REPLs on `stdin`, a Unix
710808
socket, and a TCP socket:
711809

712-
```js
810+
```mjs
811+
import net from 'node:net';
812+
import repl from 'node:repl';
813+
import process from 'node:process';
814+
815+
let connections = 0;
816+
817+
repl.start({
818+
prompt: 'Node.js via stdin> ',
819+
input: process.stdin,
820+
output: process.stdout,
821+
});
822+
823+
net.createServer((socket) => {
824+
connections += 1;
825+
repl.start({
826+
prompt: 'Node.js via Unix socket> ',
827+
input: socket,
828+
output: socket,
829+
}).on('exit', () => {
830+
socket.end();
831+
});
832+
}).listen('/tmp/node-repl-sock');
833+
834+
net.createServer((socket) => {
835+
connections += 1;
836+
repl.start({
837+
prompt: 'Node.js via TCP socket> ',
838+
input: socket,
839+
output: socket,
840+
}).on('exit', () => {
841+
socket.end();
842+
});
843+
}).listen(5001);
844+
```
845+
846+
```cjs
713847
const net = require('node:net');
714848
const repl = require('node:repl');
715849
let connections = 0;

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