From 8eeaba62bc2c259e60b0ef725e4550a1b431559e Mon Sep 17 00:00:00 2001 From: Flandre Scarlet Date: Thu, 20 Jul 2017 16:05:39 +0800 Subject: [PATCH] readline: name some anonymous functions PR-URL: https://github.com/nodejs/node/pull/14297 Refs: https://github.com/nodejs/node/issues/8913 Refs: https://github.com/nodejs/node/pull/14297#pullrequestreview-50206772 Reviewed-By: Khaidi Chu Reviewed-By: Colin Ihrig Reviewed-By: James M Snell --- lib/readline.js | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/lib/readline.js b/lib/readline.js index 8a6a5e2d1ac203..90891542421c15 100644 --- a/lib/readline.js +++ b/lib/readline.js @@ -126,9 +126,11 @@ function Interface(input, output, completer, terminal) { // Check arity, 2 - for async, 1 for sync if (typeof completer === 'function') { - this.completer = completer.length === 2 ? completer : function(v, cb) { - cb(null, completer(v)); - }; + this.completer = completer.length === 2 ? + completer : + function completerWrapper(v, cb) { + cb(null, completer(v)); + }; } this.setPrompt(prompt); @@ -171,15 +173,23 @@ function Interface(input, output, completer, terminal) { } if (!this.terminal) { - input.on('data', ondata); - input.on('end', onend); - self.once('close', function() { + function onSelfCloseWithoutTerminal() { input.removeListener('data', ondata); input.removeListener('end', onend); - }); - this._decoder = new StringDecoder('utf8'); + } + input.on('data', ondata); + input.on('end', onend); + self.once('close', onSelfCloseWithoutTerminal); + this._decoder = new StringDecoder('utf8'); } else { + function onSelfCloseWithTerminal() { + input.removeListener('keypress', onkeypress); + input.removeListener('end', ontermend); + if (output !== null && output !== undefined) { + output.removeListener('resize', onresize); + } + } emitKeypressEvents(input, this); @@ -202,13 +212,7 @@ function Interface(input, output, completer, terminal) { if (output !== null && output !== undefined) output.on('resize', onresize); - self.once('close', function() { - input.removeListener('keypress', onkeypress); - input.removeListener('end', ontermend); - if (output !== null && output !== undefined) { - output.removeListener('resize', onresize); - } - }); + self.once('close', onSelfCloseWithTerminal); } input.resume(); @@ -450,7 +454,7 @@ Interface.prototype._tabComplete = function(lastKeypressWasTab) { var self = this; self.pause(); - self.completer(self.line.slice(0, self.cursor), function(err, rv) { + self.completer(self.line.slice(0, self.cursor), function onComplete(err, rv) { self.resume(); if (err) { @@ -464,7 +468,7 @@ Interface.prototype._tabComplete = function(lastKeypressWasTab) { // Apply/show completions. if (lastKeypressWasTab) { self._writeToOutput('\r\n'); - var width = completions.reduce(function(a, b) { + var width = completions.reduce(function completionReducer(a, b) { return a.length > b.length ? a : b; }).length + 2; // 2 space padding var maxColumns = Math.floor(self.columns / width); @@ -485,7 +489,9 @@ Interface.prototype._tabComplete = function(lastKeypressWasTab) { } // If there is a common prefix to all matches, then apply that portion. - var f = completions.filter(function(e) { if (e) return e; }); + var f = completions.filter(function completionFilter(e) { + if (e) return e; + }); var prefix = commonPrefix(f); if (prefix.length > completeOn.length) { self._insertString(prefix.slice(completeOn.length));