From a110409b2af0adfc714aa8211a23bcafbafe26d4 Mon Sep 17 00:00:00 2001 From: HEESEUNG Date: Sat, 10 Aug 2024 10:49:40 +0900 Subject: [PATCH] console: use validateOneOf for colorMode validation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refactor the Console constructor to use validateOneOf for validating the colorMode parameter. PR-URL: https://github.com/nodejs/node/pull/54245 Reviewed-By: Kohei Ueno Reviewed-By: Tobias Nießen Reviewed-By: Yagiz Nizipli Reviewed-By: Jake Yuesong Li Reviewed-By: Luigi Pinca --- lib/internal/console/constructor.js | 5 ++--- test/parallel/test-console-tty-colors.js | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/internal/console/constructor.js b/lib/internal/console/constructor.js index e3c7a2cb5b36f8..4a48dd4a0864e7 100644 --- a/lib/internal/console/constructor.js +++ b/lib/internal/console/constructor.js @@ -44,7 +44,6 @@ const { isStackOverflowError, codes: { ERR_CONSOLE_WRITABLE_STREAM, - ERR_INVALID_ARG_VALUE, ERR_INCOMPATIBLE_OPTION_PAIR, }, } = require('internal/errors'); @@ -52,6 +51,7 @@ const { validateArray, validateInteger, validateObject, + validateOneOf, } = require('internal/validators'); const { previewEntries } = internalBinding('util'); const { Buffer: { isBuffer } } = require('buffer'); @@ -135,8 +135,7 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */) { throw new ERR_CONSOLE_WRITABLE_STREAM('stderr'); } - if (typeof colorMode !== 'boolean' && colorMode !== 'auto') - throw new ERR_INVALID_ARG_VALUE('colorMode', colorMode); + validateOneOf(colorMode, 'colorMode', ['auto', true, false]); if (groupIndentation !== undefined) { validateInteger(groupIndentation, 'groupIndentation', diff --git a/test/parallel/test-console-tty-colors.js b/test/parallel/test-console-tty-colors.js index 0eb51c72898f7c..69951e27e3c6b0 100644 --- a/test/parallel/test-console-tty-colors.js +++ b/test/parallel/test-console-tty-colors.js @@ -67,7 +67,7 @@ check(false, false, false); }); }, { - message: `The argument 'colorMode' is invalid. Received ${received}`, + message: `The argument 'colorMode' must be one of: 'auto', true, false. Received ${received}`, code: 'ERR_INVALID_ARG_VALUE' } );