summaryrefslogtreecommitdiff
path: root/lib/console.js
diff options
context:
space:
mode:
authorAnna Henningsen <anna@addaleax.net>2018-03-17 12:53:09 +0100
committerAnna Henningsen <anna@addaleax.net>2018-04-12 23:23:55 +0200
commitce58df58d0360779d16d60ce3bb0e9979ec5fdf4 (patch)
tree74428887af79688570649d44da4973bece5ab612 /lib/console.js
parent65765989d3d15e9d38dea8297c9c803df03eeb34 (diff)
downloadnode-new-ce58df58d0360779d16d60ce3bb0e9979ec5fdf4.tar.gz
console: allow `options` object as constructor arg
PR-URL: https://github.com/nodejs/node/pull/19372 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'lib/console.js')
-rw-r--r--lib/console.js27
1 files changed, 21 insertions, 6 deletions
diff --git a/lib/console.js b/lib/console.js
index 456c0cc439..9557b27fbc 100644
--- a/lib/console.js
+++ b/lib/console.js
@@ -51,16 +51,28 @@ const {
// Track amount of indentation required via `console.group()`.
const kGroupIndent = Symbol('groupIndent');
-function Console(stdout, stderr, ignoreErrors = true) {
+function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
if (!(this instanceof Console)) {
- return new Console(stdout, stderr, ignoreErrors);
+ return new Console(...arguments);
}
+
+ let stdout, stderr, ignoreErrors;
+ if (options && typeof options.write !== 'function') {
+ ({
+ stdout,
+ stderr = stdout,
+ ignoreErrors = true
+ } = options);
+ } else {
+ stdout = options;
+ stderr = arguments[1];
+ ignoreErrors = arguments[2] === undefined ? true : arguments[2];
+ }
+
if (!stdout || typeof stdout.write !== 'function') {
throw new ERR_CONSOLE_WRITABLE_STREAM('stdout');
}
- if (!stderr) {
- stderr = stdout;
- } else if (typeof stderr.write !== 'function') {
+ if (!stderr || typeof stderr.write !== 'function') {
throw new ERR_CONSOLE_WRITABLE_STREAM('stderr');
}
@@ -369,7 +381,10 @@ Console.prototype.table = function(tabularData, properties) {
return final(keys, values);
};
-module.exports = new Console(process.stdout, process.stderr);
+module.exports = new Console({
+ stdout: process.stdout,
+ stderr: process.stderr
+});
module.exports.Console = Console;
function noop() {}