自定义评估函数


🌐 Custom evaluation functions

当创建一个新的 repl.REPLServer 时,可以提供自定义评估函数。例如,这可以用于实现完全自定义的 REPL 应用。

🌐 When a new repl.REPLServer is created, a custom evaluation function may be provided. This can be used, for instance, to implement fully customized REPL applications.

以下展示了一个假设的 REPL 示例,它可以将文本从一种语言翻译成另一种语言:

🌐 The following illustrates a hypothetical example of a REPL that performs translation of text from one language to another:

const repl = require('node:repl');
const { Translator } = require('translator');

const myTranslator = new Translator('en', 'fr');

function myEval(cmd, context, filename, callback) {
  callback(null, myTranslator.translate(cmd));
}

repl.start({ prompt: '> ', eval: myEval });