From 90d91abbfe32d9ce6606bd8db3cef1ac37e45923 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 6c34a9ededc..c892b91919d 100644 --- a/lib/internal/console/constructor.js +++ b/lib/internal/console/constructor.js @@ -39,7 +39,6 @@ const { codes: { ERR_CONSOLE_WRITABLE_STREAM, ERR_INCOMPATIBLE_OPTION_PAIR, - ERR_INVALID_ARG_VALUE, }, isStackOverflowError, } = require('internal/errors'); @@ -47,6 +46,7 @@ const { validateArray, validateInteger, validateObject, + validateOneOf, } = require('internal/validators'); const { previewEntries } = internalBinding('util'); const { Buffer: { isBuffer } } = require('buffer'); @@ -124,8 +124,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 0eb51c72898..69951e27e3c 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' } );