Browse Source

repl: avoid using forEach

PR-URL: https://github.com/nodejs/node/pull/11582
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
v6
James M Snell 8 years ago
parent
commit
ae9e640f30
  1. 63
      lib/repl.js

63
lib/repl.js

@ -68,7 +68,10 @@ const GLOBAL_OBJECT_PROPERTIES = [
'ReferenceError', 'SyntaxError', 'TypeError', 'URIError', 'Math', 'JSON'
];
const GLOBAL_OBJECT_PROPERTY_MAP = {};
GLOBAL_OBJECT_PROPERTIES.forEach((p) => GLOBAL_OBJECT_PROPERTY_MAP[p] = p);
for (var n = 0; n < GLOBAL_OBJECT_PROPERTIES.length; n++) {
GLOBAL_OBJECT_PROPERTY_MAP[GLOBAL_OBJECT_PROPERTIES[n]] =
GLOBAL_OBJECT_PROPERTIES[n];
}
try {
// hack for require.resolve("./relative") to work properly.
@ -585,13 +588,17 @@ REPLServer.prototype.createContext = function() {
enumerable: true,
get: () => _console
});
Object.getOwnPropertyNames(global).filter((name) => {
if (name === 'console' || name === 'global') return false;
return GLOBAL_OBJECT_PROPERTY_MAP[name] === undefined;
}).forEach((name) => {
Object.defineProperty(context, name,
Object.getOwnPropertyDescriptor(global, name));
});
var names = Object.getOwnPropertyNames(global);
for (var n = 0; n < names.length; n++) {
var name = names[n];
if (name === 'console' || name === 'global')
continue;
if (GLOBAL_OBJECT_PROPERTY_MAP[name] === undefined) {
Object.defineProperty(context, name,
Object.getOwnPropertyDescriptor(global, name));
}
}
}
const module = new Module('<repl>');
@ -662,10 +669,8 @@ function ArrayStream() {
Stream.call(this);
this.run = function(data) {
var self = this;
data.forEach(function(line) {
self.emit('data', line + '\n');
});
for (var n = 0; n < data.length; n++)
this.emit('data', `${data[n]}\n`);
};
}
util.inherits(ArrayStream, Stream);
@ -709,11 +714,11 @@ function complete(line, callback) {
var tmp = this.lines.slice();
// Kill off all function declarations to push all local variables into
// global scope
this.lines.level.forEach(function(kill) {
if (kill.isFunction) {
for (var n = 0; n < this.lines.level.length; n++) {
var kill = this.lines.level[n];
if (kill.isFunction)
tmp[kill.line] = '';
}
});
}
var flat = new ArrayStream(); // make a new "input" stream
var magic = new REPLServer('', flat); // make a nested REPL
replMap.set(magic, replMap.get(this));
@ -847,9 +852,8 @@ function complete(line, callback) {
addStandardGlobals(completionGroups, filter);
} else if (Array.isArray(globals[0])) {
// Add grouped globals
globals.forEach(function(group) {
completionGroups.push(group);
});
for (var n = 0; n < globals.length; n++)
completionGroups.push(globals[n]);
} else {
completionGroups.push(globals);
addStandardGlobals(completionGroups, filter);
@ -1155,12 +1159,13 @@ function defineDefaultCommands(repl) {
(max, name) => Math.max(max, name.length),
0
);
names.forEach((name) => {
const cmd = this.commands[name];
const spaces = ' '.repeat(longestNameLength - name.length + 3);
const line = '.' + name + (cmd.help ? spaces + cmd.help : '') + '\n';
for (var n = 0; n < names.length; n++) {
var name = names[n];
var cmd = this.commands[name];
var spaces = ' '.repeat(longestNameLength - name.length + 3);
var line = `.${name}${cmd.help ? spaces + cmd.help : ''}\n`;
this.outputStream.write(line);
});
}
this.displayPrompt();
}
});
@ -1184,15 +1189,13 @@ function defineDefaultCommands(repl) {
try {
var stats = fs.statSync(file);
if (stats && stats.isFile()) {
var self = this;
var data = fs.readFileSync(file, 'utf8');
var lines = data.split('\n');
this.displayPrompt();
lines.forEach(function(line) {
if (line) {
self.write(line + '\n');
}
});
for (var n = 0; n < lines.length; n++) {
if (lines[n])
this.write(`${lines[n]}\n`);
}
} else {
this.outputStream.write('Failed to load:' + file +
' is not a valid file\n');

Loading…
Cancel
Save